dup: more or less support duplicity 0.4.2 (bandwidthlimit maybe not working anymore...
[matthijs/upstream/backupninja.git] / handlers / dup
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
11 setsection gpg
12 getconf password
13 getconf sign no
14 getconf encryptkey
15 getconf signkey
16
17 setsection source
18 getconf include
19 getconf vsnames all
20 getconf vsinclude
21 getconf exclude
22
23 setsection dest
24 getconf incremental yes
25 getconf keep 60
26 getconf sshoptions
27 getconf bandwidthlimit 0
28 getconf desthost
29 getconf destdir
30 getconf destuser
31 destdir=${destdir%/}
32
33 [ "$destdir" != "" ] || fatal "Destination directory not set"
34 [ "$include" != "" ] || fatal "No source includes specified"
35
36 ### vservers stuff ###
37
38 # If vservers are configured, check that the ones listed in $vsnames do exist.
39 local usevserver=no
40 if [ $vservers_are_available = yes ]; then
41    if [ "$vsnames" = all ]; then
42       vsnames="$found_vservers"
43    else
44       if ! vservers_exist "$vsnames" ; then
45             fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
46       fi
47    fi
48    if [ -n "$vsinclude" ]; then
49       info "Using vservers '$vsnames'"
50       usevserver=yes
51    fi
52 else
53    [ -z "$vsinclude" ] || warning 'vservers support disabled in backupninja.conf, vsincludes configuration lines will be ignored'
54 fi
55
56
57 ### see if we can login ###
58
59 if [ "$testconnect" == "yes" ]; then
60     debug "ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'"
61     if [ ! $test ]; then
62         result=`ssh $sshoptions -o PasswordAuthentication=no $desthost -l $destuser 'echo -n 1'`
63         if [ "$result" != "1" ]; then
64             fatal "Can't connect to $desthost as $destuser."
65         else
66             debug "Connected to $desthost as $destuser successfully"
67         fi
68     fi
69 fi
70
71 ### COMMAND-LINE MANGLING ###
72
73 # duplicity >= 0.4.2 needs --sftp-command (NB: sftp does not support the -l option)
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 if [ "$duplicity_major" -ge 0 -a "$duplicity_minor" -ge 4 -a "$duplicity_sub" -ge 2 ]; then
79    sftpoptions="$sshoptions"
80 fi
81
82 scpoptions="$sshoptions"
83 [ "$bandwidthlimit" == 0 ] || scpoptions="$scpoptions -l $bandwidthlimit"
84
85 if [ -z "$sftpoptions" ]; then
86    execstr="$options --no-print-statistics --scp-command 'scp $scpoptions' --ssh-command 'ssh $sshoptions' "
87 else
88    execstr="$options --no-print-statistics --scp-command 'scp $scpoptions' --sftp-command 'sftp $sftpoptions' --ssh-command 'ssh $sshoptions' "
89 fi
90
91 # deal with symmetric or asymmetric (public/private key pair) encryption
92 if [ -n "$encryptkey" ]; then
93     execstr="${execstr}--encrypt-key $encryptkey "
94     debug "Data will be encrypted with the GnuPG key $encryptkey."
95 else
96     debug "Data will be encrypted using symmetric encryption."
97 fi
98
99 # deal with data signing
100 if [ "$sign" == yes ]; then
101     # duplicity is not able to sign data when using symmetric encryption
102     [ -n "$encryptkey" ] || fatal "The encryptkey option must be set when signing."
103     # if needed, initialize signkey to a value that is not empty (checked above)
104     [ -n "$signkey" ] || signkey="$encryptkey"
105     execstr="${execstr}--sign-key $signkey "
106     debug "Data will be signed will the GnuPG key $signkey."
107 else
108     debug "Data won't be signed."
109 fi
110
111 # deal with GnuPG passphrase
112 [ -n "$password" ] || fatal "The password option must be set."
113
114 if [ "$keep" != "yes" ]; then
115     if [ "`echo $keep | tr -d 0-9`" == "" ]; then
116         keep="${keep}D"
117     fi
118     execstr="${execstr}--remove-older-than $keep "
119 fi
120
121 if [ "$incremental" == "no" ]; then
122     execstr="${execstr}--full "
123 fi
124
125 execstr_serverpart="scp://$destuser@$desthost/$destdir"
126 execstr_clientpart="/"
127
128 ### SOURCE ###
129
130 set -o noglob
131
132 # excludes
133 for i in $exclude; do
134         str="${i//__star__/*}"
135         i=`readlink -f ${i#}`
136         execstr="${execstr}--exclude '$str' "
137 done
138         
139 # includes 
140 for i in $include; do
141         str="${i//__star__/*}"
142         i=`readlink -f ${i#}`
143         execstr="${execstr}--include '$str' "
144 done
145
146 # vsincludes
147 if [ $usevserver = yes ]; then
148     for vserver in $vsnames; do
149         for vi in $vsinclude; do
150             i=`readlink -f $VROOTDIR/$vserver$vi`
151             str="${i//__star__/*}"
152             execstr="${execstr}--include '$VROOTDIR/$vserver$str' "
153         done
154     done
155 fi
156
157 set +o noglob
158
159 ### EXECUTE ###
160
161 # exclude everything else, start with root
162 #execstr="${execstr}--exclude '**' / "
163                 
164 # include client-part and server-part
165 #execstr="$execstr $execstr_serverpart"
166
167 execstr=${execstr//\\*/\\\\\\*}
168
169 debug "duplicity $execstr --exclude '**' / $execstr_serverpart"
170 if [ ! $test ]; then
171         export PASSPHRASE=$password
172         output=`nice -n $nicelevel \
173                   su -c \
174                     "duplicity $execstr --exclude '**' / $execstr_serverpart 2>&1"`
175         code=$?
176         if [ $code -eq 0 ]; then
177                 debug $output
178                 info "Duplicity finished successfully."
179         else
180                 debug $output
181                 fatal "Duplicity failed."
182         fi
183 fi      
184
185 return 0