#!/bin/bash # Written by Nick Sergeant to save his sanity. # http://nicksergeant.com # March 18th, 2008 # Prime temp email message, and grab timestamp. MESSAGE="/path/to/temp/emailmessage.txt" today=`date +%m-%d-%Y-%I-%M-%p` # Set array delimiter to newline. IFS=' ' # Define directories to perform 'svn status' on. SVNDIRS=( /path/to/dir/to/run/svnstatus /another/path/to/dir/to/run/svnstatus /some/other/path/to/dir/to/run/svnstatus ) # Define databases to dump. # Format: '-h[host] -u[user] -p[password] [database name]' # Note: [database name] will be used for filenames. MYSQLDBS=( '-hlocalhost -uuser1 -ppass1 dbname1' '-hlocalhost -uuser2 -ppass2 dbname2' '-hlocalhost -uuser3 -ppass2 dbname3' ) # Define directory where our SQL dumps should be placed. BACKUPDIR='/path/to/backup/db-backups' # Define FTP variables. FTPHOST=ftphost.com FTPUSER=ftpuser FTPPASS=ftppass # Define directory relative to FTP login root where you'd like to place the file. FTPDIR=path/to/dir/on/ftphost ########## BEGIN SVN STATUS ON DIRS ########## echo "Running SVN Status On Directories:" > $MESSAGE echo "Running SVN Status On Directories:" for DIR in ${SVNDIRS[@]} do cd $DIR pwd >> $MESSAGE svn status >> $MESSAGE done ########## BEGIN DUMP DATABASES ########## echo " Dumping DBs:" >> $MESSAGE echo " Dumping DBs:" for DB in ${MYSQLDBS[@]} do # We need to grab the DB name for usage in the filenames, so we turn the DB string into an array. # Start by setting the array delimiter to a space. IFS=' ' DBA=($DB) # Set dbname to index 3 of the DB array. dbname=${DBA[3]} # Create reference to the file we'll back up to. backup="$BACKUPDIR/db-backup-$dbname-$today.sql" # Use an if-else test to catch errors. if mysqldump $DB > $backup then echo "Successfully dumped $dbname" >> $MESSAGE else echo "Error: Could not dump $dbname" >> $MESSAGE fi done ########## BEGIN TAR DBS ########## echo " Tarring SQL Files:" >> $MESSAGE echo " Tarring SQL Files:" # Tar up all the SQL files in our directory. tar -cvzf $BACKUPDIR/db-backups-$today.tar.gz $BACKUPDIR/*.sql >> $MESSAGE # Display the file size of the new archive. echo `du -hs $BACKUPDIR/db-backups-$today.tar.gz` >> $MESSAGE echo `du -hs $BACKUPDIR/db-backups-$today.tar.gz` # Delete all individual SQL files, since they're now in the archive. rm -f $BACKUPDIR/*.sql >> $MESSAGE ########## BEGIN FTP TAR BACKUP ########## echo " Transferring db-backups-$today.tar.gz to $FTPHOST:" >> $MESSAGE echo " Transferring db-backups-$today.tar.gz to $FTPHOST:" FTPFILE=$BACKUPDIR/db-backups-$today.tar.gz if `ftp -in $FTPHOST << EOF quote USER $FTPUSER quote PASS $FTPPASS binary cd $FTPDIR put $FTPFILE quit EOF` then echo "Successfully uploaded db-backups-$today.tar.gz to $FTPHOST" >> $MESSAGE else echo "Error: Could not upload db-backups-$today.tar.gz to $FTPHOST" >> $MESSAGE fi ########## BEGIN EMAIL REPORT ########## SUBJECT="Server Status $today" EMAIL="email@domain.com" /bin/mail -s "$SUBJECT" "$EMAIL" < $MESSAGE echo "Sent Email" exit