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