Deploy A Local Ubuntu Mirror using Rsync

I've been having problems with using apt-mirror to create a local mirror for Ubuntu 16.04. Luckily, we can use rsync as an alternative solution that we will never have to change, and can be run from any distro. However, it does require more storage space. At the time of writing this article, my ubuntu mirror takes up 1,084 GiB of space.

Steps

Firstly, we need to set install apache and a storage path. The storage path should have over 1 TiB of space.

sudo apt install apache2 -y

STORAGE_PATH="/mirror-data"
sudo mkdir -p $STORAGE_PATH
sudo ln -s $STORAGE_PATH /var/www/ubuntu

Configure Apache

Run the following command to configure apache.

sudo echo '<VirtualHost *:80>
    ServerAdmin webmaster@hostname.com

    DocumentRoot /var/www
        <Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>' | sudo tee /etc/apache2/sites-enabled/000-default.conf

Now we need to create a user that will be executing the sync, yet the webserver can access all the files of. It's nice to be able to log in as this user to manually run the sync from time to time.

sudo adduser [mirror user]
sudo adduser www-data [mirror user]
su [mirror user name]
cd $HOME

In the home directory of the user we created, create and execute the following script. I called it sync.sh which will be referenced later in a cron.

#!/bin/bash

STORAGE_PATH="/mirror-data"

rsync \
--recursive \
--links \
--perms \
--times \
--compress \
--progress \
--delete \
rsync://archive.ubuntu.com/ubuntu \
$STORAGE_PATH

You can add --bwlimit=128 to limit the bandwidth to 128 Kilobytes per second. You will need to do the math to convert from kilobytes to Megabits.

Cron

Now we need the mirror to automatically stay up to date. We will run the task every 6 hours. Make sure you only do this after you have completed the initial sync which will probably take longer than 6 hours.

crontab -e

Add the following line:

0 */6 * * * /home/[mirror user]/sync.sh

Use Your Mirror

You can now configure your ubuntu computers to use this mirror for updates.

References

Author

Programster

Stuart is a software developer with a passion for Linux and open source projects.

comments powered by Disqus