lamp-backup :: Backing up LAMP Server Content
It’s been a while since I posted anything useful, but hopefully this will make up for it.
A friend came to me asking for help configuring the backup of his remote LAMP server and I duly obliged, but ended up getting a little carried away. Some hours later I emerged with this bash script to do the job properly.
Called from cron, each time the script runs:
- It creates/updates a local rsync-copy of a remote directory.
- It runs a mysqldump to a file, named by date-stamp.
- Any archives or log files older than the user-specified expiry time (days) are deleted.
- All three operations are carefully logged to appropriate “.log” and “.err” files.
- The mysqldump, the rsync-copy and all the logs are run through tar.
- The tarball is run through a user-specified compression program (default: xz).
- The final archive is named by date-stamp.
- A summary file is created including highlighted errors and execution times for each step.
- The summary.log is (optionally) e-mailed to the backup administrator.
- The script cleans up after itself, deleting any superfluous log and mysqldump files.
In the end you have a configurable period of snapshotted-history for your LAMP server, all in small archive files, named by their date and with logs for every step of every backup and permanently-deleted file.
I recommend the script is called by cron like this (assuming midnight backup):
Which will also give you activity and error logs for the script itself.
If you wish to run the script manually from the command line to test it, you can set the output to verbose like this:
Ensure that lamp-backup.conf lives in the same directory as lamp-backup.sh and is readable and carefully configured.
DO NOT RUN THIS SCRIPT AS ROOT UNLESS YOU REALLY NEED TO AND HAVE TESTED YOUR CONFIGURATION AS A NORMAL USER.
You can get a copy of v1.0 (stable) of this script and associated config file from my subversion repository:
I will be working on it more soon, abstracting further, adding command line parameters and adding additional features that seem useful.
You can track the current version via the repository trunk.