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 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
67 helper_function=${data%%:*}
68 helper_info=${data##*:}
69 listItem $helper_function "$helper_info"
75 [ "$result" = "return" ] && return
76 result=${result}_wizard
81 booleanBox "remove action" "Are you sure you want to remove action file $1?"
89 echo "Hit return to continue..."
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
99 elif [ -x "`which vim`" ]; then
101 elif [ -x "`which vi`" ]; then
104 echo "No suitable editor found."
105 echo "Please define $EDITOR or configure /etc/alternatives/editor."
113 backupninja --test --run $1
114 echo "Hit return to continue..."
128 filename=`basename $1`
129 inputBox "rename action" "enter a new filename" $filename
130 mv $dir/$filename $dir/$REPLY
135 base=`basename $action`
136 if [ "${base##*.}" == "disabled" ]; then
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;
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;;
167 #####################################################
168 ## VSERVERS RELATED FUNCTIONS
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.
180 host_or_vservers_chooser() {
182 # exit silently if vservers are not enabled
183 if [ "$vservers" != "yes" ]; then
184 host_or_vservers='host'
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
196 host_or_vservers='host'
199 host_or_vservers='vservers'
202 host_or_vservers='both'
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.
212 vserver_is_selected() {
214 local vserver_is_selected=1
216 for i in $vservers_chooser_vsnames ; do
217 [ "$vserver" == "$i" ] && vserver_is_selected=0
219 if [ $vserver_is_selected = 0 ]; then
224 return $vserver_is_selected
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.
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)
241 booleanBox "$title" "Do you want to backup all vservers?" ` [ -z "$vservers_chooser_vsnames" -o "$vservers_chooser_vsnames" == "all" ] || echo no`
243 vservers_chooser_vsnames="all"
245 # choose among the existing vservers
247 local vserver_was_selected=
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`
255 listDisplay checklist
256 [ $? = 0 ] || return 1
258 # remove quotes around each vserver name
259 vservers_chooser_vsnames=`echo $REPLY | tr -d '"'`
263 #####################################################
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?"
272 if [ "$install" == "yes" ]; then
273 apt-get install dialog
275 elif [ "$install" == "no" ]; then
278 echo "You must answer 'yes' or 'no'"
284 conffile="@CFGDIR@/backupninja.conf"
285 if [ ! -r "$conffile" ]; then
286 echo "Configuration file $conffile not found."
291 libdirectory=`grep '^libdirectory' $conffile | awk '{print $3}'`
292 if [ -z "$libdirectory" ]; then
293 if [ -d "@libdir@" ]; then
294 libdirectory="@libdir@"
296 echo "Could not find entry 'libdirectory' in $conffile."
300 if [ ! -d "$libdirectory" ]; then
301 echo "Lib directory $libdirectory not found."
306 # include shared functions
307 . $libdirectory/easydialog
308 . $libdirectory/tools
310 # am I running as root?
311 if [ "$UID" != "0" ]; then
312 msgBox "warning" "$0 must be run by root!"
316 # get global config options (second param is the default)
318 getconf configdirectory @CFGDIR@/backup.d
319 getconf scriptdirectory @datadir@
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`
325 # load all the helpers
327 for file in `find $scriptdirectory -follow -name '*.helper'`; do
332 setApplicationTitle "ninjahelper"
335 #####################################################
343 for file in `find $conf/etc/backup.d/ -type f | sort -n`; do
344 menulist="$menulist $i $file"
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"
353 [ $? = 1 -o $? = 255 ] && exit 0;
356 if [ "$choice" == "new" ]; then
358 elif [ "$choice" == "quit" ]; then
361 action=${actions[$choice]};
362 if [ -f "$action" ]; then
365 msgBox "error" "error: cannot find the file '$action'"