Shared Server Backups

From TextUsers

Jump to: navigation, search

Rotated Backups

You can use something like this (loosely based on KB 113)to automate MySQL and web/public/ backups. You should chmod 700 the script.

#!/usr/local/bin/bash

# This file will run a backup of your desired MySQL database and website and
# remove any backups older than 7 days.
#
# If you'd like to preserve backups for longer than a week, like say
# 2 weeks, then change the 'MTIME' variable from '7' to '14'.
#
# NOTE: Make sure to create a 'backups' folder in the root of your
# account and replace USERNAME, PASSWORD, and DATABASENAME with
# the appropriate values.

# Script variables
MTIME=7
HOME="/users/home/USERNAME"
BACKUPDIR="$HOME/backups"
DOMAINSDIR="$HOME"
DATE=`date "+%Y-%m-%d"`
TAR="/usr/local/bin/gtar"
MYSQLDUMP="/usr/local/bin/mysqldump"

if [ ! -x $TAR ]; then
  echo "$TAR command not found"
  exit 1
fi

if [ ! -x $MYSQLDUMP ]; then
  echo "$MYSQLDUMP command not found"
  exit 1
fi

## YOURDOMAIN backup
NAME=YOURDOMAIN
DOMAIN=YOURDOMAIN.TLD
USERNAME=YOURMYSQLUSERNAME
PASSWORD=YOURMYSQLPASSWORD
DATABASENAME=YOURMYSQLDBNAME
SQLDUMPFILENAME=${NAME}_drupal_db_${DATE}.gz
TARFILENAME=${NAME}_public_${DATE}.tar.gz

$MYSQLDUMP --opt --skip-lock-tables --user=$USERNAME --password=$PASSWORD $DATABASENAME | gzip > ${BACKUPDIR}/${SQLDUMPFILENAME}
$TAR -zcf ${BACKUPDIR}/${TARFILENAME} ${DOMAINSDIR}/${DOMAIN}/web/public/.

## Delete older backups
cd $BACKUPDIR
/usr/bin/find *.gz* -mtime +$MTIME -delete

Strongspace

Strongspace is a natural off-server backup solution for TxD shared hosting accounts. Strongspace supports the rsync protocol.

Please see this TextSnippet: http://textsnippets.com/posts/show/587

Backing up to Amazon's S3

You can also back up to Amazon's S3 service using the s3sync ruby script. Please see the installation and read me files for s3sync to get the SSL cert directory set up. I have the script installed in the directory ~/scripts/s3sync

Here is a sample Bash script to call s3sync. You can call this from cron to automate offsite backups. I first create backups of the MySQL database and web/public/ folders in ~/backups.

Again, chmod 700 the script.

#!/usr/local/bin/bash
# Simple shell script to set env variables and then
# call s3sync.rb to rsync to Amazon S3

export AWS_ACCESS_KEY_ID=*YOUR_AWS_KEY*
export AWS_SECRET_ACCESS_KEY=*YOUR_AWS_SECRET_KEY*

RUBY="/usr/local/bin/ruby"

HOME="/users/home/YOURUSERNAME"
SCRIPTDIR="${HOME}/scripts/s3sync"
BACKUPDIR="${HOME}/backups/"
export SSL_CERT_DIR=${HOME}/ssl_certs
export PATH=$PATH:${HOME}/scripts/s3sync

FLAGS="-r --delete --ssl -d -v"

S3BUCKET="YOURS3BUCKET"
S3BASEDIR="ANS3DIR"

cd $SCRIPTDIR
$RUBY ${SCRIPTDIR}/s3sync.rb $FLAGS $BACKUPDIR ${S3BUCKET}:${S3BASEDIR}
Personal tools