made it so that helpers are dynamically defined.
[matthijs/upstream/backupninja.git] / handlers / rdiff.helper
1
2 HELPERS="$HELPERS rdiff:incremental_remote_filesystem_backup"
3
4 do_rdiff_dest() {
5    formBegin "rdiff action wizard"
6      formItem "keep" "$rdiff_keep"
7      formItem "dest_directory" "$rdiff_directory"
8      formItem "dest_host" "$rdiff_host"
9      formItem "dest_user" "$rdiff_user"
10    formDisplay
11    [ $? = 1 ] && return;
12        
13    set -- $REPLY
14         rdiff_keep=$1
15         rdiff_directory=$2
16         rdiff_host=$3
17         rdiff_user=$4
18
19   _dest_done="(DONE)"
20   setDefault conn
21 }
22
23 do_rdiff_src() {
24    formBegin "rdiff action wizard: includes"
25      formItem include /var/spool/cron/crontabs
26      formItem include /var/backups
27      formItem include /etc
28      formItem include /root
29      formItem include /home
30      formItem include '/usr/local/*bin'
31      formItem include '/var/lib/dpkg/status*'
32      formItem include 
33      formItem include 
34      formItem include 
35    formDisplay
36    [ $? = 1 ] && return;
37
38    rdiff_includes=   
39    set -o noglob
40    for i in $REPLY; do
41       [ "$i" != "" ] && rdiff_includes="$rdiff_includes\ninclude = $i"
42    done
43    set +o noglob
44
45    formBegin "rdiff action wizard: excludes" 
46      formItem exclude '/home/*/.gnupg'
47      formItem exclude 
48      formItem exclude 
49    formDisplay
50    [ $? = 1 ] && return;
51
52    rdiff_excludes=
53    set -o noglob
54    for i in $REPLY; do
55       [ "$i" != "" ] && rdiff_excludes="$rdiff_excludes\nexclude = $i"
56    done
57    set +o noglob
58    
59    _src_done="(DONE)"
60    setDefault dest
61 }
62
63 do_rdiff_con() {
64   if [ "$_dest_done" = "" ]; then
65     msgBox "rdiff action wizard: error" "You must first configure the destination"
66     return
67   else
68     booleanBox "rdiff action wizard" "This step will create a ssh key for the local root user with no passphrase (if one does not already exist), and attempt to copy root's public ssh key to authorized_keys file of $rdiff_user@$rdiff_host. This will allow the local root to make unattended backups to $rdiff_user@$rdiff_host. Are you sure you want to continue?"
69     [ $? = 1 ] && return
70   fi
71
72   if [ ! -f /root/.ssh/id_dsa.pub -a ! -f /root/.ssh/id_rsa.pub ]; then
73     echo "Creating local root's ssh key"
74     ssh-keygen -t dsa -f /root/.ssh/id_dsa -N ""
75     echo "Done. hit return to continue"
76     read
77   fi
78   
79   ssh -o PreferredAuthentications=publickey $rdiff_host -l $rdiff_user "exit" 2> /dev/null
80   if [ $? -ne 0 ]; then
81     echo "Copying root's public ssh key to authorized_keys of $rdiff_user@$rdiff_host. Specify the password for user $rdiff_user@$rdiff_host."
82     ssh-copy-id -i /root/.ssh/id_[rd]sa.pub $rdiff_user@$rdiff_host
83     if [ $? -ne 0 ]; then
84       msgBox "rdiff action wizard: error" "Failed to connect to $rdiff_user@$rdiff_host. Make sure you have the username and password correct."
85       return
86     else
87       echo "Done. hit return to continue"
88       read
89     fi
90   else
91     echo "root@localhost is already in authorized_keys of $rdiff_user@$rdiff_host. hit return to continue"
92     read
93   fi
94   _con_done="(DONE)"
95   setDefault finish
96 }
97
98 do_rdiff_finish() {
99    get_next_filename $configdirectory/90.rdiff
100    cat > $next_filename <<EOF
101 # options = --force
102 # when = everyday at 02
103
104 [source]
105 type = local
106 keep = $rdiff_keep
107 EOF
108    echo -n -e "$rdiff_includes" >> $next_filename
109    echo -e "$rdiff_excludes" >> $next_filename
110    cat >> $next_filename <<EOF
111   
112 [dest]
113 type = remote
114 directory = $rdiff_directory
115 host = $rdiff_host
116 user = $rdiff_user
117 EOF
118    chmod 000 $next_filename
119 }
120
121 rdiff_main_menu() {
122   while true; do
123     srcitem="choose files to include & exclude $_src_done"
124     destitem="configure backup destination $_dest_done"
125     conitem="set up ssh keys and test remote connection $_con_done"
126     advitem="edit advanced settings $_adv_done"
127     menuBox "rdiff action wizard" "choose a step:" \
128         src "$srcitem" \
129         dest "$destitem" \
130         conn "$conitem" \
131         finish "finish and create config file"
132     [ $? = 1 ] && return;
133     result="$REPLY"
134     case "$result" in
135        "src") do_rdiff_src;;
136        "dest") do_rdiff_dest;;
137        "conn") do_rdiff_con;;
138        "adv") do_rdiff_adv;;
139        "finish")
140           if [[ "$_con_done$_dest_done$_src_done" != "(DONE)(DONE)(DONE)" ]]; then
141             msgBox "rdiff action wizard" "You cannot create the configuration file until the other steps are completed."
142           else
143             do_rdiff_finish
144             return
145           fi
146           ;;
147     esac
148     
149   done
150 }
151
152 rdiff_wizard() {
153 #   require_packages rdiff-backup
154   _src_done=
155   _dest_done=
156   _con_done=
157   _adv_done=
158   rdiff_keep=60D
159   rdiff_directory=/backup/`hostname`
160   rdiff_user=
161   rdiff_host=
162   rdiff_main_menu
163 }
164