3 ####################################################
6 function check_perms() {
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
13 echo "helper scripts must not be group or world writable! Dying on file $file"
16 if [ `ls -ld $file | awk '{print $3}'` != "root" ]; then
17 echo "helper scripts must be owned by root! Dying on file $file"
24 ## returns the next available file name given a file
25 ## in the form @CFGDIR@/backup.d/10.sys
26 ## sets variable $next_filename
30 dir=`dirname $next_filename`
31 file=`basename $next_filename`
34 while [ -f $next_filename ]; do
36 next_filename="$dir/$number.$suffix"
41 ## installs packages (passed in as $@) if not present
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?"
50 echo "hit return to continue...."
58 ## menu for the wizards
61 # reset some variables
62 unset host_or_vservers
63 unset vservers_chooser_vsnames
65 listBegin "new action menu" "select an action to create"
66 listItem return "return to main menu"
67 for data in $HELPERS; do
69 helper_function=${data%%:*}
70 helper_info=${data##*:}
71 listItem $helper_function "$helper_info"
77 [ "$result" = "return" ] && return
78 result=${result}_wizard
83 booleanBox "remove action" "Are you sure you want to remove action file $1?"
91 echo "Hit return to continue..."
96 if [ -z "$EDITOR" -o ! -x "`which $EDITOR`" ]; then
97 if [ -h /etc/alternatives/editor -a -x "`readlink /etc/alternatives/editor`" ]; then
98 EDITOR="`readlink /etc/alternatives/editor`"
99 elif [ -x "`which nano`" ]; then
100 EDITOR="`which nano`"
101 elif [ -x "`which vim`" ]; then
103 elif [ -x "`which vi`" ]; then
106 echo "No suitable editor found."
107 echo "Please define $EDITOR or configure /etc/alternatives/editor."
115 backupninja --test --run $1
116 echo "Hit return to continue..."
130 filename=`basename $1`
131 inputBox "rename action" "enter a new filename" $filename
132 mv $dir/$filename $dir/$REPLY
137 base=`basename $action`
138 if [ "${base##*.}" == "disabled" ]; then
144 menuBox "action menu" "$action $first" \
145 main "return to main menu" \
146 view "view configuration" \
147 xedit "launch external editor" \
148 $enable "$enable action" \
149 name "change the filename" \
150 run "run this action now" \
151 test "do a test run" \
152 kill "remove this action"
153 [ $? = 1 ] && return;
156 "view") dialog --textbox $action 0 0;;
157 "xedit") do_xedit $action;;
158 "disable") do_disable $action; return;;
159 "enable") do_enable $action; return;;
160 "name") do_rename $action; return;;
161 "run") do_run $action;;
162 "test") do_run_test $action;;
163 "kill") do_rm_action $action; return;;
169 #####################################################
170 ## VSERVERS RELATED FUNCTIONS
173 ## If vservers are not enabled, exit silently and set host_or_vservers to 'host'.
174 ## Else, have the user choose the target he/she wants to perform the backup on:
175 ## - host system only
176 ## - some vservers only
177 ## - both the host system and some vservers
178 ## Sets, respectively, $host_or_vservers to 'host', 'vservers', or 'both'
179 ## $host_or_vservers is unset when a new helper is run.
180 ## Returns 1 if cancelled.
182 host_or_vservers_chooser() {
184 # exit silently if vservers are not enabled
185 if [ "$vservers" != "yes" ]; then
186 host_or_vservers='host'
189 # if there is one, set the previously chosen item as the default
190 [ -n "$host_or_vservers" ] && setDefault $host_or_vservers
191 menuBox "$title - src" "Do you want to operate on the host system and/or on vservers?" \
192 "host" "Host system only" \
193 "vservers" "Vservers only" \
194 "both" "Host system and Vservers"
195 [ $? = 0 ] || return 1
198 host_or_vservers='host'
201 host_or_vservers='vservers'
204 host_or_vservers='both'
210 ## If the argument is the name of a vserver selected for backup (in
211 ## $vservers_chooser_vsnames), echoes 'on' and returns 0.
212 ## Else, echoes 'off' and returns 1.
214 vserver_is_selected() {
216 local vserver_is_selected=1
218 for i in $vservers_chooser_vsnames ; do
219 [ "$vserver" == "$i" ] && vserver_is_selected=0
221 if [ $vserver_is_selected = 0 ]; then
226 return $vserver_is_selected
230 ## Have the user choose among "all vservers" and a not-empty subset of these.
231 ## Sets global $vservers_chooser_vsnames variable to "all" or to a
232 ## space-separated name list.
233 ## Depends on host_or_vservers() to have already run.
234 ## $vservers_chooser_vsnames is unset when a new helper is run.
235 ## Returns 1 if cancelled.
240 [ -n "$VROOTDIR" ] || (msgBox "warning" "VROOTDIR is not set in $conffile and could not be guessed."; return 1)
241 [ -d "$VROOTDIR" ] || (msgBox "warning" "VROOTDIR ($VROOTDIR) does not exist."; return 1)
243 booleanBox "$title" "Do you want to backup all vservers?" ` [ -z "$vservers_chooser_vsnames" -o "$vservers_chooser_vsnames" == "all" ] || echo no`
245 vservers_chooser_vsnames="all"
247 # choose among the existing vservers
249 local vserver_was_selected=
251 while [ -z "$REPLY" ]; do
252 listBegin "$title" "Choose at least one Linux-Vserver to backup:"
253 # list existing vservers, preselecting the previously selected ones
254 for vserver in `ls $VROOTDIR | grep -E -v "lost+found|ARCHIVES"`; do
255 listItem "$vserver" "Backup $vserver vserver" `vserver_is_selected $vserver`
257 listDisplay checklist
258 [ $? = 0 ] || return 1
260 # remove quotes around each vserver name
261 vservers_chooser_vsnames=`echo $REPLY | tr -d '"'`
265 #####################################################
268 if [ ! -x "`which dialog`" ]; then
269 echo "ninjahelper is a menu based wizard for backupninja."
270 echo "It requires 'dialog' in order to run. Do you want to install dialog now?"
274 if [ "$install" == "yes" ]; then
275 apt-get install dialog
277 elif [ "$install" == "no" ]; then
280 echo "You must answer 'yes' or 'no'"
286 conffile="@CFGDIR@/backupninja.conf"
287 if [ ! -r "$conffile" ]; then
288 echo "Configuration file $conffile not found."
293 libdirectory=`grep '^libdirectory' $conffile | awk '{print $3}'`
294 if [ -z "$libdirectory" ]; then
295 if [ -d "@libdir@" ]; then
296 libdirectory="@libdir@"
298 echo "Could not find entry 'libdirectory' in $conffile."
302 if [ ! -d "$libdirectory" ]; then
303 echo "Lib directory $libdirectory not found."
308 # include shared functions
309 . $libdirectory/easydialog
310 . $libdirectory/tools
312 # am I running as root?
313 if [ "$UID" != "0" ]; then
314 msgBox "warning" "$0 must be run by root!"
318 # get global config options (second param is the default)
320 getconf configdirectory @CFGDIR@/backup.d
321 getconf scriptdirectory @datadir@
323 getconf VSERVERINFO /usr/sbin/vserver-info
324 getconf VSERVER /usr/sbin/vserver
325 getconf VROOTDIR `if [ -f "$VSERVERINFO" ]; then $VSERVERINFO info SYSINFO |grep vserver-Rootdir | awk '{print $2}'; fi`
327 # load all the helpers
329 for file in `find $scriptdirectory -follow -name '*.helper'`; do
334 setApplicationTitle "ninjahelper"
337 #####################################################
345 for file in `find $conf/etc/backup.d/ -type f | sort -n`; do
346 menulist="$menulist $i $file"
351 menuBox "main menu" "Select a backup action for more options, or create a new action:" $menulist \
352 new "create a new backup action" \
353 quit "leave ninjahelper"
355 [ $? = 1 -o $? = 255 ] && exit 0;
358 if [ "$choice" == "new" ]; then
360 elif [ "$choice" == "quit" ]; then
363 action=${actions[$choice]};
364 if [ -f "$action" ]; then
367 msgBox "error" "error: cannot find the file '$action'"