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
30 getconf bandwidthlimit 0
36 ### SANITY CHECKS ##############################################################
38 [ -n "$desturl" -o -n "$destdir" ] || fatal "The destination directory (destdir) must be set when desturl is not used."
39 [ -n "$include" -o -n "$vsinclude" ] || fatal "No source includes specified"
40 [ -n "$password" ] || fatal "The password option must be set."
43 # If vservers are configured, check that the ones listed in $vsnames do exist.
45 if [ $vservers_are_available = yes ]; then
46 if [ "$vsnames" = all ]; then
47 vsnames="$found_vservers"
49 if ! vservers_exist "$vsnames" ; then
50 fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
53 if [ -n "$vsinclude" ]; then
54 info "Using vservers '$vsnames'"
58 [ -z "$vsinclude" ] || warning 'vservers support disabled in backupninja.conf, vsincludes configuration lines will be ignored'
61 ### See if we can login on $desthost
62 if [ "$testconnect" == "yes" ]; then
63 if [ -n "$desturl" ]; then
64 warning 'testconnect can not be used when desturl is set'
66 debug "ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
68 result=`ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'`
69 if [ "$result" != "1" ]; then
70 fatal "Can't connect to $desthost as $destuser."
72 debug "Connected to $desthost as $destuser successfully"
78 ### COMMAND-LINE MANGLING ######################################################
80 ### initialize $execstr*
82 execstr_options="$options --no-print-statistics"
84 if [ -n "$desturl" ]; then
85 [ -z "$destuser" ] || warning 'the configured destuser is ignored since desturl is set'
86 [ -z "$desthost" ] || warning 'the configured desthost is ignored since desturl is set'
87 [ -z "$destdir" ] || warning 'the configured destdir is ignored since desturl is set'
88 execstr_serverpart="$desturl"
90 execstr_serverpart="scp://$destuser@$desthost/$destdir"
94 duplicity_version="`duplicity --version | @AWK@ '{print $2}'`"
95 duplicity_major="`echo $duplicity_version | @AWK@ -F '.' '{print $1}'`"
96 duplicity_minor="`echo $duplicity_version | @AWK@ -F '.' '{print $2}'`"
97 duplicity_sub="`echo $duplicity_version | @AWK@ -F '.' '{print $3}'`"
99 ### ssh/scp/sftp options
100 # 1. duplicity >= 0.4.2 needs --sftp-command
101 # (NB: sftp does not support the -l option)
102 # 2. duplicity 0.4.3 to 0.4.9 replace --ssh-command with --ssh-options, which is
103 # passed to scp and sftp commands by duplicity. We don't use it: since this
104 # version does not use the ssh command anymore, we keep compatibility with
105 # our previous config files by passing $sshoptions to --scp-command and
106 # --sftp-command ourselves
108 scpoptions="$sshoptions"
109 if [ "$bandwidthlimit" != 0 ]; then
110 [ -z "$desturl" ] || warning 'The bandwidthlimit option is not used when desturl is set.'
111 scpoptions="$scpoptions -l $bandwidthlimit"
114 # < 0.4.2 : only uses ssh and scp
115 if [ "$duplicity_major" -le 0 -a "$duplicity_minor" -le 4 -a "$duplicity_sub" -lt 2 ]; then
116 execstr_options="${execstr_options} --scp-command 'scp $scpoptions' --ssh-command 'ssh $sshoptions'"
117 # >= 0.4.2 : also uses sftp, --sftp-command option is now supported
119 sftpoptions="$sshoptions"
120 # == 0.4.2 : uses ssh, scp and sftp
121 if [ "$duplicity_major" -eq 0 -a "$duplicity_minor" -eq 4 -a "$duplicity_sub" -eq 2 ]; then
122 execstr_options="${execstr_options} --scp-command 'scp $scpoptions' --sftp-command 'sftp $sftpoptions' --ssh-command 'ssh $sshoptions'"
123 # >= 0.4.3 : uses only scp and sftp, --ssh-command option is not supported anymore
125 execstr_options="${execstr_options} --scp-command 'scp $scpoptions' --sftp-command 'sftp $sftpoptions'"
129 ### Symmetric or asymmetric (public/private key pair) encryption
130 if [ -n "$encryptkey" ]; then
131 execstr_options="${execstr_options} --encrypt-key $encryptkey"
132 debug "Data will be encrypted with the GnuPG key $encryptkey."
134 debug "Data will be encrypted using symmetric encryption."
137 ### Data signing (or not)
138 if [ "$sign" == yes ]; then
139 # duplicity is not able to sign data when using symmetric encryption
140 [ -n "$encryptkey" ] || fatal "The encryptkey option must be set when signing."
141 # if needed, initialize signkey to a value that is not empty (checked above)
142 [ -n "$signkey" ] || signkey="$encryptkey"
143 execstr_options="${execstr_options} --sign-key $signkey"
144 debug "Data will be signed will the GnuPG key $signkey."
146 debug "Data won't be signed."
149 ### Incremental or full backup mode
150 # If incremental==yes, use the default duplicity behaviour: perform an
151 # incremental backup if old signatures can be found, else switch to
153 # If incremental==no, force a full backup anyway.
154 if [ "$incremental" == "no" ]; then
155 # before 0.4.4, full was an option and not a command
156 if [ "$duplicity_major" -le 0 -a "$duplicity_minor" -le 4 -a "$duplicity_sub" -lt 4 ]; then
157 execstr_options="${execstr_options} --full"
159 execstr_command="full"
163 ### Temporary directory
165 if [ -n "$tmpdir" ]; then
166 if [ ! -d "$tmpdir" ]; then
167 info "Temporary directory ($tmpdir) does not exist, creating it."
169 [ $? -eq 0 ] || fatal "Could not create temporary directory ($tmpdir)."
172 info "Using $tmpdir as TMPDIR"
173 precmd="${precmd}TMPDIR=$tmpdir "
176 ### Cleanup old backup sets (or not)
177 if [ "$keep" != "yes" ]; then
178 if [ "`echo $keep | tr -d 0-9`" == "" ]; then
181 # before 0.4.4, remove-older-than was an option and not a command
182 if [ "$duplicity_major" -le 0 -a "$duplicity_minor" -le 4 -a "$duplicity_sub" -lt 4 ]; then
183 execstr_options="${execstr_options} --remove-older-than $keep"
192 for i in $exclude; do
193 str="${i//__star__/*}"
194 execstr_source="${execstr_source} --exclude '$str'"
198 for i in $include; do
199 [ "$i" != "/" ] || fatal "Sorry, you cannot use 'include = /'"
200 str="${i//__star__/*}"
201 execstr_source="${execstr_source} --include '$str'"
205 if [ $usevserver = yes ]; then
206 for vserver in $vsnames; do
207 for vi in $vsinclude; do
208 str="${vi//__star__/*}"
209 str="$VROOTDIR/$vserver$str"
210 execstr_source="${execstr_source} --include '$str'"
217 ### EXECUTE ####################################################################
219 execstr_source=${execstr_source//\\*/\\\\\\*}
221 ### Cleanup commands (duplicity >= 0.4.4)
224 if [ "$duplicity_major" -ge 0 -a "$duplicity_minor" -ge 4 -a "$duplicity_sub" -ge 4 ]; then
225 debug "$precmd duplicity cleanup --force $execstr_options $execstr_serverpart"
227 export PASSPHRASE=$password
228 output=`nice -n $nicelevel \
230 "$precmd duplicity cleanup --force $execstr_options $execstr_serverpart 2>&1"`
232 if [ $exit_code -eq 0 ]; then
234 info "Duplicity cleanup finished successfully."
237 warning "Duplicity cleanup failed."
243 if [ "$keep" != "yes" ]; then
244 if [ "$duplicity_major" -ge 0 -a "$duplicity_minor" -ge 4 -a "$duplicity_sub" -ge 4 ]; then
245 debug "$precmd duplicity remove-older-than $keep --force $execstr_options $execstr_serverpart"
247 export PASSPHRASE=$password
248 output=`nice -n $nicelevel \
250 "$precmd duplicity remove-older-than $keep --force $execstr_options $execstr_serverpart 2>&1"`
252 if [ $exit_code -eq 0 ]; then
254 info "Duplicity remove-older-than finished successfully."
257 warning "Duplicity remove-older-than failed."
264 debug "$precmd duplicity $execstr_command $execstr_options $execstr_source --exclude '**' / $execstr_serverpart"
266 outputfile=`maketemp backupout`
267 export PASSPHRASE=$password
268 output=`nice -n $nicelevel \
270 "$precmd duplicity $execstr_command $execstr_options $execstr_source --exclude '**' / $execstr_serverpart >$outputfile 2>&1"`
273 cat $outputfile | (while read output ; do
277 if [ $exit_code -eq 0 ]; then
278 info "Duplicity finished successfully."
280 fatal "Duplicity failed."