Update just one repository on Linux

So if you build your own packages (rpm/dpkg/other), you might have your own repository of packages for your systems. And if you want to tell your hosts to update just the one repository, you might be looking for a solution.

A user on Ask Ubuntu had a fantastic answer for Ubuntu/debian flavor: http://askubuntu.com/questions/65245/apt-get-update-only-for-a-specific-repository/197532#197532.

For those of you who don’t want to click through, here is a summary of the entries, collapsed down to just one file (which might not be the best solution, but it works).
Add these lines to ~/.bashrc:
update-repo() {
for source in "$@"; do
sudo apt-get update -o Dir::Etc::sourcelist="sources.list.d/${source}" \
-o Dir::Etc::sourceparts="-" -o APT::Get::List-Cleanup="0"
done
}

_ppa_lists(){
local cur
_init_completion || return

COMPREPLY=( $( find /etc/apt/sources.list.d/ -name "*$cur*.list" \
-exec basename {} \; 2> /dev/null ) )
return 0
} &&
complete -F _ppa_lists update-repo

With rpm-based systems like RHEL/CentOS/Fedora, you are using yum or dnf. Here’s my dnf implementation:

_command_dnf=yum
which dnf 1>/dev/null 2>&1 && _command_dnf="$( which dnf 2>/dev/null )"

# update-repo command for dnf update just one repository
update-repo() {
case "${_command_dnf}" in
*dnf)
for source in "$@"; do
sudo "${_command_dnf}" check-update -q --refresh --disablerepo=* --enablerepo="${source}"
done
;;
*yum)
for source in "$@"; do
sudo "${_command_dnf}" clean metadata -q --disablerepo=* --enablerepo="${source}" -q; yum check-update -q --disablerepo=* --enablerepo="${source}"
done
;;
esac
}

# autocomplete for update-repo
_repo_lists() {
local cur
_init_completion || return
COMPREPLY=( $( grep -hoiE -- "^\[.*\]" /etc/yum.repos.d/* | tr -d '[]' | grep -E "^${2:-.*}" ) )
return 0
} &&
complete -F _repo_lists -o filenames update-repo

The functions whose names start with an underscore are the auto-complete commands. Who doesn’t like a good tab auto-completion? Now that I learned how to do my own bash-completion, I’ll be doing it a lot more!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s