Last active
December 19, 2020 04:11
-
-
Save devm33/9634018 to your computer and use it in GitHub Desktop.
Bash script to backup home directories using rsync
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Script to backup the home directory to an external hardrive mounted at /media/backup | |
# Uses rsync, dpkg, and mail (for error logging) | |
SRC='/home/*' | |
DEST='/media/backup' | |
RSYNC_OPTS='-haAXuv --delete' | |
EXCLUDE='*/.cache/* */.thumbnails/* */.config/google-* lost+found .gvfs' | |
EXCLUDE="$(echo $EXCLUDE | sed 's/\(\S\+\)/ --exclude \1/g')" | |
LOGFILE='/home/mehta/logs/backup' | |
TMPFILE='/tmp/backup_out' | |
ERRFILE='/tmp/backup_err' | |
ERRFLAG='ERROR' | |
EMAIL='[email protected]' | |
# clear out tmp files | |
[ -a $TMPFILE ] && rm -rf $TMPFILE | |
[ -a $ERRFILE ] && rm -rf $ERRFILE | |
touch $TMPFILE | |
touch $ERRFILE | |
# check if interactive | |
if [[ -t 0 || -p /dev/stdin ]]; | |
then | |
INTERACTIVE=1 | |
echo "note: rsync progress can be followed at $TMPFILE" | |
else | |
INTERACTIVE=0 | |
fi | |
# log handling for this script | |
function log { | |
# if passed params appends them to log file | |
# otherwise appends TMPFILE to log file | |
if [ $# -gt 0 ]; | |
then | |
LOG="$@" | |
else | |
LOG="$(cat $TMPFILE)" | |
fi | |
echo -e "$LOG" >> $LOGFILE | |
# also duplicates output to stdout if INTERACTIVE | |
if [ $INTERACTIVE ]; | |
then | |
echo -e "$LOG" | |
fi | |
} | |
# error handling for this script | |
function error_out { | |
# if passed args use those as error message | |
# otherwise assumes error message will be in ERRFILE | |
if [ $# -gt 0 ]; | |
then | |
ERRMSG="$@" | |
else | |
ERRMSG="$(cat $ERRFILE)" | |
fi | |
ERRMSG="There was an error backing up files on ganesh on $(date):\n${ERRMSG}" | |
echo -e "$ERRMSG" | mail -s "Backup Error" $EMAIL | |
log "$ERRMSG" | |
echo "$ERRFLAG" >> $LOGFILE # leave a flag for next time | |
exit 1 | |
} | |
# check if destination is a mount point otherwise quit | |
(mount | grep -q $DEST ) || error_out "Backup destination not mounted" | |
# keep an updated list of all installed packages | |
dpkg --get-selections | grep -v deinstall | cut -f 1 > $DEST/packages | |
# backup everything in /home with a few exceptions | |
rsync $RSYNC_OPTS $EXCLUDE $SRC $DEST > $TMPFILE 2> $ERRFILE || error_out | |
# send an email if last time there was an error | |
if [ "$ERRFLAG" == "$(tail -n 1 $LOGFILE)" ]; | |
then | |
echo "Backup successfully restarted on $(date)" | mail -s "Backup Success" $EMAIL | |
fi | |
# log success | |
log "$(tail -n 2 $TMPFILE)" # this is the summary from the end of rsync verbose output | |
log "Home directories backed up successfully on $(date)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment