Fixed broken toint function, causes backups to not run from cron
[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   # (re-)initialize vservers support
62   init_vservers
63   # menu
64   listBegin "new action menu" "select an action to create"
65   listItem return "return to main menu"
66   for data in $HELPERS; do
67     data=${data//_/ }
68     helper_function=${data%%:*}
69     helper_info=${data##*:}
70     listItem $helper_function "$helper_info"
71   done
72   listDisplay menu
73     
74   [ $? = 1 ] && return
75   result="$REPLY"
76   [ "$result" = "return" ] && return
77   result=${result}_wizard
78   $result
79 }
80
81 do_rm_action() {
82    booleanBox "remove action" "Are you sure you want to remove action file $1?"
83    if [ $? = 0 ]; then
84      rm $1;
85    fi
86 }
87
88 do_run() {
89    backupninja --run $1
90    echo "Hit return to continue..."
91    read
92 }
93
94 do_xedit() {
95    if [ -z "$EDITOR" -o ! -x "`which $EDITOR`" ]; then
96       if [ -h /etc/alternatives/editor -a -x "`readlink /etc/alternatives/editor`" ]; then
97          EDITOR="`readlink /etc/alternatives/editor`"
98       elif [ -x "`which nano`" ]; then
99          EDITOR="`which nano`"
100       elif [ -x "`which vim`" ]; then
101          EDITOR="`which vim`"
102       elif [ -x "`which vi`" ]; then
103          EDITOR="`which vi`"
104       else
105          echo "No suitable editor found."
106          echo "Please define $EDITOR or configure /etc/alternatives/editor."
107          exit
108       fi
109    fi
110    $EDITOR $1
111 }
112
113 do_run_test() {
114   backupninja --test --run $1
115   echo "Hit return to continue..."
116   read
117 }
118
119 do_disable() {
120   mv $1 $1.disabled
121 }
122
123 do_enable() {
124   mv $1 ${1%.*}
125 }
126
127 do_rename() {
128   dir=`dirname $1`
129   filename=`basename $1`
130   inputBox "rename action" "enter a new filename" $filename
131   mv $dir/$filename $dir/$REPLY
132 }
133
134 doaction() {
135   action=$1
136   base=`basename $action`
137   if [ "${base##*.}" == "disabled" ]; then
138      enable="enable";
139   else
140      enable="disable";
141   fi
142   while true; do
143     menuBox "action menu" "$action $first" \
144       main "return to main menu" \
145       view "view configuration" \
146       xedit "launch external editor" \
147       $enable "$enable action" \
148       name "change the filename" \
149       run "run this action now" \
150       test "do a test run" \
151       kill "remove this action" 
152     [ $? = 1 ] && return;
153     result="$REPLY"
154         case "$result" in
155            "view") dialog --textbox $action 0 0;; 
156            "xedit") do_xedit $action;;
157            "disable") do_disable $action; return;;
158            "enable") do_enable $action; return;;
159            "name") do_rename $action; return;;
160            "run") do_run $action;;
161            "test") do_run_test $action;; 
162            "kill") do_rm_action $action; return;;
163            "main") return;;
164         esac
165   done
166 }
167
168 #####################################################
169 ## begin program
170
171 if [ ! -x "`which dialog`" ]; then
172         echo "ninjahelper is a menu based wizard for backupninja."
173         echo "It requires 'dialog' in order to run. Do you want to install dialog now?"
174         while true; do
175           echo -n "(yes/no): "
176           read install
177           if [ "$install" == "yes" ]; then
178              apt-get install dialog
179              break
180           elif [ "$install" == "no" ]; then
181              exit
182           else
183              echo "You must answer 'yes' or 'no'"
184           fi
185     done
186 fi
187
188 # bootstrap
189 conffile="@CFGDIR@/backupninja.conf"
190 if [ ! -r "$conffile" ]; then
191         echo "Configuration file $conffile not found." 
192         exit 1
193 fi
194
195 # find $libdirectory
196 libdirectory=`grep '^libdirectory' $conffile | awk '{print $3}'`
197 if [ -z "$libdirectory" ]; then
198         if [ -d "@libdir@" ]; then
199            libdirectory="@libdir@"
200         else
201            echo "Could not find entry 'libdirectory' in $conffile." 
202            exit 1
203         fi
204 else
205         if [ ! -d "$libdirectory" ]; then
206            echo "Lib directory $libdirectory not found." 
207            exit 1
208         fi
209 fi
210
211 # include shared functions
212 . $libdirectory/easydialog
213 . $libdirectory/tools
214 . $libdirectory/vserver
215
216 # am I running as root?
217 if [ "$UID" != "0" ]; then
218         msgBox "warning" "`basename $0` must be run by root!"
219         exit 1
220 fi
221
222 # get global config options (second param is the default)
223 setfile $conffile
224 getconf configdirectory @CFGDIR@/backup.d
225 getconf scriptdirectory @datadir@
226
227 # load all the helpers
228 HELPERS=""
229 for file in `find $scriptdirectory -follow -name '*.helper'`; do
230    check_perms $file
231    . $file
232 done
233
234 setApplicationTitle "ninjahelper"
235 setDimension 75 19
236
237 #####################################################
238 ## main event loop
239
240 while true; do
241
242 menulist=
243 action=
244 let "i = 1"
245 for file in `find $conf/etc/backup.d/ -type f | sort -n`; do
246   menulist="$menulist $i $file"
247   actions[$i]=$file
248   let "i += 1"
249 done
250
251 menuBox "main menu" "Select a backup action for more options, or create a new action:" $menulist \
252   new "create a new backup action" \
253   quit "leave ninjahelper" 
254
255 [ $? = 1 -o $? = 255 ] && exit 0;
256
257 choice="$REPLY"
258 if [ "$choice" == "new" ]; then
259   donew;
260 elif [ "$choice" == "quit" ]; then
261   exit 0;
262 else
263   action=${actions[$choice]};
264   if [ -f "$action" ]; then
265      doaction $action
266   else
267      msgBox "error" "error: cannot find the file '$action'"
268   fi
269 fi
270
271
272 done