2005-12-28 21:19:29 +01:00
# -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2009-05-22 15:27:09 +02:00
# vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
2005-01-19 20:37:20 +01:00
#
# duplicity script for backupninja
2010-10-12 11:47:15 +02:00
# requires duplicity >= 0.4.4, and >= 0.4.9 when using a custom tmpdir.
2005-01-19 20:37:20 +01:00
#
getconf options
2005-05-24 21:08:51 +02:00
getconf testconnect yes
getconf nicelevel 0
2007-11-26 08:42:24 +01:00
getconf tmpdir
2005-05-24 21:08:51 +02:00
setsection gpg
getconf password
2013-11-13 15:18:15 +01:00
getconf signpassword
2005-05-24 21:08:51 +02:00
getconf sign no
getconf encryptkey
2005-12-27 20:38:15 +01:00
getconf signkey
2005-05-24 21:08:51 +02:00
setsection source
2005-01-19 20:37:20 +01:00
getconf include
2005-08-09 22:41:37 +02:00
getconf vsnames all
getconf vsinclude
2005-01-19 20:37:20 +01:00
getconf exclude
2005-05-24 21:08:51 +02:00
setsection dest
2005-08-20 20:17:25 +02:00
getconf incremental yes
2010-05-02 17:40:50 +02:00
getconf increments 30
2005-05-24 21:08:51 +02:00
getconf keep 60
2010-11-21 10:07:36 +01:00
getconf keepincroffulls all
2008-06-24 15:32:34 +02:00
getconf desturl
2010-01-09 23:14:17 +01:00
getconf awsaccesskeyid
getconf awssecretaccesskey
2011-10-22 21:46:50 +02:00
getconf cfusername
getconf cfapikey
getconf cfauthurl
2010-05-06 20:05:20 +02:00
getconf ftp_password
2005-05-24 21:08:51 +02:00
getconf sshoptions
2005-05-27 20:41:40 +02:00
getconf bandwidthlimit 0
2005-01-19 20:37:20 +01:00
getconf desthost
getconf destdir
getconf destuser
destdir=${destdir%/}
2008-01-14 21:32:19 +01:00
### SANITY CHECKS ##############################################################
2005-01-19 20:37:20 +01:00
2008-06-24 15:32:34 +02:00
[ -n "$desturl" -o -n "$destdir" ] || fatal "The destination directory (destdir) must be set when desturl is not used."
2008-06-24 17:39:03 +02:00
[ -n "$include" -o -n "$vsinclude" ] || fatal "No source includes specified"
2008-01-14 21:32:19 +01:00
[ -n "$password" ] || fatal "The password option must be set."
2013-11-13 15:18:15 +01:00
[ -n "$signpassword" -a -n "$signkey" -a -n "$encryptkey" -a "$signkey" != "$encryptkey" ] || fatal "The signpassword option must be set because signkey is different from encryptkey."
2010-01-09 23:14:17 +01:00
if [ "`echo $desturl | @AWK@ -F ':' '{print $1}'`" == "s3+http" ]; then
[ -n "$awsaccesskeyid" -a -n "$awssecretaccesskey" ] || fatal "AWS access keys must be set for S3 backups."
fi
2011-10-22 21:46:50 +02:00
if [ "`echo $desturl | @AWK@ -F ':' '{print $1}'`" == "cf+http" ]; then
[ -n "$cfusername" -a -n "$cfapikey" ] || fatal "Cloudfiles access keys must be set for S3 backups."
fi
2010-05-06 20:05:20 +02:00
if [ "`echo $desturl | @AWK@ -F ':' '{print $1}'`" == "ftp" ]; then
[ -n "$ftp_password" ] || fatal "ftp_password must be set for FTP backups."
fi
2012-09-20 00:10:10 +02:00
if [ "`echo $desturl | @AWK@ -F ':' '{print $1}'`" == "file" ]; then
if [ ! -e "`echo $desturl | @AWK@ -F '://' '{print $2}'`" ]; then
fatal "The destination directory ($desturl) does not exist."
elif [ ! -d "`echo $desturl | @AWK@ -F '://' '{print $2}'`" ]; then
fatal "The destination ($desturl) is not a directory."
fi
fi
2006-01-19 22:58:38 +01:00
2008-01-14 21:32:19 +01:00
### VServers
2006-01-19 22:56:53 +01:00
# If vservers are configured, check that the ones listed in $vsnames do exist.
2011-04-25 18:55:58 +02:00
usevserver=no
2006-01-19 22:56:53 +01:00
if [ $vservers_are_available = yes ]; then
if [ "$vsnames" = all ]; then
vsnames="$found_vservers"
else
if ! vservers_exist "$vsnames" ; then
fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
fi
fi
if [ -n "$vsinclude" ]; then
info "Using vservers '$vsnames'"
usevserver=yes
fi
2006-01-17 23:21:39 +01:00
else
[ -z "$vsinclude" ] || warning 'vservers support disabled in backupninja.conf, vsincludes configuration lines will be ignored'
2005-08-09 22:41:37 +02:00
fi
2008-01-14 21:32:19 +01:00
### See if we can login on $desthost
2005-05-24 21:08:51 +02:00
if [ "$testconnect" == "yes" ]; then
2008-06-24 15:32:34 +02:00
if [ -n "$desturl" ]; then
warning 'testconnect can not be used when desturl is set'
else
debug "ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
if [ ! $test ]; then
result=`ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'`
if [ "$result" != "1" ]; then
2009-05-22 14:50:52 +02:00
fatal "Can't connect to $desthost as $destuser."
2008-06-24 15:32:34 +02:00
else
2009-05-22 14:50:52 +02:00
debug "Connected to $desthost as $destuser successfully"
2008-06-24 15:32:34 +02:00
fi
2008-01-14 21:32:19 +01:00
fi
fi
2005-01-19 20:37:20 +01:00
fi
2008-01-14 21:32:19 +01:00
### COMMAND-LINE MANGLING ######################################################
### initialize $execstr*
2014-03-01 12:54:34 +01:00
execstr_precmd='LC_ALL=C'
2008-01-14 21:32:19 +01:00
execstr_command=
execstr_options="$options --no-print-statistics"
execstr_source=
2008-06-24 15:32:34 +02:00
if [ -n "$desturl" ]; then
[ -z "$destuser" ] || warning 'the configured destuser is ignored since desturl is set'
[ -z "$desthost" ] || warning 'the configured desthost is ignored since desturl is set'
[ -z "$destdir" ] || warning 'the configured destdir is ignored since desturl is set'
execstr_serverpart="$desturl"
else
execstr_serverpart="scp://$destuser@$desthost/$destdir"
fi
2005-05-24 21:08:51 +02:00
2010-04-24 19:56:54 +02:00
### duplicity version (ignore anything else than 0-9 and ".")
duplicity_version="`duplicity --version | @AWK@ '{print $2}' | @SED@ 's/[^.[:digit:]]//g'`"
2007-10-15 22:55:59 +02:00
2010-10-12 11:44:35 +02:00
### ssh/scp/sftp options (duplicity < 0.4.3 is unsupported)
2012-01-29 14:39:39 +01:00
## duplicity >= 0.6.17 : paramiko backend
2014-02-22 15:21:19 +01:00
if version_ge "$duplicity_version" '0.6.17'; then
2012-01-29 14:39:39 +01:00
if [ -n "$sshoptions" ]; then
2013-07-23 12:01:37 +02:00
if echo "$sshoptions" | grep -Eqs '^-o[[:space:]]*IdentityFile=[^ ]+$' ; then
spaceless_sshoptions="$(echo -n "$sshoptions" | @SED@ 's/^-o[[:space:]]*/-o/')"
if [ "$spaceless_sshoptions" != "$sshoptions" ] ; then
warning 'Since duplicity >= 0.6.17, sshoptions option requires no space between -o and IdentityFile.'
warning 'The bad space has been ignored. Update your duplicity handler config file to suppress this message.'
sshoptions="$spaceless_sshoptions"
fi
else
warning 'duplicity >= 0.6.17 only supports the IdentityFile SSH option'
fi
2012-01-29 14:39:39 +01:00
fi
execstr_options="${execstr_options} --ssh-options '$sshoptions'"
2012-01-29 14:40:46 +01:00
if [ "$bandwidthlimit" != 0 ]; then
[ -z "$desturl" ] || warning 'The bandwidthlimit option is not used when desturl is set.'
execstr_precmd="trickle -s -d $bandwidthlimit -u $bandwidthlimit"
fi
2012-01-29 14:39:39 +01:00
## duplicity < 0.6.17 : scp/sftp backend
else
scpoptions="$sshoptions"
if [ "$bandwidthlimit" != 0 ]; then
[ -z "$desturl" ] || warning 'The bandwidthlimit option is not used when desturl is set.'
scpoptions="$scpoptions -l $bandwidthlimit"
fi
sftpoptions="$sshoptions"
execstr_options="${execstr_options} --scp-command 'scp $scpoptions' --sftp-command 'sftp $sftpoptions'"
2008-06-24 16:11:22 +02:00
fi
2005-05-24 21:08:51 +02:00
2008-01-14 21:32:19 +01:00
### Symmetric or asymmetric (public/private key pair) encryption
2005-12-28 18:32:12 +01:00
if [ -n "$encryptkey" ]; then
2008-01-14 21:32:19 +01:00
execstr_options="${execstr_options} --encrypt-key $encryptkey"
debug "Data will be encrypted with the GnuPG key $encryptkey."
2005-12-28 18:32:12 +01:00
else
2008-01-14 21:32:19 +01:00
debug "Data will be encrypted using symmetric encryption."
2005-12-28 18:32:12 +01:00
fi
2008-01-14 21:32:19 +01:00
### Data signing (or not)
2005-12-28 18:32:12 +01:00
if [ "$sign" == yes ]; then
2008-01-14 21:32:19 +01:00
# duplicity is not able to sign data when using symmetric encryption
[ -n "$encryptkey" ] || fatal "The encryptkey option must be set when signing."
# if needed, initialize signkey to a value that is not empty (checked above)
[ -n "$signkey" ] || signkey="$encryptkey"
execstr_options="${execstr_options} --sign-key $signkey"
debug "Data will be signed will the GnuPG key $signkey."
2005-12-28 18:32:12 +01:00
else
2008-01-14 21:32:19 +01:00
debug "Data won't be signed."
2005-05-24 21:08:51 +02:00
fi
2008-01-14 21:32:19 +01:00
### Incremental or full backup mode
# If incremental==yes, use the default duplicity behaviour: perform an
# incremental backup if old signatures can be found, else switch to
# full backup.
# If incremental==no, force a full backup anyway.
if [ "$incremental" == "no" ]; then
2010-10-12 11:44:35 +02:00
execstr_command="full"
2010-05-02 12:05:10 +02:00
else
2010-05-02 17:40:50 +02:00
# we're in incremental mode
if [ "$increments" != "keep" ]; then
# if we don't want to keep every increments
if [ "`echo $increments | tr -d 0-9`" == "" ]; then
increments="${increments}D"
2010-05-02 12:05:10 +02:00
fi
2010-05-02 17:40:50 +02:00
execstr_options="${execstr_options} --full-if-older-than $increments"
2010-05-02 12:05:10 +02:00
fi
2005-01-19 20:37:20 +01:00
fi
2010-03-06 19:25:27 +01:00
### Cleanup options
2014-02-22 15:21:19 +01:00
if ! version_ge "$duplicity_version" '0.6.20'; then
2013-11-17 15:55:05 +01:00
execstr_options="${execstr_options} --extra-clean"
fi
2010-03-06 19:25:27 +01:00
2008-01-14 21:32:19 +01:00
### Temporary directory
if [ -n "$tmpdir" ]; then
if [ ! -d "$tmpdir" ]; then
info "Temporary directory ($tmpdir) does not exist, creating it."
mkdir -p "$tmpdir"
[ $? -eq 0 ] || fatal "Could not create temporary directory ($tmpdir)."
2008-06-25 11:54:45 +02:00
chmod 0700 "$tmpdir"
2008-01-14 21:32:19 +01:00
fi
info "Using $tmpdir as TMPDIR"
2010-10-12 11:15:47 +02:00
execstr_options="${execstr_options} --tempdir '$tmpdir'"
2005-08-20 20:17:25 +02:00
fi
2010-05-06 19:27:18 +02:00
### Archive directory
2011-08-17 12:31:30 +02:00
# duplicity now enables the archive_dir by default, let's put it into /var/cache/backupninja/duplicity
2010-05-06 19:27:18 +02:00
# unless the user has specified it.
2011-08-17 12:31:30 +02:00
if echo "${options}" | grep -qv -- "--archive-dir" ; then
execstr_options="${execstr_options} --archive-dir /var/cache/backupninja/duplicity"
2010-05-06 19:27:18 +02:00
fi
2008-01-14 21:32:19 +01:00
### Cleanup old backup sets (or not)
if [ "$keep" != "yes" ]; then
if [ "`echo $keep | tr -d 0-9`" == "" ]; then
keep="${keep}D"
fi
fi
2005-05-24 21:08:51 +02:00
2008-01-14 21:32:19 +01:00
### Source
2005-01-19 20:37:20 +01:00
2006-01-19 22:58:38 +01:00
set -o noglob
2005-01-19 20:37:20 +01:00
# excludes
2010-01-09 00:44:43 +01:00
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
2006-01-19 22:58:38 +01:00
for i in $exclude; do
2006-06-04 02:30:03 +02:00
str="${i//__star__/*}"
2008-01-14 21:32:19 +01:00
execstr_source="${execstr_source} --exclude '$str'"
2005-01-19 20:37:20 +01:00
done
2010-01-09 00:44:43 +01:00
IFS=$SAVEIFS
2008-01-14 21:32:19 +01:00
# includes
2010-01-09 00:44:43 +01:00
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
2006-01-19 22:58:38 +01:00
for i in $include; do
2006-06-04 02:36:33 +02:00
[ "$i" != "/" ] || fatal "Sorry, you cannot use 'include = /'"
2006-06-04 02:30:03 +02:00
str="${i//__star__/*}"
2008-01-14 21:32:19 +01:00
execstr_source="${execstr_source} --include '$str'"
2005-01-19 20:37:20 +01:00
done
2010-01-09 00:44:43 +01:00
IFS=$SAVEIFS
2005-01-19 20:37:20 +01:00
2005-08-09 22:41:37 +02:00
# vsincludes
2006-01-19 22:56:53 +01:00
if [ $usevserver = yes ]; then
2006-06-04 02:30:03 +02:00
for vserver in $vsnames; do
2010-01-09 00:44:43 +01:00
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
2006-06-04 02:30:03 +02:00
for vi in $vsinclude; do
2009-05-22 14:50:52 +02:00
str="${vi//__star__/*}"
str="$VROOTDIR/$vserver$str"
execstr_source="${execstr_source} --include '$str'"
2006-06-04 02:30:03 +02:00
done
2010-01-09 00:44:43 +01:00
IFS=$SAVEIFS
2006-06-04 02:30:03 +02:00
done
2005-08-09 22:41:37 +02:00
fi
2006-01-19 22:58:38 +01:00
set +o noglob
2008-01-14 21:32:19 +01:00
### EXECUTE ####################################################################
execstr_source=${execstr_source//\\*/\\\\\\*}
2010-01-09 23:14:17 +01:00
### If desturl is an S3 URL export the AWS environment variables
if [ "`echo $desturl | @AWK@ -F ':' '{print $1}'`" == "s3+http" ]; then
export AWS_ACCESS_KEY_ID="$awsaccesskeyid"
export AWS_SECRET_ACCESS_KEY="$awssecretaccesskey"
fi
2011-10-22 21:46:50 +02:00
### If desturl is a RackSpace's CloudFiles URL export the relevant
### environment variables
if [ "`echo $desturl | @AWK@ -F ':' '{print $1}'`" == "cf+http" ]; then
export CLOUDFILES_USERNAME="$cfusername"
export CLOUDFILES_APIKEY="$cfapikey"
if [ -n "$cfauthurl" ]; then
export CLOUDFILES_AUTHURL="$cfauthurl"
fi
fi
2008-01-14 21:32:19 +01:00
### Cleanup commands (duplicity >= 0.4.4)
# cleanup
2012-01-29 14:39:10 +01:00
debug "$execstr_precmd duplicity cleanup --force $execstr_options $execstr_serverpart"
2010-10-12 11:44:35 +02:00
if [ ! $test ]; then
export PASSPHRASE=$password
2013-11-13 15:18:15 +01:00
export SIGN_PASSPHRASE=$signpassword
2010-10-12 11:44:35 +02:00
export FTP_PASSWORD=$ftp_password
output=`nice -n $nicelevel \
su -c \
2012-01-29 14:39:10 +01:00
"$execstr_precmd duplicity cleanup --force $execstr_options $execstr_serverpart 2>&1"`
2010-10-12 11:44:35 +02:00
exit_code=$?
if [ $exit_code -eq 0 ]; then
debug $output
info "Duplicity cleanup finished successfully."
else
debug $output
warning "Duplicity cleanup failed."
fi
fi
# remove-older-than
if [ "$keep" != "yes" ]; then
2012-01-29 14:39:10 +01:00
debug "$execstr_precmd duplicity remove-older-than $keep --force $execstr_options $execstr_serverpart"
2008-01-14 21:32:19 +01:00
if [ ! $test ]; then
export PASSPHRASE=$password
2013-11-13 15:18:15 +01:00
export SIGN_PASSPHRASE=$signpassword
2010-05-06 20:05:20 +02:00
export FTP_PASSWORD=$ftp_password
2008-01-14 21:32:19 +01:00
output=`nice -n $nicelevel \
2010-10-12 11:44:35 +02:00
su -c \
2012-01-29 14:39:10 +01:00
"$execstr_precmd duplicity remove-older-than $keep --force $execstr_options $execstr_serverpart 2>&1"`
2008-01-14 21:32:19 +01:00
exit_code=$?
if [ $exit_code -eq 0 ]; then
2009-05-22 14:50:52 +02:00
debug $output
2010-10-12 11:44:35 +02:00
info "Duplicity remove-older-than finished successfully."
2008-01-14 21:32:19 +01:00
else
2009-05-22 14:50:52 +02:00
debug $output
2010-10-12 11:44:35 +02:00
warning "Duplicity remove-older-than failed."
2008-01-14 21:32:19 +01:00
fi
fi
fi
2005-01-19 20:37:20 +01:00
2010-11-21 10:07:36 +01:00
# remove-all-inc-of-but-n-full : remove increments of older full backups : only keep latest ones
if [ "$keep" != "yes" ]; then
if [ "$keepincroffulls" != "all" ]; then
2014-02-22 15:21:19 +01:00
if version_ge "$duplicity_version" '0.6.10'; then
2012-01-29 14:39:10 +01:00
debug "$execstr_precmd duplicity remove-all-inc-of-but-n-full $keepincroffulls --force $execstr_options $execstr_serverpart"
2010-11-21 10:07:36 +01:00
if [ ! $test ]; then
export PASSPHRASE=$password
2013-11-13 15:18:15 +01:00
export SIGN_PASSPHRASE=$signpassword
2010-11-21 10:07:36 +01:00
export FTP_PASSWORD=$ftp_password
output=`nice -n $nicelevel \
su -c \
2012-01-29 14:39:10 +01:00
"$execstr_precmd duplicity remove-all-inc-of-but-n-full $keepincroffulls --force $execstr_options $execstr_serverpart 2>&1"`
2010-11-21 10:07:36 +01:00
exit_code=$?
if [ $exit_code -eq 0 ]; then
debug $output
info "Duplicity remove-all-inc-of-but-n-full finished successfully."
else
debug $output
warning "Duplicity remove-all-inc-of-but-n-full failed."
fi
fi
fi
fi
fi
2008-01-14 21:32:19 +01:00
### Backup command
2012-01-29 14:39:10 +01:00
debug "$execstr_precmd duplicity $execstr_command $execstr_options $execstr_source --exclude '**' / $execstr_serverpart"
2005-01-19 20:37:20 +01:00
if [ ! $test ]; then
2010-01-05 06:44:36 +01:00
outputfile=`maketemp backupout`
2008-01-14 21:32:19 +01:00
export PASSPHRASE=$password
2013-11-13 15:18:15 +01:00
export SIGN_PASSPHRASE=$signpassword
2010-05-06 20:05:20 +02:00
export FTP_PASSWORD=$ftp_password
2008-01-14 21:32:19 +01:00
output=`nice -n $nicelevel \
su -c \
2012-01-29 14:39:10 +01:00
"$execstr_precmd duplicity $execstr_command $execstr_options $execstr_source --exclude '**' / $execstr_serverpart >$outputfile 2>&1"`
2008-01-14 21:32:19 +01:00
exit_code=$?
2010-01-05 06:44:36 +01:00
debug $output
cat $outputfile | (while read output ; do
2012-02-25 15:04:38 +01:00
if [ $exit_code -eq 0 ]; then
info $output
else
error $output
fi
2010-01-05 06:44:36 +01:00
done
)
2008-01-14 21:32:19 +01:00
if [ $exit_code -eq 0 ]; then
info "Duplicity finished successfully."
else
fatal "Duplicity failed."
fi
2010-01-05 06:44:36 +01:00
rm $outputfile
2008-01-14 21:32:19 +01:00
fi
2005-01-19 20:37:20 +01:00
return 0