Skip to content

Instantly share code, notes, and snippets.

@warewolf
Created May 9, 2014 19:26
Show Gist options
  • Select an option

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

Select an option

Save warewolf/f65bf73a26518e346bc1 to your computer and use it in GitHub Desktop.
richard's backup scripts
#!/bin/bash
# backup wrapper
EMAIL="backups@yourdomain.com"
CONFIG=$1
CONFIG_DIR=/mnt/backups/configs/
EMAIL_SUBJ="nas backup: "
# 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
BACKUP="server.com"
SHARE="192.168.122.1::backup"
ENABLED="1"
/sys
/var/named/proc
*.mp4
*.iso
*.mp3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment