How to: download entire repositories and check for updates daily CentOS/RHEL/Fedora

For those of you who deploy many systems running on Linux, especially the CentOS/RHEL/Fedora variety, you may want to speed up the process. Especially when it comes to installing packages and updates without having to wait for your internet connection. Luckily, you can download an entire repository and serve it straight from the server. Here’s how you can do so by using a bash script.

First off, you will want to get the exact names of our repositories. This is because the names are case sensitive and will need to be implemented into our script letter for letter. You can simply do this by pulling up our repos with:

dnf repolist

or if you prefer yum:

yum repolist

To the left you will find the actual names for the repos that you’ll be using. Make note of it for when we place it in the script. Now, with your preferred text editor(vi or nano) go ahead and make the file downloadrepo.sh.

nano downloadrepo.sh

You will then want to input the following into the script.

#!/bin/bash

PKGREPO=(appstream baseos extras epel)

for REPO in ${PKGREPO[@]}
do
dnf reposync -p /path/to/directory --download-metadata --repoid=${REPO}
done

Here we’ve made the variable PKGREPO and the brackets represent the list of repos that we are going to define. -p will tell us to download the following repositories in our variable to the preferred directory(make sure to specify one instead of /path/to/directory).

When you’re ready go ahead and save it and exit out of the editor. Now, make it an executable with this:

chmod +x ./downloadrepo.sh

And then run it:

./download.sh

All repos defined within the PKGREPO will be downloaded until completed. If you have a slow internet connection this may take awhile. Also make sure you have plenty of space on the storage drive that you are planning on downloading all of this to, preferably 50GB to 70GB.

Checking for repo updates daily

We now will be making a script to check for any new package updates for each of our repos. Every day cron will execute any scripts placed into the /etc/cron.daily directory. Let’s go ahead and create our script file there with our preferred text editor:

nano /etc/cron.daily/updaterepo.sh

We will then be adding the following code, similar to the one above, but with some changes:

#!/usr/bin/env bash

PATH=/usr/bin
export PATH

PKGREPO=(appstream baseos extras epel)

for REPO in ${PKGREPO[@]}
do
dnf reposync -p /path/to/directory --download-metadata --newest-only --repoid=${REPO}
done

Using a different shebang(#!/usr/bin/env bash) is entirely optional. We want to give the cron user a path for the dnf program which is /usr/bin/dnf, for example your distribution may vary. This allows us to run the command without having to define a full path to it.

Before we forget, make sure to make the script executable via chmod:

chmod +x /etc/cron.hourly/updaterepo.sh

You may test the script to make sure it’s working without any errors:

cd /etc/cron.hourly/ && ./updaterepo.sh

You may see packages getting skipped, this is normal as there is no version that needs to be updated.