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