Fixed bug in rdiff.helper that kept include and exclude lines from being put in the...
[matthijs/upstream/backupninja.git] / handlers / rdiff.helper
1
2 HELPERS="$HELPERS rdiff:incremental_remote_filesystem_backup"
3
4 declare -a rdiff_includes
5 declare -a rdiff_excludes
6
7 do_rdiff_dest() {
8   set -o noglob
9   formBegin "rdiff action wizard"
10     formItem "keep" "$rdiff_keep"
11     formItem "dest_directory" "$rdiff_directory"
12     formItem "dest_host" "$rdiff_host"
13     formItem "dest_user" "$rdiff_user"
14   formDisplay
15   [ $? = 1 ] && return;
16
17   IFS=$''
18   replyconverted=`echo $REPLY | tr '\n' :`
19   IFS=$':'
20   thereply=($replyconverted)
21   IFS=$' \t\n'
22   
23   rdiff_keep=${thereply[0]}
24   rdiff_directory=${thereply[1]}
25   rdiff_host=${thereply[2]}
26   rdiff_user=${thereply[3]}
27
28   _dest_done="(DONE)"
29   setDefault conn
30   set +o noglob
31 }
32
33 do_rdiff_src() {
34   #echo ${rdiff_includes[@]}
35   set -o noglob
36   formBegin "rdiff action wizard: includes"
37     for ((i=0; i < ${#rdiff_includes[@]} ; i++)); do
38        formItem include ${rdiff_includes[$i]}
39     done
40     formItem include 
41     formItem include 
42     formItem include 
43   formDisplay
44   [ $? = 1 ] && return;
45
46   unset rdiff_includes
47   rdiff_includes=($REPLY)
48
49   formBegin "rdiff action wizard: excludes" 
50     for ((i=0; i < ${#rdiff_excludes[@]} ; i++)); do
51        formItem exclude ${rdiff_excludes[$i]}
52     done
53     formItem exclude 
54     formItem exclude 
55   formDisplay
56   [ $? = 1 ] && return;
57
58   unset rdiff_excludes
59   rdiff_excludes=($REPLY)
60    
61   _src_done="(DONE)"
62   setDefault dest
63   set +o noglob
64 }
65
66 do_ssh_con() {
67   IFS=$' \t\n'
68   if [ "$_dest_done" = "" ]; then
69     msgBox "rdiff action wizard: error" "You must first configure the destination."
70     return
71   elif [ "$rdiff_user" = "" ]; then
72     msgBox "rdiff action wizard: error" "You must first configure the destination user."
73     return
74   elif [ "$rdiff_host" = "" ]; then
75     msgBox "rdiff action wizard: error" "You must first configure the destination host."
76     return
77   else
78     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.\n\n\nAre you sure you want to continue?"
79     [ $? = 1 ] && return
80   fi
81
82   if [ ! -f /root/.ssh/id_dsa.pub -a ! -f /root/.ssh/id_rsa.pub ]; then
83     echo "Creating local root's ssh key"
84     ssh-keygen -t dsa -f /root/.ssh/id_dsa -N ""
85     echo "Done. hit return to continue"
86     read
87   fi
88   
89   ssh -o PreferredAuthentications=publickey $rdiff_host -l $rdiff_user "exit" 2> /dev/null
90   if [ $? -ne 0 ]; then
91     echo "Copying root's public ssh key to authorized_keys of $rdiff_user@$rdiff_host. When prompted, specify the password for user $rdiff_user@$rdiff_host."
92     ssh-copy-id -i /root/.ssh/id_[rd]sa.pub $rdiff_user@$rdiff_host
93     if [ $? -ne 0 ]; then
94       echo "FAILED: Couldn't copy root's public ssh key to authorized_keys of $rdiff_user@$rdiff_host."
95       ssh $rdiff_user@$rdiff_host 'test -w .ssh || test -w .'
96       result=$?
97       echo "Hit return to continue."
98       read
99       case $result in
100         0 )   msgBox "rdiff action wizard: error" "Directories are writable: Probably just a typo the first time." ;;
101         1 )   msgBox "rdiff action wizard: error" "Connected successfully to $rdiff_user@$rdiff_host, but unable to write. Check ownership and modes of ~$rdiff_user on $rdiff_host." ;;
102         255 ) msgBox "rdiff action wizard: error" "Failed to connect to $rdiff_user@$rdiff_host. Check hostname, username, and password. Also, make sure sshd is running on the destination host." ;;
103         * )   msgBox "rdiff action wizard: error" "Unexpected error." ;;
104       esac 
105       return
106     else
107       echo "Done. hit return to continue"
108       read
109     fi
110   else
111     echo "root@localhost is already in authorized_keys of $rdiff_user@$rdiff_host."
112     echo "Hit return to continue."
113     read
114   fi
115 do_rdiff_con
116 }
117
118 do_rdiff_con() {
119   echo "Testing to make sure destination has rdiff-backup installed and is compatible."
120   remote_result=`rdiff-backup --test-server $rdiff_user@$rdiff_host::/ 2>&1 >&-`
121   if [ $? -ne 0 ]
122   then
123         echo $remote_result | grep -q "command not found"
124         if [ $? -eq 0 ]
125         then
126                 if [ "$rdiff_user" = "root" ]
127                 then
128                         booleanBox "install rdiff-backup?" "It seems like the remote machine does not have rdiff-backup installed, I can attempt to install rdiff-backup on the remote machine.\n\n\nDo you want me to attempt this now?"
129                         if [ $? = 0 ] 
130                         then
131                                 ssh $rdiff_user@$rdiff_host 'apt-get install rdiff-backup'
132                                 result=$?
133                                 echo "Hit return to continue."
134                                 read
135                                 case $result in
136                                         0)   
137                                                 msgBox "rdiff action wizard: success" "Installation of rdiff-backup was a success!" 
138                                                 do_rdiff_con
139                                         ;;
140                                         1)   
141                                                 msgBox "rdiff action wizard: error" "Connected successfully to $rdiff_user@$rdiff_host, but was unable to install the package for some reason." 
142                                         ;;
143                                         255) 
144                                                 msgBox "rdiff action wizard: error" "Failed to connect to $rdiff_user@$rdiff_host. Check hostname, username, and password. Also, make sure sshd is running on the destination host." 
145                                         ;;
146                                         *)   
147                                                 msgBox "rdiff action wizard: error" "Unexpected error." 
148                                         ;;
149                                 esac 
150                                 return
151                         fi
152                 else
153                         booleanBox "install rdiff-backup" "Please install rdiff-backup on the remote machine, this cannot be done automatically, as the remote user in your configuration is not root. \n\nIf you have installed rdiff-backup on the remote machine and you are getting this error, then there is a version incompatibility between that version and the local version.\n\nPlease resolve this problem and then try connecting again.\n\n\n\nTry connecting again?"
154                         if [ $? = 0 ]
155                         then
156                                 do_rdiff_con
157                         else
158                                 return
159                         fi
160                 fi
161         else
162                 msgBox "incompatible versions of rdiff-backup" "It looks like rdiff-backup is installed on the remote machine, but it may be an incompatible version with the one installed locally, or something else is amiss.\n\nPlease resolve this problem and then try connecting again.\n\n\nTry connecting again?"
163                 if [ $? = 0 ]
164                 then
165                         do_rdiff_con
166                 else
167                         return
168                 fi
169         fi
170    else
171         echo "SUCCESS: Everything looks good!"
172         echo "Hit return to continue."
173         read
174    fi
175
176   _con_done="(DONE)"
177   setDefault finish
178 }
179
180 do_rdiff_finish() {
181    get_next_filename $configdirectory/90.rdiff
182    cat > $next_filename <<EOF
183 # options = --force
184 # when = everyday at 02
185
186 [source]
187 type = local
188 keep = $rdiff_keep
189 EOF
190     set -o noglob
191     for ((i=0; i < ${#rdiff_includes[@]} ; i++)); do
192         echo "include = ${rdiff_includes[$i]}" >> $next_filename
193     done
194     for ((i=0; i < ${#rdiff_excludes[@]} ; i++)); do
195         echo exclude = ${rdiff_excludes[$i]} >> $next_filename
196     done
197     set +o noglob
198     cat >> $next_filename <<EOF
199   
200 [dest]
201 type = remote
202 directory = $rdiff_directory
203 host = $rdiff_host
204 user = $rdiff_user
205 EOF
206    chmod 600 $next_filename
207 }
208
209 rdiff_main_menu() {
210
211   while true; do
212     srcitem="choose files to include & exclude $_src_done"
213     destitem="configure backup destination $_dest_done"
214     conitem="set up ssh keys and test remote connection $_con_done"
215     advitem="edit advanced settings $_adv_done"
216     menuBox "rdiff action wizard" "choose a step:" \
217         src "$srcitem" \
218         dest "$destitem" \
219         conn "$conitem" \
220         finish "finish and create config file"
221     [ $? = 1 ] && return;
222     result="$REPLY"
223
224     case "$result" in
225        "src") do_rdiff_src;;
226        "dest") do_rdiff_dest;;
227        "conn") do_ssh_con;;
228        "adv") do_rdiff_adv;;
229        "finish")
230           if [[ "$_con_done$_dest_done$_src_done" != "(DONE)(DONE)(DONE)" ]]; then
231             msgBox "rdiff action wizard" "You cannot create the configuration file until the other steps are completed."
232           else
233             do_rdiff_finish
234             return
235           fi
236           ;;
237     esac
238
239   done
240 }
241
242 rdiff_wizard() {
243   require_packages rdiff-backup
244   _src_done=
245   _dest_done=
246   _con_done=
247   _adv_done=
248   rdiff_keep=60D
249   rdiff_directory=/backup/`hostname`
250   rdiff_user=
251   rdiff_host=
252   rdiff_includes=(/var/spool/cron/crontabs /var/backups /etc /root /home /usr/local/*bin /var/lib/dpkg/status*)
253   rdiff_excludes=(/home/*/.gnupg)
254   rdiff_main_menu
255 }
256