Skip to content

Instantly share code, notes, and snippets.

@alcir
Created July 9, 2017 05:12
Show Gist options
  • Save alcir/7cb799edfb677a50fc38741dc706d73f to your computer and use it in GitHub Desktop.
Save alcir/7cb799edfb677a50fc38741dc706d73f to your computer and use it in GitHub Desktop.
Sample backup script using ZFS snapshot
#!/bin/bash
SCRIPTPATH=$(cd "$(dirname "$0")" && pwd)
ZFSFILESYSTEM=zpool_tank/my_backups
ZFSMOUNTPOINT=/zpool_tank/my_backups
TODAY=`date +%Y%m%d`
YESTERDAY=`date --date="1 days ago" +%Y%m%d`
ERROR=0
MSG=()
LOGFILE=${SCRIPTPATH}/log/lastlog.log"
_V=1
NOSNAPSHOT=0
while [ $# -gt 0 ]; do
case $1 in
-n)
NOSNAPSHOT=1
shift
;;
--)
shift
break
;;
*)
echo "Internal Error: option processing error: $1" 1>&2
exit 1
;;
esac
done
do_rsync() {
# do rsync
SRV=$1
WHAT=$2
DEST=$3
echo Make rsync $SRV | tee -a ${LOGFILE}
rsync -avp --progress --delete --bwlimit=999k --exclude-from=${SCRIPTPATH}/excluded.txt -e "ssh -i /home/user/.ssh/my-key" user@$SRV:$WHAT ${ZFSMOUNTPOINT}/$DEST/ 2>&1 | tee -a ${LOGFILE}
EL=${PIPESTATUS[0]}
if [ $EL -ne 0 ]
then
# Rsync error
echo Rsync error $SRV $EL | tee -a ${LOGFILE}
else
echo Rsync OK $SRV $EL | tee -a ${LOGFILE}
fi
}
echo Backup $TODAY `date +%H%M` | tee ${LOGFILE}
if [ ${NOSNAPSHOT} -eq 0 ]
then
# Take a snapshot
echo Take zfs snapshot @${YESTERDAY} | tee -a ${LOGFILE}
sudo zfs snapshot ${ZFSFILESYSTEM}@${YESTERDAY} 2>&1 | tee -a ${LOGFILE}
EL=${PIPESTATUS[0]}
else
# No snapshot option provided
echo I do not make a snapshot, rsync only | tee -a ${LOGFILE}
EL=0
fi
# Check zfs snapshot command errorlever
if [ $EL -eq 0 ]
then
# if zfs snapshot was OK
# call function do_rsync
# do_rsync SRV WHAT DEST
# SRV = remote machine
# WHAT = remote path we want to backup
# DEST = this is a directory inside the destination zfs mountpoint
do_rsync 192.168.100.100 /opt/tomcat/ tomcat
else
echo Error taking zfs snapshot $EL | tee -a ${LOGFILE}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment