Debian daily backup domains with tartarus.

In a previous article we saw how to set up a backup process for tartarus, in this article we will see how we can generate backups of the data in each vhost with tartarus, the reason I do this is so we can set up a daily incremental backup of the files for each domain.

Make sure you read the article Debian Backup with Tartarus, this will prepare you for the basics of this article.

Now lets create some files that we can use for this:

/etc/tartarus/generic.inc

STORAGE_METHOD="FILE"
STORAGE_SERVER="example.com"
STORAGE_USER="user"
STORAGE_DIR="backup"
STORAGE_FILE_DIR="/mnt/$STORAGE_SERVER/$STORAGE_DIR/"
WEEK_NUMBER=`date +%V`

LIMIT_DISK_IO="yes"

FILE_LIST_CREATION="yes"
FILE_LIST_DIRECTORY="/mnt/$STORAGE_SERVER/$STORAGE_DIR/"

COMPRESSION_METHOD="bzip2"

STAY_IN_FILESYSTEM="yes"

TAR_OPTIONS="--preserve-permissions"

ENCRYPT_SYMMETRICALLY="no"
ENCRYPT_PASSPHRASE_FILE="/etc/tartarus/backup.sec"

TARTARUS_PRE_PROCESS_HOOK() {

  if [ ! -d "/mnt/$STORAGE_SERVER" ]; then
    mkdir -p "/mnt/$STORAGE_SERVER"
  fi
  
  umount "/mnt/$STORAGE_SERVER"
  sshfs $STORAGE_USER@$STORAGE_SERVER:/ /mnt/$STORAGE_SERVER -o reconnect

  if [ ! -d "$STORAGE_FILE_DIR" ]; then
     mkdir -p "$STORAGE_FILE_DIR"
  fi

  if [ ! -d "$FILE_LIST_DIRECTORY" ]; then
     mkdir -p "$FILE_LIST_DIRECTORY"
  fi

}

TARTARUS_PRE_CLEANUP_HOOK() {
  echo "Cleaning ..." | logger -i
}

TARTARUS_POST_CLEANUP_HOOK() {
   umount "/mnt/$STORAGE_SERVER"
}

TARTARUS_DEBUG_HOOK() {
    echo $DEBUGMSG | logger -i
}

TARTARUS_PRE_FREEZE_HOOK() {
  sync
}

We need to supply the proper information for STORAGE_USER and STORAGE_SERVER in the bash script above.

/etc/tartarus/vhost.template

source /etc/tartarus/generic.inc
NAME="%NAME%"
DIRECTORY="%DIRECTORY%"
EXCLUDE=""
CREATE_LVM_SNAPSHOT="no"
INCREMENTAL_STACKING="yes"
INCREMENTAL_TIMESTAMP_FILE="/var/spool/tartarus/timestamps/%NAME%"
STORAGE_FILE_DIR="$STORAGE_FILE_DIR/vhosts/%NAME%/archives/${WEEK_NUMBER}"
FILE_LIST_DIRECTORY="$FILE_LIST_DIRECTORY/vhosts/%NAME%/lists"

/etc/tartarus/create.sh

#!/bin/bash
items=`find /var/www/vhosts/ -maxdepth 1 -group psaserv`

for itm in $items; do
  basefile=$(basename $itm)
  name=$basefile
  dir=$itm
  ./create_vhost.sh "$name" "$dir"
done

/etc/tartarus/create_vhost.sh

#!/bin/bash

if [ "$#" -ne 2 ]
then
  echo "Usage: ./create_vhost.sh <name> <directory>"
  exit 1
fi

NAME=$1
DIRECTORY=$2

sed -e "s:%DIRECTORY%:$DIRECTORY:g" -e "s:%NAME%:$NAME:g" vhost.template > "vhost_${NAME}.conf"

And there you go, now calling the script /etc/tartarus/create.sh will create all the required files for the hosts, you will need to re-run this whenever you add new domains, so you can also run this periodically, once a week, for the first full backup.