Yeah, the title is pretty long. I really couldn't come up with anything better. Anyways, after the recent switch to Media Temple for all of my sites as a result of this debachle, I needed some peace of mind with regards to file changes and database backups for each of my sites. I also thought it'd be cool if I didn't have to do anything and would get an email every night of the 'status' of the server and the script's results.
So, I wrote a bash script. After writing it, I realized I probably could have / should have written the script in either Python or Perl, but learning some shell scripting was neat, too.
The script does four specific things to fit my four specific needs (I'm greedy):
So how does it work? Well, it's simple:
First, define a temp file we'll use for the email message we'll send out at the end of the script, and set a variable to the current date and time:
MESSAGE="/path/to/temp/emailmessage.txt" today=`date +%m-%d-%Y-%I-%M-%p`
Next, set the 'IFS' to a new-line character. We want to separate our list of SVN directories and MySQL connection strings in a line delimited format.
IFS=' '
Define the directories you'd like to run '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 )
Next, define the MySQL databases you'd like to dump:
MYSQLDBS=( '-hlocalhost -uuser1 -ppass1 dbname1' '-hlocalhost -uuser2 -ppass2 dbname2' '-hlocalhost -uuser3 -ppass2 dbname3' )
Now, define the directory where you want to put the database dumps, and eventually the archive of all of them:
BACKUPDIR='/path/to/backup/db-backups'
After we put each of the individual SQL dumps into an archive, we'll want to transfer the file to an external server for backup purposes. We'll use FTP, but you can use SCP if you'd like. For FTP, define your host, user, pass and directory:
FTPHOST=ftphost.com FTPUSER=ftpuser FTPPASS=ftppass FTPDIR=path/to/dir/on/ftphost
After the script has run, it will send off an email with the report of its successes or failures. Set your email preferences at the bottom of the script:
SUBJECT="Server Status $today" EMAIL="email@domain.com"
That's about it for setting up the script variables. The rest of the script essentially does what it's supposed to do, and is pretty self explanatory.
Ha, right.
As always, feel free to leave any questions or suggestions for improvement of the script.
Post new comment