Backup Drupal with Rsync

Backup Any Website

This tutorial will show you how to backup a drupal installation using rsync. This tutorial however can also be used for more than just drupal; the following script will allow you to backup your entire web directory and mysql databases to another server.

For this script I use rsync, a free utility that comes with most Unix based operating systems (Linux and Mac) that is used for backing up folders/files. Rsync is a great utility because it is command-line based, meaning it has no Graphical User Interface (GUI), and can easily be used inside a script, and easily automated using Cron.

I have another tutorial on rsync in the Mac OS X section that shows how to rsync files. Backing up a drupal (or any webserver) installation will use the same basic ideas as that tutorial.

The script (seen below) has many variables that the user must fill-in, to customize the script to their environment. The script also has an “–excludes” option, to allow the user to exclude certain files/folders from being backed up with rsync. This is helpful for me, because I exclude backing up the directory with my music which would take a long time.

This script can be run in Cron. Cron allows for the automation of scripts, and can be used to keep your server backed up regularly. To add this script to cron you must first run this command:

crontab -e

Which opens up the cron table for your user, and then add this line:

0 0 * * * /path/to/script/backupdrupal.sh > /path/to/log/file.log 2>&1

This will have the script run everyday at midnight automatically, and send all of the output to file.log.

Learn more about

The Code

#!/bin/bash
#
# backupdrupal.sh
# by Dave Eddy
# dave@daveeddy.com
# http://www.daveeddy.com
#

#### USER VARIABLES ####
PRIVATEKEY="/home/<USERNAME>/.ssh/id_rsa_autobackup" # Private key for use with SSH keys
SERVER_USER="username" # Username on remote server
SERVER_BACKUP_FOLDER="~/drupalbackup" # Folder on remote server
SERVER="fake.daveeddy.com" # Remote server hostname or IP
BASEURL="/var/www" # Web directory to backup / NO TRAILING SLASH (for rsyncs sake)

# this should be a user with SELECT privileges ONLY!
MYSQL_USER="" # MYSQL username
MYSQL_PASSWORD="" # MYSQL password

FILENAME="backup.sql" # Name of file to save MYSQL databases to
DUMP_DIR="/home/<USERNAME>/backup" # place to dump backup / NO TRAILING SLASH

# Start backing up
/bin/date
echo "rsyncing $BASEURL"
# rsync all the files in $BASEURL (your web directory) to a server using a private key
rsync -avr --delete -e "ssh -i $PRIVATEKEY" "$BASEURL" "$SERVER_USER"@"$SERVER":"$SERVER_BACKUP_FOLDER"
# dump all databases and write it to a file. the mysql password is in clear text so make sure this script is readable by you!!! 700 should work
mysqldump -u$MYSQL_USER -p$MYSQL_PASSWORD --all-databases > "$DUMP_DIR"/"$FILENAME"
# make the dump of mysql readable only by you
chmod 700 "$DUMP_DIR"/"$FILENAME"

# now rsync the database dump using the same SSH credentials
echo;echo;echo "rsyncing $FILENAME"
rsync -avr --delete -e "ssh -i $PRIVATEKEY" "$DUMP_DIR/$FILENAME" "$SERVER_USER"@"$SERVER":"$SERVER_BACKUP_FOLDER"

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>