public
Last active

Backup script using rsync

  • Download Gist
backup.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#!/bin/sh
 
# Taken from http://samba.anu.edu.au/rsync/examples.html
 
# This script does personal backups. You will end up with a 7 day
# rotating incremental backup. The incrementals will go into
# subdirectories named after the day of the week, and the
# current full backup goes into a directory called "current".
 
# directory to backup
BDIR=$HOME
 
# excludes file - this contains a wildcard pattern per line of files
# to exclude
EXCLUDES=$HOME/local/etc/backup_exclude
 
# the backup location
BLOCATION=/Volumes/Storage/Backups
 
# set to the location of directory containing additional
# backup scripts to be run prior to backing up
SCRIPTS_DIR=$HOME/local/etc/backup.d
 
#######################################################
 
# run the backup scripts located in SCRIPTS_DIR
if [[ -d $SCRIPTS_DIR ]]
then
for file in `ls $SCRIPTS_DIR`
do
$SCRIPTS_DIR/$file $BDIR 2> /dev/null > /dev/null
if (( $? != 0 ))
then
ERRCODE=$?
echo execution of script $SCRIPTS_DIR/$file failed
exit $?
fi
done
fi
 
# set some options for rsync
BACKUPDIR=`date +%A`
OPTS="--extended-attributes --force --ignore-errors --delete-excluded --exclude-from=$EXCLUDES
--delete --times --backup --backup-dir=../$BACKUPDIR -a"
 
# the following line clears the last weeks incremental directory
[ -d $HOME/emptydir ] || mkdir $HOME/emptydir
rsync --delete -a $HOME/emptydir/ $BLOCATION/$USER/$BACKUPDIR/
rmdir $HOME/emptydir
 
# now the actual transfer
rsync $OPTS $BDIR $BLOCATION/$USER/current

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.