r3562@krups: intrigeri | 2005-11-16 20:20:16 +0100
[matthijs/upstream/backupninja.git] / handlers / dup.helper
1 HELPERS="$HELPERS dup:incremental_encrypted_remote_filesystem_backup"
2
3 ### Functions
4
5 do_dup_host_includes() {
6    set -o noglob
7    # choose the files to backup
8    REPLY=
9    while [ -z "$REPLY" ]; do
10       formBegin "$dup_title - host system: includes"
11          for ((i=0; i < ${#dup_default_includes[@]} ; i++)); do
12             formItem include ${dup_default_includes[$i]}
13          done
14          formItem include ""
15          formItem include ""
16          formItem include ""
17          formDisplay
18       [ $? = 0 ] || return 1
19       dup_includes=($REPLY)
20    done
21    set +o noglob
22 }
23
24 do_dup_vserver() {
25    # choose the vservers to backup
26    vservers_chooser "$dup_title"
27    [ $? = 0 ] || return 1
28    dup_vsnames="$vservers_chooser_vsnames"
29
30    set -o noglob
31    # choose the files to backup
32    REPLY=
33    while [ -z "$REPLY" ]; do
34       formBegin "$dup_title - vservers: includes"
35          for ((i=0; i < ${#dup_default_includes[@]} ; i++)); do
36             formItem include ${dup_default_includes[$i]}
37          done
38          formItem include ""
39          formItem include ""
40          formItem include ""
41       formDisplay
42       [ $? = 0 ] || return 1
43       dup_vsincludes=($REPLY)
44    done
45    set +o noglob
46 }
47
48 do_dup_excludes() {
49    set -o noglob
50    formBegin "$dup_title: excludes"
51      for ((i=0; i < ${#dup_default_excludes[@]} ; i++)); do
52         formItem exclude ${dup_default_excludes[$i]}
53      done
54      formItem exclude ""
55      formItem exclude ""
56      formItem exclude ""
57    formDisplay
58    [ $? = 0 ] || return 1
59    dup_excludes=($REPLY)
60    set +o noglob
61 }
62
63 do_dup_src() {
64    host_or_vservers_chooser "$dup_title"
65    [ $? = 0 ] || return 1
66    case $host_or_vservers in
67       'host')
68          do_dup_host_includes
69          [ $? = 0 ] || return 1
70          ;;
71       'vservers')
72          do_dup_vserver
73          [ $? = 0 ] || return 1
74          ;;
75       'both')
76          do_dup_host_includes
77          [ $? = 0 ] || return 1
78          do_dup_vserver
79          [ $? = 0 ] || return 1
80          ;;
81       *)
82          return 1
83          ;;
84    esac
85    do_dup_excludes
86    [ $? = 0 ] || return 1
87    
88    _src_done="(DONE)"
89    setDefault dest
90 }
91
92 do_dup_dest() {
93
94    local replyconverted
95    local thereply
96
97    set -o noglob
98    REPLY=
99    while [ -z "$REPLY" -o -z "$dup_destdir" -o -z "$dup_desthost" -o -z "$dup_destuser" ]; do
100       formBegin "$dup_title - destination: last three items are compulsory"
101         formItem "desthost" "$dup_desthost"
102         formItem "destuser" "$dup_destuser"
103         formItem "destdir" "$dup_destdir"
104         formItem "keep" "$dup_keep"
105         formItem "incremental" "$dup_incremental"
106         formItem "bandwidthlimit" "$dup_bandwidth"
107         formItem "sshoptions" "$dup_sshoptions"
108       formDisplay
109       [ $? = 0 ] || return 1
110
111       IFS=$''
112       replyconverted=`echo $REPLY | tr '\n' :`
113       IFS=$':'
114       thereply=($replyconverted)
115       IFS=$' \t\n'
116       
117       dup_desthost=${thereply[0]}
118       dup_destuser=${thereply[1]}
119       dup_destdir=${thereply[2]}
120       dup_keep=${thereply[3]}
121       dup_incremental=${thereply[4]}
122       dup_bandwidth=${thereply[5]}
123       dup_sshoptions=${thereply[6]}
124
125    done
126    set +o noglob
127
128    _dest_done="(DONE)"
129    setDefault gpg
130 }
131
132 do_dup_gpg() {
133    
134    set -o noglob
135
136    # encryptkey ?
137    REPLY=
138    while [ -z "$REPLY" -o -z "$dup_gpg_encryptkey" ]; do
139       inputBox "$dup_title - GnuPG" "Enter the GnuPG key ID to be used to encrypt the backups:" "$dup_gpg_encryptkey"
140       [ $? = 0 ] || return 1
141       dup_gpg_encryptkey="$REPLY"
142    done
143
144    # passphrase ?
145    REPLY=
146    while [ -z "$REPLY" -o -z "$dup_gpg_password" ]; do
147       passwordBox "$dup_title - GnuPG" "Enter the passphrase needed to unlock the key 0x$dup_gpg_encryptkey"
148       [ $? = 0 ] || return 1
149       dup_gpg_password="$REPLY"
150    done
151
152    # sign ?
153    booleanBox "$dup_title - GnuPG" "Sign the backups?" "$dup_gpg_sign"
154    if [ $? = 0 ]; then
155       dup_gpg_sign=yes
156    else
157       dup_gpg_sign=no
158    fi
159
160    set +o noglob
161    _gpg_done="(DONE)"
162    setDefault conn
163 }
164
165 do_dup_conn() {
166    _con_done="(DONE)"
167    setDefault adv
168 }
169
170 do_dup_misc_options() {
171
172    set -o noglob
173    local replyconverted
174    local thereply
175
176    formBegin "$dup_title - misc. options"
177      formItem "nicelevel" "$dup_nicelevel"
178      formItem "testconnect" "$dup_testconnect"
179      formItem "options" "$dup_options"
180    formDisplay
181    [ $? = 0 ] || return 1
182
183    IFS=$''
184    replyconverted=`echo $REPLY | tr '\n' :`
185    IFS=$':'
186    thereply=($replyconverted)
187    IFS=$' \t\n'
188
189    dup_nicelevel=${thereply[0]}
190    dup_testconnect=${thereply[1]}
191    dup_options=${thereply[2]}
192
193    set +o noglob
194 }
195
196 # (rdiff.helper compatible interface... there could be some sode to share, hmmm.)
197 do_dup_adv() {
198    do_dup_misc_options
199    [ $? = 0 ] || return 1
200    _adv_done="(DONE)"
201    setDefault finish
202 }
203
204 do_dup_finish() {
205    get_next_filename $configdirectory/90.dup
206    cat > $next_filename <<EOF
207 # passed directly to duplicity
208 #options = --verbosity 8
209 options = $dup_options
210
211 # default is 0, but set to 19 if you want to lower the priority.
212 nicelevel = $dup_nicelevel
213
214 # default is yes. set to no to skip the test if the remote host is alive
215 testconnect = $dup_testconnect
216
217 ######################################################
218 ## gpg section
219 ## (how to encrypt and optionnally sign the backups)
220
221 [gpg]
222
223 # passphrase needed to unlock the GnuPG key
224 # NB: do not quote it, and it should not contain any quote
225 password = $dup_gpg_password
226
227 # default is no, for backward compatibility with backupninja <= 0.5.
228 # when set to yes, encryptkey option must be set below.
229 sign = $dup_gpg_sign
230
231 # key ID used for data encryption and, optionnally, signing.
232 # if not set, local root's default gpg key is used.
233 encryptkey = $dup_gpg_encryptkey
234
235 ######################################################
236 ## source section
237 ## (where the files to be backed up are coming from)
238
239 [source]
240
241 # files to include in the backup
242 # (supports globbing with '*')
243 # BIG FAT WARNING
244 # Symlinks are not dereferenced. Moreover, an include line whose path
245 # contains, at any level, a symlink to a directory, will only have the
246 # symlink backed-up, not the target directory's content. Yes, you have
247 # to dereference yourself the symlinks, or to use 'mount --bind'
248 # instead.
249 # EXAMPLE
250 # Let's say /home is a symlink to /mnt/crypt/home ; the following line
251 # will only backup a "/home" symlink ; neither /home/user nor
252 # /home/user/Mail will be backed-up :
253 #   include = /home/user/Mail
254 # A workaround is to 'mount --bind /mnt/crypt/home /home' ; another
255 # one is to write :
256 #   include = /mnt/crypt/home/user/Mail
257 EOF
258
259    if [ "$host_or_vservers" == 'host' -o "$host_or_vservers" == 'both' ]; then
260       set -o noglob
261       for ((i=0; i < ${#dup_includes[@]} ; i++)); do
262          echo "include = ${dup_includes[$i]}" >> $next_filename
263       done
264       set +o noglob
265    fi
266
267    cat >> $next_filename <<EOF
268
269 # If vservers = yes in /etc/backupninja.conf then the following variables can
270 # be used:
271 # vsnames = all | <vserver1> <vserver2> ... (default = all)
272 # vsinclude = <path>
273 # Any path specified in vsinclude is added to the include list for each vserver
274 # listed in vsnames (or all if vsnames = all).
275 # E.g. vsinclude = /home will backup the /home partition in every vserver
276 # listed in vsnames. If you have vsnames = "foo bar baz", this vsinclude will
277 # add to the include list /vservers/foo/home, /vservers/bar/home and
278 # /vservers/baz/home.
279 # Vservers paths are derived from $VROOTDIR.
280
281 EOF
282
283    if [ "$host_or_vservers" == 'vservers' -o "$host_or_vservers" == 'both' ]; then
284       set -o noglob
285       echo "vsnames = \"$dup_vsnames\"\n" >> $next_filename
286       for ((i=0; i < ${#dup_vsincludes[@]} ; i++)); do
287          echo "vsinclude = ${dup_vsincludes[$i]}" >> $next_filename
288       done
289       set +o noglob
290    fi
291
292    # excludes
293    cat >> $next_filename <<EOF
294
295 # rdiff-backup specific comment, TO ADAPT
296 # files to exclude from the backup
297 # (supports globbing with '*')
298 EOF
299     set -o noglob
300     for ((i=0; i < ${#dup_excludes[@]} ; i++)); do
301         echo exclude = ${dup_excludes[$i]} >> $next_filename
302     done
303     set +o noglob
304
305     cat >> $next_filename <<EOF
306
307 ######################################################
308 ## destination section
309 ## (where the files are copied to)
310
311 [dest]
312
313 # perform an incremental backup? (default = yes)
314 # if incremental = no, perform a full backup in order to start a new backup set
315 incremental = $dup_incremental
316
317 # how many days of data to keep ; default is 60 days.
318 # (you can also use the time format of duplicity)
319 # 'keep = yes' means : do not delete old data, the remote host will take care of this
320 #keep = 60
321 #keep = yes
322 keep = $dup_keep
323
324 # bandwith limit, in kbit/s ; default is 0, i.e. no limit
325 #bandwidthlimit = 128
326 bandwidthlimit = $dup_bandwidth
327
328 # passed directly to ssh and scp
329 #sshoptions = -i /root/.ssh/id_dsa_duplicity
330 sshoptions = $dup_sshoptions
331
332 # put the backups under this directory
333 destdir = $dup_destdir
334
335 # the machine which will receive the backups
336 desthost = $dup_desthost
337
338 # make the files owned by this user
339 # note: you must be able to ssh backupuser@backhost
340 # without specifying a password (if type = remote).
341 destuser = $dup_destuser
342
343 EOF
344
345     chmod 600 $next_filename
346
347 }
348
349 dup_main_menu() {
350
351   while true; do
352      srcitem="choose files to include & exclude $_src_done"
353      destitem="configure backup destination $_dest_done"
354      gpgitem="configure GnuPG encryption/signing $_gpg_done"
355      conitem="set up ssh keys and test remote connection $_con_done"
356      advitem="edit advanced settings $_adv_done"
357      menuBox "$dup_title" "choose a step:" \
358         src "$srcitem" \
359         dest "$destitem" \
360         gpg "$gpgitem" \
361         conn "$conitem" \
362         adv "$advitem" \
363         finish "finish and create config file"
364      [ $? = 0 ] || return 1
365      result="$REPLY"
366
367      case "$result" in
368         "src") do_dup_src;;
369         "dest") do_dup_dest;;
370         "gpg") do_dup_gpg;;
371         "conn") do_dup_conn;;
372         "adv") do_dup_adv;;
373         "finish")
374            if [[ "$_con_done$_dest_done$_gpg_done$_src_done" != "(DONE)(DONE)(DONE)(DONE)" ]]; then
375               msgBox "$dup_title" "You cannot create the configuration file until the four first steps are completed."
376            else
377               do_dup_finish
378               break
379            fi
380            ;;
381      esac
382
383   done
384 }
385
386 ### Main function
387
388 dup_wizard() {
389    
390    require_packages duplicity
391
392    # Global variables
393    dup_title="Duplicity action wizard"
394    _src_done=
395    _dest_done=
396    _con_done=
397    _gpg_done=
398    _adv_done=
399    declare -a dup_default_includes
400    declare -a dup_default_excludes
401    declare -a dup_includes
402    declare -a dup_excludes
403    declare -a dup_vsincludes
404    dup_vsnames=
405    dup_incremental=yes
406    dup_keep=60
407    dup_bandwidth=
408    dup_sshoptions=
409    dup_destdir="/backups/`hostname`"
410    dup_desthost=
411    dup_destuser=
412    dup_gpg_sign="yes"
413    dup_gpg_encryptkey=""
414    dup_gpg_password=""
415    dup_nicelevel=19
416    dup_testconnect=yes
417    dup_options=
418
419    # Global variables whose '*' shall not be expanded
420    set -o noglob
421    dup_default_includes=(/var/spool/cron/crontabs /var/backups /etc /root /home /usr/local/*bin /var/lib/dpkg/status*)
422    dup_default_excludes=(/home/*/.gnupg)
423    set +o noglob
424
425    dup_main_menu
426 }