1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3 # duplicity script for backupninja
8 getconf testconnect yes
25 getconf incremental yes
29 getconf bandwidthlimit 0
35 ### SANITY CHECKS ##############################################################
37 [ -n "$desturl" -o -n "$destdir" ] || fatal "The destination directory (destdir) must be set when desturl is not used."
38 [ -n "$include" -o -n "$vsinclude" ] || fatal "No source includes specified"
39 [ -n "$password" ] || fatal "The password option must be set."
42 # If vservers are configured, check that the ones listed in $vsnames do exist.
44 if [ $vservers_are_available = yes ]; then
45 if [ "$vsnames" = all ]; then
46 vsnames="$found_vservers"
48 if ! vservers_exist "$vsnames" ; then
49 fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
52 if [ -n "$vsinclude" ]; then
53 info "Using vservers '$vsnames'"
57 [ -z "$vsinclude" ] || warning 'vservers support disabled in backupninja.conf, vsincludes configuration lines will be ignored'
60 ### See if we can login on $desthost
61 if [ "$testconnect" == "yes" ]; then
62 if [ -n "$desturl" ]; then
63 warning 'testconnect can not be used when desturl is set'
65 debug "ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
67 result=`ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'`
68 if [ "$result" != "1" ]; then
69 fatal "Can't connect to $desthost as $destuser."
71 debug "Connected to $desthost as $destuser successfully"
77 ### COMMAND-LINE MANGLING ######################################################
79 ### initialize $execstr*
81 execstr_options="$options --no-print-statistics"
83 if [ -n "$desturl" ]; then
84 [ -z "$destuser" ] || warning 'the configured destuser is ignored since desturl is set'
85 [ -z "$desthost" ] || warning 'the configured desthost is ignored since desturl is set'
86 [ -z "$destdir" ] || warning 'the configured destdir is ignored since desturl is set'
87 execstr_serverpart="$desturl"
89 execstr_serverpart="scp://$destuser@$desthost/$destdir"
93 duplicity_version="`duplicity --version | @AWK@ '{print $2}'`"
94 duplicity_major="`echo $duplicity_version | @AWK@ -F '.' '{print $1}'`"
95 duplicity_minor="`echo $duplicity_version | @AWK@ -F '.' '{print $2}'`"
96 duplicity_sub="`echo $duplicity_version | @AWK@ -F '.' '{print $3}'`"
98 ### ssh/scp/sftp options
99 # 1. duplicity >= 0.4.2 needs --sftp-command
100 # (NB: sftp does not support the -l option)
101 # 2. duplicity 0.4.3 to 0.4.9 replace --ssh-command with --ssh-options, which is
102 # passed to scp and sftp commands by duplicity. We don't use it: since this
103 # version does not use the ssh command anymore, we keep compatibility with
104 # our previous config files by passing $sshoptions to --scp-command and
105 # --sftp-command ourselves
107 scpoptions="$sshoptions"
108 if [ "$bandwidthlimit" =! 0 ]; then
109 [ -z "$testurl" ] || warning 'The bandwidthlimit option is not used when desturl is set.'
110 scpoptions="$scpoptions -l $bandwidthlimit"
113 # < 0.4.2 : only uses ssh and scp
114 if [ "$duplicity_major" -le 0 -a "$duplicity_minor" -le 4 -a "$duplicity_sub" -lt 2 ]; then
115 execstr_options="${execstr_options} --scp-command 'scp $scpoptions' --ssh-command 'ssh $sshoptions'"
116 # >= 0.4.2 : also uses sftp, --sftp-command option is now supported
118 sftpoptions="$sshoptions"
119 # == 0.4.2 : uses ssh, scp and sftp
120 if [ "$duplicity_major" -eq 0 -a "$duplicity_minor" -eq 4 -a "$duplicity_sub" -eq 2 ]; then
121 execstr_options="${execstr_options} --scp-command 'scp $scpoptions' --sftp-command 'sftp $sftpoptions' --ssh-command 'ssh $sshoptions'"
122 # >= 0.4.3 : uses only scp and sftp, --ssh-command option is not supported anymore
124 execstr_options="${execstr_options} --scp-command 'scp $scpoptions' --sftp-command 'sftp $sftpoptions'"
128 ### Symmetric or asymmetric (public/private key pair) encryption
129 if [ -n "$encryptkey" ]; then
130 execstr_options="${execstr_options} --encrypt-key $encryptkey"
131 debug "Data will be encrypted with the GnuPG key $encryptkey."
133 debug "Data will be encrypted using symmetric encryption."
136 ### Data signing (or not)
137 if [ "$sign" == yes ]; then
138 # duplicity is not able to sign data when using symmetric encryption
139 [ -n "$encryptkey" ] || fatal "The encryptkey option must be set when signing."
140 # if needed, initialize signkey to a value that is not empty (checked above)
141 [ -n "$signkey" ] || signkey="$encryptkey"
142 execstr_options="${execstr_options} --sign-key $signkey"
143 debug "Data will be signed will the GnuPG key $signkey."
145 debug "Data won't be signed."
148 ### Incremental or full backup mode
149 # If incremental==yes, use the default duplicity behaviour: perform an
150 # incremental backup if old signatures can be found, else switch to
152 # If incremental==no, force a full backup anyway.
153 if [ "$incremental" == "no" ]; then
154 # before 0.4.4, full was an option and not a command
155 if [ "$duplicity_major" -le 0 -a "$duplicity_minor" -le 4 -a "$duplicity_sub" -lt 4 ]; then
156 execstr_options="${execstr_options} --full"
158 execstr_command="full"
162 ### Temporary directory
164 if [ -n "$tmpdir" ]; then
165 if [ ! -d "$tmpdir" ]; then
166 info "Temporary directory ($tmpdir) does not exist, creating it."
168 [ $? -eq 0 ] || fatal "Could not create temporary directory ($tmpdir)."
171 info "Using $tmpdir as TMPDIR"
172 precmd="${precmd}TMPDIR=$tmpdir "
175 ### Cleanup old backup sets (or not)
176 if [ "$keep" != "yes" ]; then
177 if [ "`echo $keep | tr -d 0-9`" == "" ]; then
180 # before 0.4.4, remove-older-than was an option and not a command
181 if [ "$duplicity_major" -le 0 -a "$duplicity_minor" -le 4 -a "$duplicity_sub" -lt 4 ]; then
182 execstr_options="${execstr_options} --remove-older-than $keep"
191 for i in $exclude; do
192 str="${i//__star__/*}"
193 execstr_source="${execstr_source} --exclude '$str'"
197 for i in $include; do
198 [ "$i" != "/" ] || fatal "Sorry, you cannot use 'include = /'"
199 str="${i//__star__/*}"
200 execstr_source="${execstr_source} --include '$str'"
204 if [ $usevserver = yes ]; then
205 for vserver in $vsnames; do
206 for vi in $vsinclude; do
207 str="${vi//__star__/*}"
208 str="$VROOTDIR/$vserver$str"
209 execstr_source="${execstr_source} --include '$str'"
216 ### EXECUTE ####################################################################
218 execstr_source=${execstr_source//\\*/\\\\\\*}
220 ### Cleanup commands (duplicity >= 0.4.4)
223 if [ "$duplicity_major" -ge 0 -a "$duplicity_minor" -ge 4 -a "$duplicity_sub" -ge 4 ]; then
224 debug "$precmd duplicity cleanup --force $execstr_options $execstr_serverpart"
226 export PASSPHRASE=$password
227 output=`nice -n $nicelevel \
229 "$precmd duplicity cleanup --force $execstr_options $execstr_serverpart 2>&1"`
231 if [ $exit_code -eq 0 ]; then
233 info "Duplicity cleanup finished successfully."
236 warning "Duplicity cleanup failed."
242 if [ "$keep" != "yes" ]; then
243 if [ "$duplicity_major" -ge 0 -a "$duplicity_minor" -ge 4 -a "$duplicity_sub" -ge 4 ]; then
244 debug "$precmd duplicity remove-older-than $keep --force $execstr_options $execstr_serverpart"
246 export PASSPHRASE=$password
247 output=`nice -n $nicelevel \
249 "$precmd duplicity remove-older-than $keep --force $execstr_options $execstr_serverpart 2>&1"`
251 if [ $exit_code -eq 0 ]; then
253 info "Duplicity remove-older-than finished successfully."
256 warning "Duplicity remove-older-than failed."
263 debug "$precmd duplicity $execstr_command $execstr_options $execstr_source --exclude '**' / $execstr_serverpart"
265 export PASSPHRASE=$password
266 output=`nice -n $nicelevel \
268 "$precmd duplicity $execstr_command $execstr_options $execstr_source --exclude '**' / $execstr_serverpart 2>&1"`
270 if [ $exit_code -eq 0 ]; then
272 info "Duplicity finished successfully."
275 fatal "Duplicity failed."