1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 # vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
4 # duplicity script for backupninja
9 getconf testconnect yes
26 getconf incremental yes
29 getconf awsaccesskeyid
30 getconf awssecretaccesskey
32 getconf bandwidthlimit 0
38 ### SANITY CHECKS ##############################################################
40 [ -n "$desturl" -o -n "$destdir" ] || fatal "The destination directory (destdir) must be set when desturl is not used."
41 [ -n "$include" -o -n "$vsinclude" ] || fatal "No source includes specified"
42 [ -n "$password" ] || fatal "The password option must be set."
43 if [ "`echo $desturl | @AWK@ -F ':' '{print $1}'`" == "s3+http" ]; then
44 [ -n "$awsaccesskeyid" -a -n "$awssecretaccesskey" ] || fatal "AWS access keys must be set for S3 backups."
48 # If vservers are configured, check that the ones listed in $vsnames do exist.
50 if [ $vservers_are_available = yes ]; then
51 if [ "$vsnames" = all ]; then
52 vsnames="$found_vservers"
54 if ! vservers_exist "$vsnames" ; then
55 fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
58 if [ -n "$vsinclude" ]; then
59 info "Using vservers '$vsnames'"
63 [ -z "$vsinclude" ] || warning 'vservers support disabled in backupninja.conf, vsincludes configuration lines will be ignored'
66 ### See if we can login on $desthost
67 if [ "$testconnect" == "yes" ]; then
68 if [ -n "$desturl" ]; then
69 warning 'testconnect can not be used when desturl is set'
71 debug "ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
73 result=`ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'`
74 if [ "$result" != "1" ]; then
75 fatal "Can't connect to $desthost as $destuser."
77 debug "Connected to $desthost as $destuser successfully"
83 ### COMMAND-LINE MANGLING ######################################################
85 ### initialize $execstr*
87 execstr_options="$options --no-print-statistics"
89 if [ -n "$desturl" ]; then
90 [ -z "$destuser" ] || warning 'the configured destuser is ignored since desturl is set'
91 [ -z "$desthost" ] || warning 'the configured desthost is ignored since desturl is set'
92 [ -z "$destdir" ] || warning 'the configured destdir is ignored since desturl is set'
93 execstr_serverpart="$desturl"
95 execstr_serverpart="scp://$destuser@$desthost/$destdir"
99 duplicity_version="`duplicity --version | @AWK@ '{print $2}'`"
100 duplicity_major="`echo $duplicity_version | @AWK@ -F '.' '{print $1}'`"
101 duplicity_minor="`echo $duplicity_version | @AWK@ -F '.' '{print $2}'`"
102 duplicity_sub="`echo $duplicity_version | @AWK@ -F '.' '{print $3}'`"
104 ### ssh/scp/sftp options
105 # 1. duplicity >= 0.4.2 needs --sftp-command
106 # (NB: sftp does not support the -l option)
107 # 2. duplicity 0.4.3 to 0.4.9 replace --ssh-command with --ssh-options, which is
108 # passed to scp and sftp commands by duplicity. We don't use it: since this
109 # version does not use the ssh command anymore, we keep compatibility with
110 # our previous config files by passing $sshoptions to --scp-command and
111 # --sftp-command ourselves
113 scpoptions="$sshoptions"
114 if [ "$bandwidthlimit" != 0 ]; then
115 [ -z "$desturl" ] || warning 'The bandwidthlimit option is not used when desturl is set.'
116 scpoptions="$scpoptions -l $bandwidthlimit"
119 # < 0.4.2 : only uses ssh and scp
120 if [ "$duplicity_major" -le 0 -a "$duplicity_minor" -le 4 -a "$duplicity_sub" -lt 2 ]; then
121 execstr_options="${execstr_options} --scp-command 'scp $scpoptions' --ssh-command 'ssh $sshoptions'"
122 # >= 0.4.2 : also uses sftp, --sftp-command option is now supported
124 sftpoptions="$sshoptions"
125 # == 0.4.2 : uses ssh, scp and sftp
126 if [ "$duplicity_major" -eq 0 -a "$duplicity_minor" -eq 4 -a "$duplicity_sub" -eq 2 ]; then
127 execstr_options="${execstr_options} --scp-command 'scp $scpoptions' --sftp-command 'sftp $sftpoptions' --ssh-command 'ssh $sshoptions'"
128 # >= 0.4.3 : uses only scp and sftp, --ssh-command option is not supported anymore
130 execstr_options="${execstr_options} --scp-command 'scp $scpoptions' --sftp-command 'sftp $sftpoptions'"
134 ### Symmetric or asymmetric (public/private key pair) encryption
135 if [ -n "$encryptkey" ]; then
136 execstr_options="${execstr_options} --encrypt-key $encryptkey"
137 debug "Data will be encrypted with the GnuPG key $encryptkey."
139 debug "Data will be encrypted using symmetric encryption."
142 ### Data signing (or not)
143 if [ "$sign" == yes ]; then
144 # duplicity is not able to sign data when using symmetric encryption
145 [ -n "$encryptkey" ] || fatal "The encryptkey option must be set when signing."
146 # if needed, initialize signkey to a value that is not empty (checked above)
147 [ -n "$signkey" ] || signkey="$encryptkey"
148 execstr_options="${execstr_options} --sign-key $signkey"
149 debug "Data will be signed will the GnuPG key $signkey."
151 debug "Data won't be signed."
154 ### Incremental or full backup mode
155 # If incremental==yes, use the default duplicity behaviour: perform an
156 # incremental backup if old signatures can be found, else switch to
158 # If incremental==no, force a full backup anyway.
159 if [ "$incremental" == "no" ]; then
160 # before 0.4.4, full was an option and not a command
161 if [ "$duplicity_major" -le 0 -a "$duplicity_minor" -le 4 -a "$duplicity_sub" -lt 4 ]; then
162 execstr_options="${execstr_options} --full"
164 execstr_command="full"
168 ### Temporary directory
170 if [ -n "$tmpdir" ]; then
171 if [ ! -d "$tmpdir" ]; then
172 info "Temporary directory ($tmpdir) does not exist, creating it."
174 [ $? -eq 0 ] || fatal "Could not create temporary directory ($tmpdir)."
177 info "Using $tmpdir as TMPDIR"
178 precmd="${precmd}TMPDIR=$tmpdir "
181 ### Cleanup old backup sets (or not)
182 if [ "$keep" != "yes" ]; then
183 if [ "`echo $keep | tr -d 0-9`" == "" ]; then
186 # before 0.4.4, remove-older-than was an option and not a command
187 if [ "$duplicity_major" -le 0 -a "$duplicity_minor" -le 4 -a "$duplicity_sub" -lt 4 ]; then
188 execstr_options="${execstr_options} --remove-older-than $keep"
198 IFS=$(echo -en "\n\b")
199 for i in $exclude; do
200 str="${i//__star__/*}"
201 execstr_source="${execstr_source} --exclude '$str'"
207 IFS=$(echo -en "\n\b")
208 for i in $include; do
209 [ "$i" != "/" ] || fatal "Sorry, you cannot use 'include = /'"
210 str="${i//__star__/*}"
211 execstr_source="${execstr_source} --include '$str'"
216 if [ $usevserver = yes ]; then
217 for vserver in $vsnames; do
219 IFS=$(echo -en "\n\b")
220 for vi in $vsinclude; do
221 str="${vi//__star__/*}"
222 str="$VROOTDIR/$vserver$str"
223 execstr_source="${execstr_source} --include '$str'"
231 ### EXECUTE ####################################################################
233 execstr_source=${execstr_source//\\*/\\\\\\*}
235 ### If desturl is an S3 URL export the AWS environment variables
236 if [ "`echo $desturl | @AWK@ -F ':' '{print $1}'`" == "s3+http" ]; then
237 export AWS_ACCESS_KEY_ID="$awsaccesskeyid"
238 export AWS_SECRET_ACCESS_KEY="$awssecretaccesskey"
241 ### Cleanup commands (duplicity >= 0.4.4)
244 if [ "$duplicity_major" -ge 0 -a "$duplicity_minor" -ge 4 -a "$duplicity_sub" -ge 4 ]; then
245 debug "$precmd duplicity cleanup --force $execstr_options $execstr_serverpart"
247 export PASSPHRASE=$password
248 output=`nice -n $nicelevel \
250 "$precmd duplicity cleanup --force $execstr_options $execstr_serverpart 2>&1"`
252 if [ $exit_code -eq 0 ]; then
254 info "Duplicity cleanup finished successfully."
257 warning "Duplicity cleanup failed."
263 if [ "$keep" != "yes" ]; then
264 if [ "$duplicity_major" -ge 0 -a "$duplicity_minor" -ge 4 -a "$duplicity_sub" -ge 4 ]; then
265 debug "$precmd duplicity remove-older-than $keep --force $execstr_options $execstr_serverpart"
267 export PASSPHRASE=$password
268 output=`nice -n $nicelevel \
270 "$precmd duplicity remove-older-than $keep --force $execstr_options $execstr_serverpart 2>&1"`
272 if [ $exit_code -eq 0 ]; then
274 info "Duplicity remove-older-than finished successfully."
277 warning "Duplicity remove-older-than failed."
284 debug "$precmd duplicity $execstr_command $execstr_options $execstr_source --exclude '**' / $execstr_serverpart"
286 outputfile=`maketemp backupout`
287 export PASSPHRASE=$password
288 output=`nice -n $nicelevel \
290 "$precmd duplicity $execstr_command $execstr_options $execstr_source --exclude '**' / $execstr_serverpart >$outputfile 2>&1"`
293 cat $outputfile | (while read output ; do
297 if [ $exit_code -eq 0 ]; then
298 info "Duplicity finished successfully."
300 fatal "Duplicity failed."