Skip to content

Instantly share code, notes, and snippets.

@rdkls
Created February 24, 2014 02:17
Show Gist options
  • Save rdkls/9180840 to your computer and use it in GitHub Desktop.
Save rdkls/9180840 to your computer and use it in GitHub Desktop.
duplicity backup script
#!/bin/bash
export FTP_HOST=YOUR_HOST_HERE
export FTP_USER=YOUR_USERNAME_HERE
export FTP_PASSWORD=YOUR_PASSWORD_HERE
export PASSPHRASE=YOUR_PASSPHRASE_HERE
export SIGN_PASSPHRASE=YOUR_SIGN_PASSPHRASE_HERE
usage='Usage: backups [list|ftp|retrieve|ship|restore]'
duplicity=/usr/bin/duplicity
remote_basedir=/db01-nick/db
backup_dir=/data/backup
mysqldump=/usr/bin/mysqldump
mysql_backupdir=${backup_dir}/mysql
mysql_user='YOUR_MYSQL_USER_HERE'
mysql_pass='YOUR_MYSQL_PASS_HERE'
mysql_host='127.0.0.1'
mysql_dbs='YOUR_MYSQL_DBS_HERE'
if [ -z $1 ] ; then
echo $usage
exit 1
fi
if [ 'list' == $1 ] ; then
cmd="$duplicity list-current-files ftp://${FTP_USER}@${FTP_HOST}${remote_basedir}"
$cmd
elif [ 'ftp' == $1 ] ; then
ncftp -u $FTP_USER -p $FTP_PASSWORD $FTP_HOST
elif [ 'retrieve' == $1 ] ; then
if [ -z $2 ] ; then
echo 'backup: supply the full path to the backup you wish to retrieve. easiest obtained with "backup list"'
exit 1
fi
src=$2
tmp_dir=`mktemp -d`
dst=${tmp_dir}/`basename $src`
echo "Restoring '$src' to '$tmp_dir' ..."
cmd="$duplicity restore --file-to-restore $src ftp://${FTP_USER}@${FTP_HOST}${remote_basedir} $dst"
$cmd
if [ 'gz' == ${dst##*.} ] ; then
cd $tmp_dir
gunzip $dst
fi
echo '... done.'
elif [ 'restore' == $1 ] ; then
if [ -z $2 ] ; then
echo 'backup: supply the backup you wish to restore'
exit 1
fi
read -p "About to restore database backup $2. Are you sure? " -n 1 -r
echo # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]] ; then
echo 'cancelled'
exit 1
fi
echo 'restore'
elif [ 'ship' == $1 ] ; then
echo "`date` - db01-nick MySQL backup start"
#/usr/sbin/automysqlbackup
fn=`date +%Y%m%d_%H%M%S`.sql
$mysqldump --host=$mysql_host --user=$mysql_user --password=$mysql_pass --databases $mysql_dbs > ${mysql_backupdir}/${fn}
echo "`date` - MySQL backup complete done"
if [ 01 == `date +%d` ] ; then
echo 'First of month - duplicity FULL'
type=full
fi
echo "`date` - Duplicity Start ..."
cmd="$duplicity $type ${backup_dir} ftp://${FTP_USER}@${FTP_HOST}${remote_basedir}"
$cmd
echo "`date` - db01-nick Database backups complete"
else
echo $usage
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment