rdiff: incorporate sshoptions into options via remote-schema if not there
[matthijs/upstream/backupninja.git] / handlers / dup.in
1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 #
3 # duplicity script for backupninja
4 # requires duplicity
5 #
6
7 getconf options
8 getconf testconnect yes
9 getconf nicelevel 0
10 getconf tmpdir
11
12 setsection gpg
13 getconf password
14 getconf sign no
15 getconf encryptkey
16 getconf signkey
17
18 setsection source
19 getconf include
20 getconf vsnames all
21 getconf vsinclude
22 getconf exclude
23
24 setsection dest
25 getconf incremental yes
26 getconf keep 60
27 getconf sshoptions
28 getconf bandwidthlimit 0
29 getconf desthost
30 getconf destdir
31 getconf destuser
32 destdir=${destdir%/}
33
34 [ "$destdir" != "" ] || fatal "Destination directory not set"
35 [ "$include" != "" ] || fatal "No source includes specified"
36
37 ### vservers stuff ###
38
39 # If vservers are configured, check that the ones listed in $vsnames do exist.
40 local usevserver=no
41 if [ $vservers_are_available = yes ]; then
42    if [ "$vsnames" = all ]; then
43       vsnames="$found_vservers"
44    else
45       if ! vservers_exist "$vsnames" ; then
46             fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
47       fi
48    fi
49    if [ -n "$vsinclude" ]; then
50       info "Using vservers '$vsnames'"
51       usevserver=yes
52    fi
53 else
54    [ -z "$vsinclude" ] || warning 'vservers support disabled in backupninja.conf, vsincludes configuration lines will be ignored'
55 fi
56
57
58 ### see if we can login ###
59
60 if [ "$testconnect" == "yes" ]; then
61     debug "ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
62     if [ ! $test ]; then
63         result=`ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'`
64         if [ "$result" != "1" ]; then
65             fatal "Can't connect to $desthost as $destuser."
66         else
67             debug "Connected to $desthost as $destuser successfully"
68         fi
69     fi
70 fi
71
72 ### COMMAND-LINE MANGLING ###
73
74 duplicity_version="`duplicity --version | @AWK@ '{print $2}'`"
75 duplicity_major="`echo $duplicity_version | @AWK@ -F '.' '{print $1}'`"
76 duplicity_minor="`echo $duplicity_version | @AWK@ -F '.' '{print $2}'`"
77 duplicity_sub="`echo $duplicity_version | @AWK@ -F '.' '{print $3}'`"
78
79 # 1. duplicity >= 0.4.2 needs --sftp-command (NB: sftp does not support the -l option)
80 # 2. duplicity >= 0.4.3 replaces --ssh-command with --ssh-options, which:
81 #      - is passed to scp and sftp commands by duplicity
82 #      - has a special syntax we can not directly feed the command line with
83 #        (e.g. "IdentityFile=/root/.ssh/id_dsa_backupninja Port=2222", without the -o)
84 #    so we don't use it: since this version does not use the ssh command anymore,
85 #    we keep compatibility with our previous config files by passing $sshoptions to
86 #    --scp-command and --sftp-command ourselves
87
88 scpoptions="$sshoptions"
89 [ "$bandwidthlimit" == 0 ] || scpoptions="$scpoptions -l $bandwidthlimit"
90
91 execstr="$options --no-print-statistics "
92
93 # < 0.4.2 : only uses ssh and scp
94 if [ "$duplicity_major" -le 0 -a "$duplicity_minor" -le 4 -a "$duplicity_sub" -lt 2 ]; then
95    execstr="$execstr --scp-command 'scp $scpoptions' --ssh-command 'ssh $sshoptions' "
96 # >= 0.4.2 : also uses sftp, --sftp-command option is now supported
97 else
98    sftpoptions="$sshoptions"
99    # == 0.4.2 : uses ssh, scp and sftp
100    if [ "$duplicity_major" -eq 0 -a "$duplicity_minor" -eq 4 -a "$duplicity_sub" -eq 2 ]; then
101       execstr="$execstr --scp-command 'scp $scpoptions' --sftp-command 'sftp $sftpoptions' --ssh-command 'ssh $sshoptions' "
102    # >= 0.4.3 : uses only scp and sftp, --ssh-command option is not supported anymore
103    else
104       execstr="$execstr --scp-command 'scp $scpoptions' --sftp-command 'sftp $sftpoptions' "
105    fi
106 fi
107
108 # deal with symmetric or asymmetric (public/private key pair) encryption
109 if [ -n "$encryptkey" ]; then
110     execstr="${execstr}--encrypt-key $encryptkey "
111     debug "Data will be encrypted with the GnuPG key $encryptkey."
112 else
113     debug "Data will be encrypted using symmetric encryption."
114 fi
115
116 # deal with data signing
117 if [ "$sign" == yes ]; then
118     # duplicity is not able to sign data when using symmetric encryption
119     [ -n "$encryptkey" ] || fatal "The encryptkey option must be set when signing."
120     # if needed, initialize signkey to a value that is not empty (checked above)
121     [ -n "$signkey" ] || signkey="$encryptkey"
122     execstr="${execstr}--sign-key $signkey "
123     debug "Data will be signed will the GnuPG key $signkey."
124 else
125     debug "Data won't be signed."
126 fi
127
128 # deal with GnuPG passphrase
129 [ -n "$password" ] || fatal "The password option must be set."
130
131 if [ "$keep" != "yes" ]; then
132     if [ "`echo $keep | tr -d 0-9`" == "" ]; then
133         keep="${keep}D"
134     fi
135     execstr="${execstr}--remove-older-than $keep "
136 fi
137
138 if [ "$incremental" == "no" ]; then
139     execstr="${execstr}--full "
140 fi
141
142 execstr_serverpart="scp://$destuser@$desthost/$destdir"
143 execstr_clientpart="/"
144
145 ### SOURCE ###
146
147 set -o noglob
148
149 # excludes
150 for i in $exclude; do
151    str="${i//__star__/*}"
152    execstr="${execstr}--exclude '$str' "
153 done
154         
155 # includes 
156 for i in $include; do
157    [ "$i" != "/" ] || fatal "Sorry, you cannot use 'include = /'"
158    str="${i//__star__/*}"
159    execstr="${execstr}--include '$str' "
160 done
161
162 # vsincludes
163 if [ $usevserver = yes ]; then
164    for vserver in $vsnames; do
165       for vi in $vsinclude; do
166          str="${vi//__star__/*}"
167          str="$VROOTDIR/$vserver$str"
168          execstr="${execstr}--include '$str' "
169       done
170    done
171 fi
172
173 set +o noglob
174
175 ### deal with tmpdir ###
176 precmd=
177 if [ -n "$tmpdir" ]; then
178    if [ ! -d "$tmpdir" ]; then
179       info "Temporary directory ($tmpdir) does not exist, creating it."
180       mkdir -p "$tmpdir"
181       [ $? -eq 0 ] || fatal "Could not create temporary directory ($tmpdir)."
182    fi
183    info "Using $tmpdir as TMPDIR"
184    precmd="${precmd}TMPDIR=$tmpdir "
185 fi
186
187 ### EXECUTE ###
188
189 execstr=${execstr//\\*/\\\\\\*}
190
191 debug "$precmd duplicity $execstr --exclude '**' / $execstr_serverpart"
192 if [ ! $test ]; then
193         export PASSPHRASE=$password
194         output=`nice -n $nicelevel \
195                   su -c \
196                     "$precmd duplicity $execstr --exclude '**' / $execstr_serverpart 2>&1"`
197         code=$?
198         if [ $code -eq 0 ]; then
199                 debug $output
200                 info "Duplicity finished successfully."
201         else
202                 debug $output
203                 fatal "Duplicity failed."
204         fi
205 fi      
206
207 return 0