public
Fork of postpostmodern/web-server-backup
Description: A bash script for backing up MySQL databases and web site files (for cron)
Homepage:
Clone URL: git://github.com/trey/web-server-backup.git
name age message
file .gitignore Sat Oct 04 21:28:27 -0700 2008 Initial commit [postpostmodern]
file README.markdown Sat Oct 04 21:53:50 -0700 2008 Changed default backup dir. Can't use ~ for home. [postpostmodern]
file backup.sh Mon Apr 20 14:56:39 -0700 2009 Merged branch 's3sync' into master. Conflicts:... [trey]
README.markdown

Web Server Backup Script

This is a bash script for backing up multiple web sites and MySQL databases into a specified backups directory. It's a good idea to run it every night via cron.

Once configured (variables set within the script), it does this:

  • Creates a directory for your site backups (if it doesn't exist)
  • Creates a directory for your MySQL dumps (if it doesn't exist)
  • Loops through all of your MySQL databases and dumps each one of them to a gzipped file
  • Deletes database dumps older than a specified number of days from the backup directory
  • Tars and gzips each folder within your sites directory (I keep my websites in /var/www/sites/)
  • Deletes site archives older than a specified number of days from the backup directory
  • Optionally rsyncs all backup files to a remote server

BETA WARNING

This script works fine for me (using Ubuntu 8.04 on Slicehost), but servers vary greatly. USE THIS SCRIPT AT YOUR OWN RISK!! There is always risk involved with running a script. I AM NOT RESPONSIBLE FOR DAMAGE CAUSED BY THIS SCRIPT.

You may very well know more about bash scripting and archiving than I do. If you find any flaws with this script or have any recommendations as to how this script can be improved, please fork it and send me a pull request.

Installation

  • MOST IMPORTANTLY: Open the backup.sh file in a text editor and set the configuration variables at the top (see below).
  • Place the backup.sh file somewhere on your server (something like /usr/local/web-server-backup or ~/scripts).
  • Make sure the backup.sh script is executable by you: chmod 744 backup.sh
  • Optionally, set up an account on another server and configure it to connect using an SSH key.
  • Preferably set up cron to run it every night (see below).

Configuration

There are a bunch of variables that you can set to customize the way the script works. Some of them must be set before running the script!

NOTE: The BACKUP_DIR setting is preset to ~/site-backups. If you want to use something like /var/site-backups, you'll need to create the directory first and set it to be writable by you.

General Settings:

  • BACKUP_DIR: The parent directory in which the backups will be placed. It's preset to: "/home/whoami/site-backups"
  • KEEP_MYSQL: How many days worth of mysql dumps to keep. It's preset to: "14"
  • KEEP_SITES: How many days worth of site tarballs to keep. It's preset to: "2"
  • THE_DATE: The date that will be appended to filenames. It's preset to: "$(date '+%Y-%m-%d')"

MySQL Settings:

  • MYSQL_HOST: The MySQL hostname. It's preset to the standard: "localhost"
  • MYSQL_USER: The MySQL username. It's preset to the standard: "root"
  • MYSQL_PASS: The MySQL password. You'll need to set this yourself!
  • MYSQL_BACKUP_DIR: The directory in which the dumps will be placed. It's preset to: "$BACKUP\_DIR/mysql/"

Web Site Settings:

  • SITES_DIR: This is the directory where you keep all of your web sites. It's preset to: "/var/www/sites/"
  • SITES_BACKUP_DIR: The directory in which the archived site files will be placed. It's preset to: "$BACKUP_DIR/sites/"

Rsync Settings:

  • RSYNC: Whether or not you want to rsync the backups to another server. (Either "true" or "false") It's preset to: "true"
  • RSYNC_USER: The user account name on the remote server. Please note that there is no password setting. It is recommended that you use an SSH key. You'll need to set this yourself!
  • RSYNC_SERVER: The server address of the remote server. You'll need to set this yourself! It's preset to: "other.server.com"
  • RSYNC_DIR: The directory on the remote server that will be synchronized with $BACKUP_DIR. It's preset to: "web_site_backups"

Paths to commands: (probably won't need to change these)

  • MYSQL_PATH: Path to mysql. It's preset to: "$(which mysql)"
  • MYSQLDUMP_PATH: Path to mysqldump. It's preset to: "$(which mysqldump)"
  • FIND_PATH: Path to find. It's preset to: "$(which find)"
  • TAR_PATH: Path to tar. It's preset to: "$(which tar)"
  • RSYNC_PATH: Path to rsync. It's preset to: "$(which rsync)"

Running with cron

Once you've tested the script, I recommend setting it up to be run every night with cron. Here's a sample cron config:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin
MAILTO=jason@example.com
HOME=/home/jason

30 4 * * * jason /home/jason/scripts/backup.sh

Assuming you are me (user account is 'jason'), that'll run the script (located in ~/scripts) at 4:30 every morning and email the output to jason@example.com.

So, take the above example, change the user account name, email address, home path, etc., save it to a text file, and place it in /etc/cron.d/. That should do it.