#!/bin/bash ##jsbk.sh 12-07-11 ##email: jason [at] schaeferconsulting [dot] com ##505.570.1114 ########################## USER EDIT BEGINS ############################# ## Email to, comma separated list for multiple recipients. host needs to be able to relay local mail. postfix recommended. to="" ## Set "yes" to rely on gnome volume manager (/media/[drivename]). Leave blank for manual mount (requires root priv. and will mount to /mnt) automount="yes" ## Name of drive. If you have multiple backup drives, they should be named the same. drivename="mybackupdrive" ## Name of directory to write backups in. Leave blank to write to the root of backup drive. bkdir="mybackups" ## If you are in need of assistance, please contact [the following string] support="Schaefer Consulting at 505.570.1114" ## To bypass the backup commands, for debug purposes. yes/no bypassbk="no" backup_commands () { ########################## START BACKUP COMMANDS ######################## echo "Running backup commands now..." >> $log 2>&1 ## Enter backup line's here i.e., cp, ssh, rsync #### ## append "||check" to the end of the command to verify if the command ran successfully ## exmpl: /bin/tar zcvf $bklocation/`date +%F`.tar.gz --exclude media/* /home ||check 2>> $log ## exmpl: /bin/cp -rvu /home/ $bklocation/new-n-changed ||check 2>> $log ## exmpl: /usr/bin/rsync -av --delete --stats --exclude media/* /home/ $bklocation/rsync-mirror ||check 2>> $log ## exmpl: /usr/bin/pg_dump -Z 9 -p 5432 -h hostname -U username dbname > $bklocation/dbname_`date +%F_%T`.sqldump.gz ||check 2>> $log ## postgres auth uses ~/.pgpass containing localhost:5432:db:dbuser:dbpass ## BACKUP over SMB ## exmpl: cd $bklocation/backup-destination && /usr/bin/smbget -URq smb://user:password@hostname/share 2>> $log ########################### END BACKUP COMMANDS ######################### ########################### USER EDIT ENDS ############################## ########################### CRON REFERENCE ############################## ## setup to run on a schedule with cron ## minutes (0-59) | hours (0-23) | day of month (1-31) | month (1-12) | day of week (0-6 or Sun-Sat 0,6) | command ## 0 2 * * * /usr/local/bin/jsbk.sh ## will run at 2:00am every day ## 0 2 1,15 * * /usr/local/bin/jsbk.sh ## will run at 2:00am 1st and 15th of the month } #if you want to see the log on your screen, use log="/dev/stdout" instead log="/tmp/jsbk.backup.log" #log="/dev/stdout" subject="Backup Log from `hostname`" #mountpoint, for easy reference later if [ "$automount" = "yes" ]; then mp="/media/$drivename" else mp="/mnt" fi #mount point plus backup dir, for easy reference in backup commands bklocation="$mp/$bkdir" starttime=`date +%s` echo $bklocation if [ -d "$bklocation" ];then echo bklocation=\"$bklocation\" exists, moving on else echo bklocation=\"$bklocation\" is not a directory. stripping bkdir, will write files root of $mp bkdir="" fi trap caughtsig 1 2 3 6 caughtsig () { echo "******************** Caught INTERUPT SIG 1,2,3, or 6. **********************" >> $log 2>&1 exit 1 } #check if backup is still running or exited without cleanup. if [ -f $log ]; then echo "log ($log) exists, this indicates that $0 may be running or exited uncleanly. verify with ps aux |grep $0 and if not running, delete $log." >> $log 2>&1 error emailend fi check () { echo "ERROR: A command FAILED!" >> $log 2>&1 failed="failed" } error () { echo >> $log 2>&1 echo -e "\t\t * Backup is NOT working!" >> $log 2>&1 echo -e "\t\t * Contact technician for service." >> $log 2>&1 echo -e "\t\t * Keep this message for diagnoses" >> $log 2>&1 echo -e "\t\t * Backup Failed on `date +%c`" >> $log 2>&1 echo -e "\t Diagnoses:" >> $log 2>&1 #mount >> $log 2>&1 echo >> $log 2>&1 df -h >> $log 2>&1 echo >> $log 2>&1 echo Mount Point: $mp >> $log 2>&1 } emailend () { echo >> $log 2>&1 #calculate running time of backup endtime=`date +%s` elapsed=$(($endtime - $starttime)) printf "Running time: "%dh:%dm:%ds"\n" $(($elapsed/3600)) $(($elapsed%3600/60)) $(($elapsed%60)) >> $log 2>&1 echo "---" >> $log 2>&1 echo "If you are in need of assistance, please contact $support" >> $log 2>&1 echo >> $log 2>&1 if [ -z "$automount" ]; then umount $mp >> $log 2>&1 || { echo -e "Error: Removing hard drive failed\n";} >> $log 2>&1 fi echo "Emailing logs..." >> $log 2>&1 if [ ! -z "$to" ];then /bin/cat $log | /usr/bin/mail -v -s "$subject" $to else /bin/mv $log /tmp/jsbk.last.log fi exit } [ "$automount" != yes ] && [ ! -z "$automount" ] && echo ERROR: automount=\"$automount\" is not valid. >> $log 2>&1 && error && emailend [ -z "$drivename" ] && echo ERROR: drivename is not declared >> $log 2>&1 && error && emailend [ -z "$bkdir" ] && echo fyi, bkdir is not declared [ -z "$support" ] && echo fyi, support string is empty #begin echo "BACKUP LOG" >> $log 2>&1 echo "server: `hostname`" >> $log 2>&1 echo -e "Backup BEGAN on `date +%c` \n" >> $log 2>&1 if [ ! -d "$mp" ];then echo "Mount point $mp does not exist!" >> $log 2>&1 if [ "$automount" = "yes" ]; then echo "be sure drivename=\"$drivename\" is correct and gnome volume mounting is working. Or switch to automount=\"no\"" >> $log 2>&1; fi if [ "$automount" = "no" ]; then echo "check that $mp exists" >> $log 2>&1; fi echo "Also, be sure the drive is plugged in or turned on." >> $log 2>&1 error emailend fi if [ "$automount" = "no" ]; then #mount drive manually at /mnt mount -L $drivename $mp >> $log 2>&1 || { echo "ERROR: mounting hard drive failed. Be sure drivename=\"\" is correct. If drive is already mounted this error can be ignored. Also, be sure the drive is plugged in or turned on.";} >> $log 2>&1 fi echo -e "\n +++ START LOG +++ \n" >> $log 2>&1 if [ "$bypassbk" = "yes" ]; then touch $bklocation/bypassbk.test echo "Bypassing all backup commands for debug purposes" >> $log 2>&1 emailend else #user backup commands function backup_commands fi echo -e "\n +++ END LOG +++ \n" >> $log 2>&1 #check to see if $failed equals zero, if it is then all commands were successfull. If not, something failed. if [ "$failed" = failed ]; then echo "ERROR: A problem was detected!" >> $log 2>&1 error emailend else echo -e "\t SUCCESS: Backup Complete!" >> $log 2>&1 echo -e "\t\t " >> $log 2>&1 emailend fi