Backup for a personal website: Hello BackupMonitor !

In the previous post I explained why BackupMonitor has become obsolete on my Ubuntu desktop where DejaDup solves the backup problem.

However there's still one backup problem that I hadn't automatized yet: backing up my website !

You'll tell me that this kind of backup can certainly be handled (usually with an additional fee) by my hosting provider, and that is actually quite right ! However, I still fancy the idea to have my data at home - physically - and doing that doesn't exclude other backup solutions anyway (the more, the better).

So I'm going to explain how I set up this kind of backup.

So the main idea here is to throw away almost everything that was at the heart of BackupMonitor and just keep the following trivial features:

  • timely notifications of a need for backup
  • easy registration of custom scripts
  • a simplistic GUI with a big button to start launching all those scripts in a row, and reporting success or errors for each script
The custom scripts to be registered as backup "items" can actually be very simple such as the one provided below, that makes it possible to copy files from a distant server to the convention "backup" directory as defined by BackupMonitor.
#!/bin/bash

# Backup data from a remote server
# where backups are stored 
# in a directory indicated 
# by $HOME/backups/Latest
#
# WARNING: requires that you set a 
# ssh key allowing to access your remote
# server without a password !
#
# Author: Thibauld Nion
# License: Public Domain

# A name that will identify the data once backed up
BKP_NAME=example.com

# Server access configuration
USERNAME=myusername
SERVER_NETLOC=server.example.com

# Direcotry where stuff will be downloaded
BACKUP_DOWNLOAD_DIR=$BACKUP_MONITOR_DESTINATION_FOLDER/$BKP_NAME

if [ -d  $BACKUP_DOWNLOAD_DIR ]
then
echo "Removing previously created download directory $BACKUP_DOWNLOAD_DIR"
rm -r $BACKUP_DOWNLOAD_DIR
fi

echo "Preparing to download remote backup from $SERVER_NETLOC to $BACKUP_DOWNLOAD_DIR"
scp -rq $USERNAME@$SERVER_NETLOC:/home/$USERNAME/backups/Latest $BACKUP_DOWNLOAD_DIR
echo "Download finished"

This script is simple but has two requirements:

  1. that a ssh-key (and also potentially a ssh-agent) is used to have access to the website's hosting server without having to type the password
  2. that, on the server side, a script regularly makes a backup of the website's files and databases and stores all the backed up data in a folder named $HOME/backups/Latest
In my case, to fulfill the second requirement I adapted a script fromDockStreet media that did a good job in backing up a mysql db and a website's folder as they are set-up on Dreamhost.

A "template" of my adapted script is show below and my main modifications are as follow:

  • handle several folders (not just a single one)
  • handle several databases
  • create and update a symbolic link "$HOME/backups/Latest" pointing to the latest folder where the latest backups actually are
#!/bin/bash

# Site backup
# Adapted from http://www.dockstreetmedia.com/2011/03/automatic-site-backups-using-cron-jobs-in-dreamhost/

# specific config variables (EDIT THESE)
#
# Space separated list of the names of folders 
# below your home directory, that you want to backup
DIRNAMES="topFolderA topFolderB topFolderC"
# host for your site's database(s)
DBHOST=mysql.example.com
# user name for this mysql server
DBUSER=myusername
# corresponding password (the same you gave 
# to the applications powering your site (like wordpress etc)
DBPASS=xxx
# Space separated list of the names of dbs 
# within DBHOST that you want to backup
DBNAMES="sitedb_one sitedb_two sitedb_three"

# other config variables(DO NOT EDIT THESE)
NOWDATE=$(date +"%y%m%d")
NOWDAY=$(date +"%d")
BACKUPDIR=backups
MYSQLDUMP="$(which mysqldump)"
LOGFILE=$HOME/$BACKUPDIR/log.txt

TARGETPATH=$HOME/$BACKUPDIR/$NOWDAY
LINK_TO_LATEST=$HOME/$BACKUPDIR/Latest

# check to see if target path exists – if so, delete the old one and create a new one, otherwise just create it
if [ -d $TARGETPATH ]
then
# print a message for the logfile / output email
echo "$NOWDATE - Removing previous backup $TARGETPATH" | tee -a $LOGFILE
rm -r $TARGETPATH
mkdir -p $TARGETPATH
else
mkdir -p $TARGETPATH
fi

for SITEDIR in $DIRNAMES
do

# create a GZIP of the directory inside the target path
tar -zcf $TARGETPATH/${SITEDIR}_$NOWDATE.tar.gz $HOME/$SITEDIR
# print a message for the logfile / output email
echo "$NOWDATE - $SITEDIR has been backed up" | tee -a $LOGFILE

done

for DBNAME in $DBNAMES
do
# dump the data into a SQL file inside the target path
$MYSQLDUMP -u $DBUSER -h $DBHOST -p$DBPASS $DBNAME | gzip > $TARGETPATH/${DBNAME}_$NOWDATE.sql.gz
# print a message for the logfile / output email
echo "$NOWDATE - $DBNAME has been backed up" | tee -a $LOGFILE
done

# Put a link to the latest backup
if [ -e $LINK_TO_LATEST ]
then
rm $LINK_TO_LATEST
fi

ln -s $TARGETPATH $LINK_TO_LATEST

The following package backup_monitor_servers.tar.gz gather both scripts as well as the custom configuration file of BackupMonitor that must be put in ~/.config/backup-monitor/.

Of course, I could still use BackupMonitor for both kinds of backups (fetching remote backups and backing up local folders). That would be a little more elegant and I'm not pretending that it's a lot of fun to have two tools doing basically the same stuff.

However I'm definitely hoping that a polished tool will surface (or maybe just new functionalities to DejaDup) to help doing all those backups and to let me stop maintaining the couple of scripts that make BackupMonitor.

Until then, the same trick (but with different scripts) should make it possible to backup personal data from other sources like social networks (Delicious, Google, Facebook, LastFM etc).