Skip to content

Instantly share code, notes, and snippets.

@warewolf
Created July 20, 2015 15:05
Show Gist options
  • Select an option

  • Save warewolf/45825da4cdf0b013ad4a to your computer and use it in GitHub Desktop.

Select an option

Save warewolf/45825da4cdf0b013ad4a to your computer and use it in GitHub Desktop.
#!/bin/bash -x
# backup wrapper
EMAIL="[email protected]"
CONFIG=$1
CONFIG_DIR=/mnt/rdiffbackup/configs/
EMAIL_SUBJ="readynas backup: "
# I forget why this is here
#sleep $[$RANDOM%15]m
# try to locate config file that has our params in it
if [ -e $CONFIG_DIR/$CONFIG ]; then
. $CONFIG_DIR/$CONFIG
else
echo "no config for $CONFIG" | mail -s "$EMAIL_SUBJ config $CONFIG not found" $EMAIL
fi
# enable/disable backups for temporary one-shot backups
if [ "x$ENABLED" != "x1" ]; then
exit 0;
fi
# timestamp
STAMP_NOW=`date +%FT%T%z`
TOUCH_NOW=`date +%FT%T`
touch --date="$TOUCH_NOW" $BACKUPS/$BACKUP
# destination backup root directory
BACKUPS=/mnt/rdiffbackup
# the previous backup - we use this to "diff" against
PREVIOUS=`readlink $BACKUPS/$BACKUP`
# log file
LOGFILE=$BACKUPS/logs/$BACKUP.$STAMP_NOW
touch $LOGFILE;
# test connectivity
rsync -l --timeout 1 -q $SHARE >> $LOGFILE
ERROR=$?
if [ "x$ERROR" != "x0" ]; then
echo "rsync $BACKUP failed, error code $ERROR" | mail -s "$EMAIL_SUBJ $BACKUP fail" $EMAIL
exit 0
fi
echo "rsync connectivity test success, starting transfer at $STAMP_NOW" | mail -s "$EMAIL_SUBJ $BACKUP starting" $EMAIL
# we're starting a new backup, so the old "current" backup becomes the "previous" backup for us to diff against.
rm $BACKUPS/$BACKUP-previous
mv $BACKUPS/$BACKUP $BACKUPS/$BACKUP-previous
# create directory for our destination (most current copy)
CURRENT="$BACKUPS/backups/$BACKUP"_"$STAMP_NOW"
mkdir -p $CURRENT
# softlink it for easier rsync handling -- rsync doesn't like colons in filenames, it makes it think it's a remote share
ln -s $CURRENT $BACKUPS/$BACKUP
rsync \
--partial --progress \
--itemize-changes \
--recursive --links \
--hard-links \
--perms \
--times \
--group \
--owner \
--numeric-ids \
--devices --specials \
--delete \
--exclude-from=$BACKUPS/$BACKUP.exclude \
--sparse \
--link-dest=$PREVIOUS \
--log-file=$BACKUPS/logs/$BACKUP.$STAMP_NOW \
--verbose --compress $SHARE $BACKUPS/$BACKUP
EXITCODE=$?
( echo -e "backup $BACKUP exit $EXITCODE\n\n\nLog file:\n" && cat $LOGFILE ) | mailx -s "$EMAIL_SUBJ $BACKUP finished" $EMAIL
touch --date="$TOUCH_NOW" $BACKUPS/$BACKUP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment