automysqlbackup.sh is a rock-solid Unix/Linux shell script that automatically makes daily backups of your MySQL database(s). It is available as an open source project on SourceForge, consisting of just one file that is easily configured and deployed — ideal for anyone running a PHP website or any other LAMP-stack site where the M stands for MySQL. The current version, 2.5, has been downloaded 50,000 times. I’ve been relying on it ever since version 1.9.
An Ideal Rotation Scheme: The backups are automatically saved in a rotation scheme that retains daily backups for the past seven days, plus weekly backups for the past month, plus monthly backups thereafter. So, even if there is a delay between the time a corruption occurs and it’s discovered, chances are you’ll be able to restore the pre-corruption data. The scheme is a nice compromise between the need for backup retention versus available disk space.
Backups as Insert Statements: automysqlbackup.sh utilizes mysqldump to perform the actual backup. In case you’re not familiar with mysqldump, the output is a SQL script (plain ASCII) that contains table create statements, insert statements, etc. — anything necessary to re-create and repopulate the database. Thus, restoring a backup is merely a matter of playing the script. This is nice for a number of reasons:
- Because the backup is plain ASCII, you can visually inspect it, to assure yourself that all of the data is indeed being saved. (As long as you have a text editor that can handle such a large file, for example TextPad, or Notepad++.)
- Also, because it is plain ASCII, you can do anything you need to in the way of tweaking it, pruning or splicing it.
- No special recovery software is needed, so there are no worries about having to rely on a third party to maintain/support it.
- Restoration can easily be redirected to any database on any server just by changing the “uses” statement at the top of the script. This means you can work with an old copy of the data independently from the ongoing live database.
Of course, the backups are compressed when they are saved (your choice of gzip or bzip2).
Easy Setup: automysqlbackup.sh is a single shell script. It is rather lengthy, but it can be easily customized merely by changing a few lines at the top. You do not need to be a shell script expert in order to use it. Once the configuration settings at the top have been set, installing the script is merely a matter of placing it in the cron.daily folder on your server (for a Linux box).
Numerous Options: automysqlbackup.sh is preprogrammed with all kinds of options, from defining which day of the week the weekly backup is retained, to defining the disposition of the backups and/or notifications. For example, normally the backups are saved to the local hard disk and an administrator is notified via e-mail that it was done; however, the backup itself could be e-mailed as an attachment. When backing up multiple databases, you can choose between using a single backup file for all of them, or a separate file for each.
Exemplary Shell Scripting: As I said, you don’t need to know Shell scripting in order to take advantage of automysqlbackup.sh. But, if you do want to learn how to write shell scripts (better), then automysqlbackup.sh serves as a great example. Just to name a few of the concepts touched upon…
- Conditional execution based on whether or not a directory exists.
- Sending out an e-mail from a shell script
- Using variables as configuration settings.
- Logging status to a file.
- Compressing files with gzip or bzip2.
- Repeating an operation multiple times according to a list (in this case, a space-separated list of database names).
- Constructing filenames that include the current date and time.
- Sending a status code back to the operating system when done to signal whether or not the job was successful.
automysqlbackup.sh
Latest Version: 2.5 (14-Feb-2006)
Platform: Any OS that runs BASH (e.g. Linux, or via Cygwin on Windows).
Author: Harley “wipe_out”
Price: Free (FOSS)
Download Link: sourceforge.net/projects/automysqlbackup
Related articles:
- Rotating Backups
- Quick Link: SQuirreL, an Agnostic SQL Client
- Top 5 Ways to Get the Most Out of Any Software (mentions SQLYog, a tool for managing MySQL databases)
- Software Review: TextPad 5 (a text editor that can handle huge files)
- Quick Link: Notepad++ 4.1.2 (Windows) (a text editor that can handle huge files)