applied patch to rdiff.helper, removed /etc/passwd from mysql handler.
[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       echo "Couldn't copy root's public ssh key to authorized_keys of $rdiff_user@$rdiff_host. This time, testing whether directory is writable."
85       ssh $rdiff_user@$rdiff_host 'test -w .ssh || test -w .'
86       case $? in
87         0 )   msgBox "rdiff action wizard: error" "Directories are writable: Probably just a typo the first time." ;;
88         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." ;;
89         255 ) msgBox "rdiff action wizard: error" "Failed to connect to $rdiff_user@$rdiff_host. Check hostname, username, and password." ;;
90         * )   msgBox "rdiff action wizard: error" "Unexpected error." ;;
91       esac 
92       return
93     else
94       echo "Done. hit return to continue"
95       read
96     fi
97   else
98     echo "root@localhost is already in authorized_keys of $rdiff_user@$rdiff_host. hit return to continue"
99     read
100   fi
101   _con_done="(DONE)"
102   setDefault finish
103 }
104
105 do_rdiff_finish() {
106    get_next_filename $configdirectory/90.rdiff
107    cat > $next_filename <<EOF
108 # options = --force
109 # when = everyday at 02
110
111 [source]
112 type = local
113 keep = $rdiff_keep
114 EOF
115    echo -n -e "$rdiff_includes" >> $next_filename
116    echo -e "$rdiff_excludes" >> $next_filename
117    cat >> $next_filename <<EOF
118   
119 [dest]
120 type = remote
121 directory = $rdiff_directory
122 host = $rdiff_host
123 user = $rdiff_user
124 EOF
125    chmod 000 $next_filename
126 }
127
128 rdiff_main_menu() {
129   while true; do
130     srcitem="choose files to include & exclude $_src_done"
131     destitem="configure backup destination $_dest_done"
132     conitem="set up ssh keys and test remote connection $_con_done"
133     advitem="edit advanced settings $_adv_done"
134     menuBox "rdiff action wizard" "choose a step:" \
135         src "$srcitem" \
136         dest "$destitem" \
137         conn "$conitem" \
138         finish "finish and create config file"
139     [ $? = 1 ] && return;
140     result="$REPLY"
141     case "$result" in
142        "src") do_rdiff_src;;
143        "dest") do_rdiff_dest;;
144        "conn") do_rdiff_con;;
145        "adv") do_rdiff_adv;;
146        "finish")
147           if [[ "$_con_done$_dest_done$_src_done" != "(DONE)(DONE)(DONE)" ]]; then
148             msgBox "rdiff action wizard" "You cannot create the configuration file until the other steps are completed."
149           else
150             do_rdiff_finish
151             return
152           fi
153           ;;
154     esac
155     
156   done
157 }
158
159 rdiff_wizard() {
160 #   require_packages rdiff-backup
161   _src_done=
162   _dest_done=
163   _con_done=
164   _adv_done=
165   rdiff_keep=60D
166   rdiff_directory=/backup/`hostname`
167   rdiff_user=
168   rdiff_host=
169   rdiff_main_menu
170 }
171