Backing Up to Dropbox

Prerequisites

  • The instructions on this page presume a Dropbox synching user has been created using the Dropbox installation guide. These scripts will work under a different situation you just need to decide on a location to install the scripts and set the appropriate backup location in each scripts configuration.
  • Cron is required to automate the execution of the backup process. This seems to be installed by default now in Ubuntu 10.04 LTS.
sudo aptitude install cron
  • mutt is required to send emails with attachments, which is a configuratble option for some of these scripts.
sudo aptitude install mutt

Overview

The backup process involves the automated running of a series of scripts that create and manage backup files in the Dropbox linked folder. This means that all files will copied into the Dropbox account for permanent off-server storage.

The following scripts are available to use:

Apache: Backs up the apache configuration folder to daily compressed files. Files are re-cycled every week.

MySQL: Backups the MySQL Databases. Daily backups are rotated weekly. It also maintains a rotating 5 weekly backup and unlimited monthly backups, i.e. you have to manually remove unwanted monthly backups.

Subversion: This script will dump all the avaliable SVN repositories in a given directory to seperate tar.gz files which rotate every N days.

Dynamic Site Content: This script can backup user defined folders/files from any website using a simple backup configuration file. It was written to allow the backup of dynamically content that a website generates, i.e. user uploaded images.

All of the scripts have the ability to email the results to a single email address. The Apache, MySQL and Dynamic Site Content have additional options for emailing files or only emailing if an error has occurred.

As mentioned previously, the instructions here install the scripts into a folder in the home directory of the user who is running dropbox. If you are also using this approach and the scripts folder doesn't exist already then you need to create it.

sudo mkdir /home/username/scripts

Apache Configuration Backup Script

Installation

Grab the script from:

wget -O apache-config-backup.sh http://bateman.pixeldust.co.uk/server-setup/apache-config-backup.sh

Move the script to the /usr/local/bin directory. We also want to change the owner to root and set the permissions so that only the owner has access to the file.

sudo chown root.root apache-config-backup.sh 
sudo chmod 700 apache-config-backup.sh
sudo mv apache-config-backup.sh /home/dropbox/scripts/

Configuration

Set the basic variables according to you setup. The key ones are HTTPDIR and BACKUPDIR. Set the BACKUPDIR value to point to a folder inside your linked Dropbox folder (typically inside the dropbox users home folder), For example:

BACKUPDIR = /home/dropbox/Dropbox/Linode/Apache

MySQL Backup Script

Installation

Grab the script from:

wget -O automysqlbackup.sh http://bateman.pixeldust.co.uk/server-setup/automysqlbackup.sh

Move the script to the /usr/local/bin directory. We also want to change the owner to root and set the permissions so that only the owner has access to the file.

sudo chown root.root automysqlbackup.sh 
sudo chmod 700 automysqlbackup.sh
sudo mv automysqlbackup.sh /home/dropbox/scripts/

Configuration

Firstly, create a MySQL user to access the databases on behalf of the backup script. You can do this from your favourite desktop app or from the command line as follows:

mysql -u root -p mysql

Then from the mysql prompt,

mysql> CREATE USER 'backup'@'localhost' IDENTIFIED BY 'password here';
mysql> GRANT SELECT,SHOW DATABASES,LOCK TABLES ON *.* TO 'backup'@'localhost';
mysql> FLUSH PRIVILEGES;
mysql> QUIT:

The backup script is pretty flexible and has a lot of options for how it works. It's best to read through the help comments that are situated about half way down the script. Some of the key variables that you will probably need to set are:

USERNAME
PASSWORD
BACKUPDIR
MAILCONTENT
MAILADDR

Subversion Backup Script

Installation

Grab the script from:

wget -O svn-repos-backup.pl http://bateman.pixeldust.co.uk/server-setup/svn-repos-backup.pl

Move the script to the /usr/local/bin directory. We also want to change the owner to root and set the permissions so that only the owner has access to the file.

sudo chown root.root svn-repos-backup.pl
sudo chmod 700 svn-repos-backup.pl
sudo mv svn-repos-backup.pl /home/dropbox/scripts/

Configuration

Dynamic Site Content Backup Script

Installation

Grab the script from:

wget -O site-content-backup.sh http://bateman.pixeldust.co.uk/server-setup/site-content-backup.sh

Move the script to the /usr/local/bin directory. We also want to change the owner to root and set the permissions so that only the owner has access to the file.

sudo chown root.root site-content-backup 
sudo chmod 700 site-content-backup
sudo mv site-content-backup /home/dropbox/scripts/

Configuration

Testing Scripts

Each script can be tested by simply calling it from the command line with sudo privileges.

sudo /home/username/scripts/name-of-script

You can then check the Dropbox folder to see that the files are created as expected.

Automating Scripts

To review the current contents of root's crontab use:

sudo crontab -l

If the crontab is not empty then dump it into a temporary file we can edit

sudo crontab -l > tmp

Otherwise, simple create an empty file

Use your favourite text edit to edit this temporary file.

nano tmp

Paste in the following lines to the end of the file:

# Run the Subversion backup daily at 10.00pm
0 22 * * * /home/dropbox/scripts/svn-repos-backup.pl >> /dev/null 2>&1

# Run the MySQL backup script daily at 10.05pm
5 22 * * * /home/dropbox/scripts/automysqlbackup.sh >> /dev/null 2>&1

# Run the Apache configuration backup script daily at 10.10pm
10 22 * * * /home/dropbox/scripts/apache-config-backup.sh >> /dev/null 2>&1

# Run the Dynamic site content backup script daily at 10.15pm
15 22 * * *  /home/dropbox/scripts/site-content-backup.sh >> /dev/null 2>&1

Comment out the lines (backups) that you do not need to run by simply placing a hash (#) at the beginning of the line.

Now add the contents of this tmp file back to the crontab for root

sudo crontab tmp

You can also change the times at which these scripts are executed. There are many online resources that explain how to configure cron. Here is a link to one of them https://help.ubuntu.com/community/CronHowto

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License