#!/bin/bash
# creates a backup of the mysql & webdata for a specific website.
TODAY=`date +%A`
# these variables cannot contain any spaces and must be modified based on:
# the name of the /automation/folder/ of the application being backed up (application)
# on what server (localhost)
# where the backed up data should be replicated (remotehost)
# the location of the www data for the specific application (webdata)
# /var/www/somefolder
# the username, password and database required to establish the mysql connection (sqluser, sqlpass, sqldb)
# SQLPASS must contain the -p prefix
# example: -pS0m3PassW0rd
APPLICATION=someapp
LOCALHOST=thisserver
REMOTEHOST=otherserver
WEBDATA=location
SQLUSER=usernam
SQLPASS=-ppassword
SQLDB=dbname
# these variables are specific to the application being backed up.
ARCHIVE=/automation/$APPLICATION/$LOCALHOST
ERRORLOG=/automation/$APPLICATION/errorlog.txt
SQLFILE=/automation/$APPLICATION/$APPLICATION.sql
WEBFILE=/automation/$APPLICATION/$APPLICATION.web.tar
SQLGZ=/automation/$APPLICATION/$APPLICATION.sql.gz
WEBGZ=/automation/$APPLICATION/$APPLICATION.web.tar.gz
SQLA=/automation/$APPLICATION/$LOCALHOST/$APPLICATION.sql.gz
WEBA=/automation/$APPLICATION/$LOCALHOST/$APPLICATION.web.tar.gz
rm $SQLFILE
rm $WEBFILE
rm $SQLGZ
rm $WEBGZ
echo "Backup started: " `date` >> $ERRORLOG
mysqldump -u $SQLUSER $SQLPASS $SQLDB > $SQLFILE
tar -cf $WEBFILE $WEBDATA
if [ -s $SQLFILE ];
then
if [ -s $WEBFILE ];
then
gzip $SQLFILE
gzip $WEBFILE
else
echo $SQLFILE was created, $WEBFILE was not, script terminating. >> $ERRORLOG
exit
fi
else
echo $SQLFILE was not created, script terminating. >> $ERRORLOG
exit
fi
if [ -s $SQLGZ ];
then
if [ -s $WEBGZ ];
then
rm $ARCHIVE/*.$TODAY
mv $SQLGZ $SQLA.$TODAY
mv $WEBGZ $WEBA.$TODAY
else
echo $SQLGZ was created, $WEBGZ was not, script terminating. >> $ERRORLOG
exit
fi
else
echo $SQLGZ was not created, script terminating. >> $ERRORLOG
exit
fi
scp $SQLA.$TODAY username@$REMOTEHOST.domain.com:/automation/$APPLICATION/$LOCALHOST
scp $WEBA.$TODAY edcns5al@$REMOTEHOST.domain.com:/automation/$APPLICATION/$LOCALHOST
chown -R root:automation /automation/
chmod -R 770 /automation/
echo Backup completed: `date` >> $ERRORLOG