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