A quick shell script for backing up databases, FTPing them to a remote server, and notifying me of any file changes.

Posted 9 months ago in Programming

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:

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.

Download the full script.

As always, feel free to leave any questions or suggestions for improvement of the script.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options