Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tyzoid
Last active August 3, 2017 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyzoid/d258443db3a4729e9ca8845e54a2902e to your computer and use it in GitHub Desktop.
Save tyzoid/d258443db3a4729e9ca8845e54a2902e to your computer and use it in GitHub Desktop.
Arch Linux Mirroring Script
#!/bin/bash
# This script is distributed under the MIT license
# Copyright 2017 Tyler Dence
do_sync() {
# Sleep if we need to backoff
sleep "$1";
mirror="$2";
webroot="$3";
# Verify that there's not another sync process going on.
mkdir "/tmp/archsynclock" &>/dev/null || exit;
# Grab lastsync/lastupdate file via http, as it's less overhead
if [ ! -z "$mirrorhttp" ]; then
remote_update="$(wget "$mirrorhttp/lastupdate" -qO-)"
local_update="$(cat "$webroot/lastupdate")";
if [ $remote_update -gt $local_update ]; then
rsync -rtlvH --delete-after --delay-updates --safe-links "$2" "$webroot";
else
wget "$mirrorhttp/lastsync" -O "$webroot/lastsync"
echo 'Nothing more to do!';
fi
else
rsync -rtlvH --delete-after --delay-updates --safe-links "$2" "$webroot";
fi;
rmdir "/tmp/archsynclock";
}
log(){
echo "$@";
echo `date -u "+%Y-%m-%d %H:%M:%S UTC"`" [Arch Repo Sync] ""$@" >> /var/log/arch_sync.log;
}
mlog(){
while read line; do
log "$line";
done;
}
webroot='/var/www/arlm.tyzoid.com/web/';
mirror='rsync://mirror.csclub.uwaterloo.ca/archlinux/';
mirrorhttp='http://mirror.csclub.uwaterloo.ca/archlinux/';
random_backoff=0;
foreground=0;
while getopts "hfrw:m:" opt; do
case "$opt" in
r) random_backoff=$((RANDOM/36)); ;;
f) foreground=1; ;;
m) mirror="$OPTARG";
mirrorhttp=''; ;;
w) webroot="$OPTARG"; ;;
h)
echo "Arch Linux Reposatory Syncing";
echo "Options:";
echo " -r Apply a random backoff of up to 15minutes on the script";
echo " -f Run the sync in the foreground (not recommended)";
echo " -m <rsync url> Use the mirror specified instead of the default. Must be an rsync url.";
echo " -w <webroot> Use the webroot specified instead of the default. This is the location files will be synced to.";
echo " -h Show this help menu";
exit;
esac
done;
if [ "$foreground" -eq 1 ]; then
echo "Foreground, backoff=$random_backoff";
do_sync "$random_backoff" "$mirror" "$webroot" | mlog;
else
echo "Background, backoff=$random_backoff";
{ do_sync "$random_backoff" "$mirror" "$webroot" | mlog & disown; } &>/dev/null
fi;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment