How to Write A Mac/UNIX Shell Script to Backup Our Web Servers and MySQL Databases
===

I wrote myself a shell script to backup our web servers and got it working. It navigates to the backup directory, does an rsync, a mysqldump, commits it all to a git repository for versioning and pushes it to our remote git server. I've learned that there's lots of pitfalls to writing scheduled UNIX scripts, so I'm wondering if I'm missing anything or setting myself up for disaster. How would I have it email me on any error in the entire process?


    #!/bin/bash
    
    #Exit on any error
    set -e
    
    LOGFILE=~/backups/web_backup-$(date +"%m.%d.%Y_%H.%M.%S").log
    touch $LOGFILE
    echo "Backup started for "$(date +"%m.%d.%Y_%H.%M.%S") | tee -a $LOGFILE
    
    ############################################################
    ###### BACKUP WEBSITE
    ############################################################
    
    
    echo "Backing up Website..." 2>&1 | tee -a $LOGFILE
    cd ~/backups/example.org
    if [ "$?" != "0" ]; then
    	echo "Cannot change directory!" 1>&2
    	exit 1
    fi
    
    
    rsync -Pav --size-only --delete --filter='P .git' --filter='P example.com.sql' backups@example.com:/path/to/example.org/ ~/backups/example.org/ 2>&1 | tee -a $LOGFILE
    
    echo "Backing up Example.com DB..." 2>&1 | tee -a $LOGFILE
    
    ssh backups@example.com "mysqldump -u user -hlocalhost -ppassword dbname > example.com.sql 
    
    
    echo "Adding files to Git Repository..." 2>&1 | tee -a $LOGFILE 
    git add . 2>&1 | tee -a $LOGFILE| tee -a $LOGFILE
    
    echo "Commiting files to Git Repository..." 2>&1 | tee -a $LOGFILE 
    git commit -m "Backup as of "$(date +"%m.%d.%Y_%H.%M.%S") 2>&1 | tee -a $LOGFILE
    
    echo "Pusing Git Repo to Remote Location..." 2>&1 | tee -a $LOGFILE 
    git push origin master 2>&1 | tee -a $LOGFILE
    
    
    echo "Finished backing up Website..." 2>&1 | tee -a $LOGFILE
    
    echo "Backup Finished for "$(date +"%m.%d.%Y_%H.%M.%S")
    exit