Ubuntu Mirror

Hosting a local Ubuntu repository is very useful for reducing bandwidth usage outside of the local network. To create an Ubuntu mirror, all we need are Apache2 and rsync.

First install Apache2 and rsync if you don't already have them:

apt-get install apache2 rsync

Configure Apache2 to your liking, making sure set a directory to house the repository. For example:

mkdir /var/www/ubuntu

Next, we need the following script to rsync. Be sure to change rsyncsource and basedir as necessary:

#/bin/dash
fatal() {
  echo "$1"
  exit 1
}
warn() {
  echo "$1"
}
# Find a source mirror near you which supports rsync on
# https://launchpad.net/ubuntu/+archivemirrors
# rsync://<iso-country-code>.rsync.archive.ubuntu.com/ubuntu should always work
RSYNCSOURCE=rsync://archive.ubuntu.mirror.isp.com/ubuntu
# Define where you want the mirror-data to be on your mirror
BASEDIR=/var/www/ubuntuarchive/
if [ ! -d ${BASEDIR} ]; then
  warn "${BASEDIR} does not exist yet, trying to create it..."
  mkdir -p ${BASEDIR} || fatal "Creation of ${BASEDIR} failed."
fi
rsync --recursive --times --links --hard-links --stats --exclude "Packages*" --exclude "Sources*" --exclude "Release*" --exclude "InRelease" ${RSYNCSOURCE} ${BASEDIR} || fatal "First stage of sync failed."
rsync --recursive --times --links --hard-links --stats --delete --delete-after ${RSYNCSOURCE} ${BASEDIR} || fatal "Second stage of sync failed."
date -u > ${BASEDIR}/project/trace/$(hostname -f)

If you use NFS for storage, you might add the --omit-dir-times flag to the rsync command.

Finally, set the script as a cron job:

crontab -e

And in the crontab, set the cron job using whatever times you want:

*    19    *    *    *    /path/to/script