I have one local server and three virtuals at Linode. Every night my local server runs a cron job that uses rsync to suck over copies of the important files. (I also have Linode's snapshot backup service enabled.) I keep five days worth of backups in dated directories (e.g., 201019) plus one backup per month for each of the past five months. My backup device is a 4 TB external USB drive. Perhaps this script will give you some useful ideas.
I connect to the remotes via an OpenVPN tunnel which uses the 10.1.1.0/24 subnet.
Code:
#!/bin/bash
LOGDIR=/var/log/backup
DESTDIR=/media/external/backup
DIRS='etc usr/local var home'
FILEDIR=/usr/local/etc/backup
EXCL=$FILEDIR/local_excludes
ROTATE=5 # keep this many consecutive days
MONTHS=5 # keep this many months
TODAY=$(date +%y%m%d)
STALE=$(date +%y%m%d --date="$ROTATE days ago")
SMONTH=$(date +%y%m* --date="$MONTHS months ago")
LOG="${LOGDIR}/${TODAY}.log"
echo "*** - $(date +%c) - Backup starting - ***" > $LOG
cd /
STALEDIR="${DESTDIR}/${STALE}"
STALEMDIR="${DESTDIR}/${SMONTH}"
# keep one backup and log each month
if [ "$(date +%d)" != "01" ]
then
echo "Deleting stale backup $STALEDIR" >> $LOG
rm -rf $STALEDIR
echo "Deleting stale log $STALE" >> $LOG
rm -f $LOGDIR.$STALE.log
# keep $MONTHS of backups
echo "Deleting stale monthly $STALEMDIR" >> $LOG
rm -rf $STALEMDIR
fi
echo "Checking for backup device" >> $LOG
# check that backup device is mounted
TEST=$(df | grep "/media/external")
if [ "$TEST" = "" ]
then
# not mounted try remounting
umount /media/external
sleep 4
mount /media/external
sleep 4
TEST=$(mount | grep "/media/external")
if [ "$TEST" = "" ]
then
echo "Cannot mount backup device!" >> $LOG
echo 'Cannot mount backup device' | mail -s 'backup failure' admin
exit 1
fi
fi
echo "Creating new backup directory $DEST" >> $LOG
DEST="${DESTDIR}/${TODAY}"
mkdir -p $DEST/local $DEST/server1 $DEST/server2 $DEST/server3
echo "*** - $(date +%c) - Backing up local directories - ***" >> $LOG
for d in $DIRS
do
echo "Backing up $d" >> $LOG
rsync -av $d $DEST/local --exclude-from=$EXCL --delete-excluded >> $LOG 2>&1
done
echo "*** - $(date +%c) - Backing up remote servers - ***" >> $LOG
echo "*** - $(date +%c) - Backing up server1 - ***" >> $LOG
rsync -avr 10.1.1.1:/ $DEST/server1 --files-from=$FILEDIR/server1_includes --exclude-from=$FILEDIR/server1_excludes --delete-excluded >> $LOG
echo "*** - $(date +%c) - Backing up server2 - ***" >> $LOG
rsync -avr 10.1.1.2:/ $DEST/server2 --files-from=$FILEDIR/server2_includes --exclude-from=$FILEDIR/server2_excludes --delete-excluded >> $LOG
echo "*** - $(date +%c) - Backing up server3 - ***" >> $LOG
rsync -avr 10.1.1.3:/ $DEST/server3 --files-from=$FILEDIR/server3_includes --exclude-from=$FILEDIR/server3_excludes --delete-excluded >> $LOG
# keep another copy of today's backup on a different local drive
rm -rf /home/local_server/backup/*
echo "*** - $(date +%c) - Making local reserve copy - ***" >> $LOG
rsync -avr $DEST /home/local_server/backup
echo "*** - $(date +%c) - Backup concluded - ***" >> $LOG
exit 0
I keep the lists of files and directories to backup on the remote servers in /usr/local/etc. See the documentation for the "--files-from" and "--exclude-from" switches to rsync: https://linux.die.net/man/1/rsync
Bookmarks