r3562@krups: intrigeri | 2005-11-16 20:20:16 +0100
[matthijs/upstream/backupninja.git] / src / ninjahelper.in
1 #!@BASH@
2
3 ####################################################
4 ## Functions
5
6 function check_perms() {
7         local file=$1
8         local perms=`ls -ld $file`
9         group_w_perm=${perms:5:1}
10         world_w_perm=${perms:8:1}
11         if [ "$group_w_perm" == "w" -o "$world_w_perm" == "w" ]; then
12             echo $perms
13             echo "helper scripts must not be group or world writable! Dying on file $file"
14             exit
15         fi
16         if [ `ls -ld $file | awk '{print $3}'` != "root" ]; then
17                 echo "helper scripts must be owned by root! Dying on file $file"
18                 exit
19         fi
20 }
21
22
23 ##
24 ## returns the next available file name given a file
25 ## in the form @CFGDIR@/backup.d/10.sys
26 ## sets variable $next_filename
27 ##
28 get_next_filename() {
29   next_filename=$1
30   dir=`dirname $next_filename`
31   file=`basename $next_filename`
32   number=${file:0:2}
33   suffix=${file:3}
34   while [ -f $next_filename ]; do
35     let "number += 1"
36     next_filename="$dir/$number.$suffix"
37   done
38 }
39
40 ##
41 ## installs packages (passed in as $@) if not present
42 ##
43 require_packages() {
44        for pkg in "$@"; do
45            installed=`dpkg -s $pkg | grep 'ok installed'`
46        if [ -z "$installed" ]; then 
47            booleanBox "install $pkg?" "This backup action requires package $pkg. Do you want to install it now?"
48            if [ $? = 0 ]; then
49               apt-get install $pkg
50               echo "hit return to continue...."
51               read
52            fi
53        fi
54     done
55 }
56
57 ##
58 ## menu for the wizards
59 ##
60 donew() {
61   unset host_or_vservers
62   unset vservers_chooser_vsnames
63   listBegin "new action menu" "select an action to create"
64   listItem return "return to main menu"
65   for data in $HELPERS; do
66     data=${data//_/ }
67     helper_function=${data%%:*}
68     helper_info=${data##*:}
69     listItem $helper_function "$helper_info"
70   done
71   listDisplay menu
72     
73   [ $? = 1 ] && return
74   result="$REPLY"
75   [ "$result" = "return" ] && return
76   result=${result}_wizard
77   $result
78 }
79
80 do_rm_action() {
81    booleanBox "remove action" "Are you sure you want to remove action file $1?"
82    if [ $? = 0 ]; then
83      rm $1;
84    fi
85 }
86
87 do_run() {
88    backupninja --run $1
89    echo "Hit return to continue..."
90    read
91 }
92
93 do_xedit() {
94    if [ -z "$EDITOR" -o ! -x "`which $EDITOR`" ]; then
95       if [ -h /etc/alternatives/editor -a -x "`readlink /etc/alternatives/editor`" ]; then
96          EDITOR="`readlink /etc/alternatives/editor`"
97       elif [ -x "`which nano`" ]; then
98          EDITOR="`which nano`"
99       elif [ -x "`which vim`" ]; then
100          EDITOR="`which vim`"
101       elif [ -x "`which vi`" ]; then
102          EDITOR="`which vi`"
103       else
104          echo "No suitable editor found."
105          echo "Please define $EDITOR or configure /etc/alternatives/editor."
106          exit
107       fi
108    fi
109    $EDITOR $1
110 }
111
112 do_run_test() {
113   backupninja --test --run $1
114   echo "Hit return to continue..."
115   read
116 }
117
118 do_disable() {
119   mv $1 $1.disabled
120 }
121
122 do_enable() {
123   mv $1 ${1%.*}
124 }
125
126 do_rename() {
127   dir=`dirname $1`
128   filename=`basename $1`
129   inputBox "rename action" "enter a new filename" $filename
130   mv $dir/$filename $dir/$REPLY
131 }
132
133 doaction() {
134   action=$1
135   base=`basename $action`
136   if [ "${base##*.}" == "disabled" ]; then
137      enable="enable";
138   else
139      enable="disable";
140   fi
141   while true; do
142     menuBox "action menu" "$action $first" \
143       main "return to main menu" \
144       view "view configuration" \
145       xedit "launch external editor" \
146       $enable "$enable action" \
147       name "change the filename" \
148       run "run this action now" \
149       test "do a test run" \
150       kill "remove this action" 
151     [ $? = 1 ] && return;
152     result="$REPLY"
153         case "$result" in
154            "view") dialog --textbox $action 0 0;; 
155            "xedit") do_xedit $action;;
156            "disable") do_disable $action; return;;
157            "enable") do_enable $action; return;;
158            "name") do_rename $action; return;;
159            "run") do_run $action;;
160            "test") do_run_test $action;; 
161            "kill") do_rm_action $action; return;;
162            "main") return;;
163         esac
164   done
165 }
166
167 #####################################################
168 ## VSERVERS RELATED FUNCTIONS
169
170 ##
171 ## If vservers are not enabled, exit silently and set host_or_vservers to 'host'.
172 ## Else, have the user choose the target he/she wants to perform the backup on:
173 ##   - host system only
174 ##   - some vservers only
175 ##   - both the host system and some vservers
176 ## Sets, respectively, $host_or_vservers to 'host', 'vservers', or 'both'
177 ## $host_or_vservers is unset when a new helper is run.
178 ## Returns 1 if cancelled.
179 ##
180 host_or_vservers_chooser() {
181    local title=$1
182    # exit silently if vservers are not enabled
183    if [ "$vservers" != "yes" ]; then
184       host_or_vservers='host'
185       return
186    fi
187    # if there is one, set the previously chosen item as the default
188    [ -n "$host_or_vservers" ] && setDefault $host_or_vservers
189    menuBox "$title - src" "Do you want to operate on the host system and/or on vservers?" \
190       "host" "Host system only" \
191       "vservers" "Vservers only" \
192       "both" "Host system and Vservers"
193    [ $? = 0 ] || return 1
194    case $REPLY in
195       "host")
196          host_or_vservers='host'
197          ;;
198       "vservers")
199          host_or_vservers='vservers'
200          ;;
201       "both")
202          host_or_vservers='both'
203          ;;
204    esac
205 }
206
207 ##
208 ## If the argument is the name of a vserver selected for backup (in
209 ## $vservers_chooser_vsnames), echoes 'on' and returns 0.
210 ## Else, echoes 'off' and returns 1.
211 ##
212 vserver_is_selected() {
213    local vserver=$1
214    local vserver_is_selected=1
215    local i
216    for i in $vservers_chooser_vsnames ; do
217       [ "$vserver" == "$i" ] && vserver_is_selected=0
218    done
219    if [ $vserver_is_selected = 0 ]; then
220       echo on
221    else
222       echo off
223    fi
224    return $vserver_is_selected
225 }
226
227 ##
228 ## Have the user choose among "all vservers" and a not-empty subset of these.
229 ## Sets global $vservers_chooser_vsnames variable to "all" or to a
230 ## space-separated name list.
231 ## Depends on host_or_vservers() to have already run.
232 ## $vservers_chooser_vsnames is unset when a new helper is run.
233 ## Returns 1 if cancelled.
234 ##
235 vservers_chooser() {
236    local title=$1
237    local i=
238    [ -n "$VROOTDIR" ] || (msgBox "warning" "VROOTDIR is not set in $conffile and could not be guessed."; return 1)
239    [ -d "$VROOTDIR" ] || (msgBox "warning" "VROOTDIR ($VROOTDIR) does not exist."; return 1)
240
241    booleanBox "$title" "Do you want to backup all vservers?" ` [ -z "$vservers_chooser_vsnames" -o "$vservers_chooser_vsnames" == "all" ] || echo no`
242    if [ $? = 0 ]; then
243       vservers_chooser_vsnames="all"
244    else
245       # choose among the existing vservers
246       local vserver=
247       local vserver_was_selected=
248       REPLY=
249       while [ -z "$REPLY" ]; do
250          listBegin "$title" "Choose at least one Linux-Vserver to backup:"
251             # list existing vservers, preselecting the previously selected ones
252             for vserver in `ls $VROOTDIR | grep -E -v "lost+found|ARCHIVES"`; do
253                listItem "$vserver" "Backup $vserver vserver" `vserver_is_selected $vserver`
254             done
255          listDisplay checklist
256          [ $? = 0 ] || return 1
257       done
258       # remove quotes around each vserver name
259       vservers_chooser_vsnames=`echo $REPLY | tr -d '"'`
260    fi
261 }
262
263 #####################################################
264 ## begin program
265
266 if [ ! -x "`which dialog`" ]; then
267         echo "ninjahelper is a menu based wizard for backupninja."
268         echo "It requires 'dialog' in order to run. Do you want to install dialog now?"
269         while true; do
270           echo -n "(yes/no): "
271           read install
272           if [ "$install" == "yes" ]; then
273              apt-get install dialog
274              break
275           elif [ "$install" == "no" ]; then
276              exit
277           else
278              echo "You must answer 'yes' or 'no'"
279           fi
280     done
281 fi
282
283 # bootstrap
284 conffile="@CFGDIR@/backupninja.conf"
285 if [ ! -r "$conffile" ]; then
286         echo "Configuration file $conffile not found." 
287         exit 1
288 fi
289
290 # find $libdirectory
291 libdirectory=`grep '^libdirectory' $conffile | awk '{print $3}'`
292 if [ -z "$libdirectory" ]; then
293         if [ -d "@libdir@" ]; then
294            libdirectory="@libdir@"
295         else
296            echo "Could not find entry 'libdirectory' in $conffile." 
297            exit 1
298         fi
299 else
300         if [ ! -d "$libdirectory" ]; then
301            echo "Lib directory $libdirectory not found." 
302            exit 1
303         fi
304 fi
305
306 # include shared functions
307 . $libdirectory/easydialog
308 . $libdirectory/tools
309
310 # am I running as root?
311 if [ "$UID" != "0" ]; then
312         msgBox "warning" "$0 must be run by root!"
313         exit 1
314 fi
315
316 # get global config options (second param is the default)
317 setfile $conffile
318 getconf configdirectory @CFGDIR@/backup.d
319 getconf scriptdirectory @datadir@
320 getconf vservers no
321 getconf VSERVERINFO /usr/sbin/vserver-info
322 getconf VSERVER /usr/sbin/vserver
323 getconf VROOTDIR `if [ -f "$VSERVERINFO" ]; then $VSERVERINFO info SYSINFO |grep vserver-Rootdir | awk '{print $2}'; fi`
324
325 # load all the helpers
326 HELPERS=""
327 for file in `find $scriptdirectory -follow -name '*.helper'`; do
328    check_perms $file
329    . $file
330 done
331
332 setApplicationTitle "ninjahelper"
333 setDimension 75 19
334
335 #####################################################
336 ## main event loop
337
338 while true; do
339
340 menulist=
341 action=
342 let "i = 1"
343 for file in `find $conf/etc/backup.d/ -type f | sort -n`; do
344   menulist="$menulist $i $file"
345   actions[$i]=$file
346   let "i += 1"
347 done
348
349 menuBox "main menu" "Select a backup action for more options, or create a new action:" $menulist \
350   new "create a new backup action" \
351   quit "leave ninjahelper" 
352
353 [ $? = 1 -o $? = 255 ] && exit 0;
354
355 choice="$REPLY"
356 if [ "$choice" == "new" ]; then
357   donew;
358 elif [ "$choice" == "quit" ]; then
359   exit 0;
360 else
361   action=${actions[$choice]};
362   if [ -f "$action" ]; then
363      doaction $action
364   else
365      msgBox "error" "error: cannot find the file '$action'"
366   fi
367 fi
368
369
370 done