Example script to backup the /etc and /u folder.
#!/bin/bash
# databackup.sh backup script
TIMECODE=`date +%Y%m%d`
LASTFULL_REFERENCE=/etc/LASTFULL_REFERENCE
TARPATH=/mnt/backup/tarfiles
MIRRORPATH=/mnt/backup/mirror
do_fail() {
# email the admin about the failure
echo "$1"
echo "$1" | mail -s "Backup Error Alert" backup.alert@example.com
logger -t databackup "$1"
umount /mnt/backup
exit 1
}
do_ok() {
# email the admin about the success
echo "$1"
echo "$1" | mail -s "Backup OK" backup.alert@example.com
logger -t databackup "$1"
umount /mnt/backup
exit 0
}
# sanity check: backup vol mounted?
[[ -d $TARPATH ]] || mount /mnt/backup
[[ -d $TARPATH ]] || do_fail "Backup volume not mounted!"
case "${1:-''}" in
'full')
# full backup
echo $TIMECODE > $LASTFULL_REFERENCE
tar zPcf $TARPATH/etc-full-$TIMECODE.tgz /etc ; r=$?
[[ "$r" = "2" ]] && do_fail "Full Backup failed for /etc"
tar zPcf $TARPATH/u-full-$TIMECODE.tgz /u ; r=$?
[[ "$r" = "2" ]] && do_fail "Full Backup failed for /u"
do_ok "Full Backup completed successfully."
;;
'diff')
# differential backup
[[ -f $LASTFULL_REFERENCE ]] || do_fail "LASTFULL_REFERENCE file not found!"
echo $TIMECODE > /etc/DIFF_DUMMY
find /etc -newer $LASTFULL_REFERENCE -type f -print | tar zPcf $TARPATH/etc-diff-$TIMECODE.tgz -T - ; r=$?
[[ "$r" = "2" ]] && do_fail "Diff Backup failed for /etc."
echo $TIMECODE > /u/DIFF_DUMMY
find /u -newer $LASTFULL_REFERENCE -type f -print | tar zPcf $TARPATH/u-diff-$TIMECODE.tgz -T - ; r=$?
[[ "$r" = "2" ]] && do_fail "Diff Backup failed for /u."
do_ok "Differential Backup completed successfully."
;;
'mirror')
# mirror using rsync, with IO bandwidth limit of 5MB/s :
rsync -ax --delete-after --bwlimit=5000 --stats /etc /u $MIRRORPATH/ ; r=$?
[[ ! "$r" = "0" ]] && do_fail "Mirror Backup failed!"
do_ok "Mirror Backup completed successfully."
;;
'purge')
# purge old tar files
/usr/sbin/tmpwatch -m $((24*14)) $TARPATH ; r=$?
[[ ! "$r" = "0" ]] && do_fail "Old backup files purge failed!"
do_ok "Purge of old backup files completed successfully."
;;
*)
echo "Usage: databackup.sh full|diff|mirror|purge"
exit 1
;;
esac
# full backup every sunday morning 6 0 * * 7 root /usr/local/bin/databackup.sh full > /var/log/databackup.log 2>&1 # diff backup other days 6 0 * * 1-6 root /usr/local/bin/databackup.sh diff > /var/log/databackup.log 2>&1 # mirror every day 6 22 * * * root /usr/local/bin/databackup.sh mirror > /var/log/databackup.log 2>&1 # purge old tar files everyday 4 0 * * * root /usr/local/bin/databackup.sh purge