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):
- Run 'svn status' on multiple checked-out projects from my SVN repository to see if there have been any local modifications to the file structure (such as a user uploading a file).
- Run 'mysqldump' on several databases my sites use.
- Transfer an archive of all of those MySQL dumps to an external server, for backup purposes.
- Email me the statuses of each of these processes.
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:
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.
Define the directories you'd like to run 'svn status' on:
Next, define the MySQL databases you'd like to dump:
Now, define the directory where you want to put the database dumps, and eventually the archive of all of them:
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:
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:
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.
As always, feel free to leave any questions or suggestions for improvement of the script.