Made ninjahelper create files in /etc/backup.d with mode 600 instead of 000
[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_rdiff_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. Are 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. hit return to continue"
112     read
113   fi
114   _con_done="(DONE)"
115   setDefault finish
116 }
117
118 do_rdiff_finish() {
119    get_next_filename $configdirectory/90.rdiff
120    cat > $next_filename <<EOF
121 # options = --force
122 # when = everyday at 02
123
124 [source]
125 type = local
126 keep = $rdiff_keep
127 EOF
128     set -o noglob
129     for ((i=0; i < ${#rdiff_includes[@]} ; i++)); do
130         echo include = ${rdiff_includes[$i]}
131     done
132     for ((i=0; i < ${#rdiff_includes[@]} ; i++)); do
133         echo exclude = ${rdiff_excludes[$i]}
134     done
135     set +o noglob
136     cat >> $next_filename <<EOF
137   
138 [dest]
139 type = remote
140 directory = $rdiff_directory
141 host = $rdiff_host
142 user = $rdiff_user
143 EOF
144    chmod 600 $next_filename
145 }
146
147 rdiff_main_menu() {
148
149   while true; do
150     srcitem="choose files to include & exclude $_src_done"
151     destitem="configure backup destination $_dest_done"
152     conitem="set up ssh keys and test remote connection $_con_done"
153     advitem="edit advanced settings $_adv_done"
154     menuBox "rdiff action wizard" "choose a step:" \
155         src "$srcitem" \
156         dest "$destitem" \
157         conn "$conitem" \
158         finish "finish and create config file"
159     [ $? = 1 ] && return;
160     result="$REPLY"
161
162     case "$result" in
163        "src") do_rdiff_src;;
164        "dest") do_rdiff_dest;;
165        "conn") do_rdiff_con;;
166        "adv") do_rdiff_adv;;
167        "finish")
168           if [[ "$_con_done$_dest_done$_src_done" != "(DONE)(DONE)(DONE)" ]]; then
169             msgBox "rdiff action wizard" "You cannot create the configuration file until the other steps are completed."
170           else
171             do_rdiff_finish
172             return
173           fi
174           ;;
175     esac
176
177   done
178 }
179
180 rdiff_wizard() {
181   require_packages rdiff-backup
182   _src_done=
183   _dest_done=
184   _con_done=
185   _adv_done=
186   rdiff_keep=60D
187   rdiff_directory=/backup/`hostname`
188   rdiff_user=
189   rdiff_host=
190   rdiff_includes=(/var/spool/cron/crontabs /var/backups /etc /root /home /usr/local/*bin /var/lib/dpkg/status*)
191   rdiff_excludes=(/home/*/.gnupg)
192   rdiff_main_menu
193 }
194