greatly improved the rdiff wizard. added default option to menu in easydialog.
[matthijs/upstream/backupninja.git] / ninjahelper
1 #!/bin/bash
2
3 ####################################################
4 ## Functions
5
6 ##
7 ## returns the next available file name given a file
8 ## in the form /etc/backup.d/10.sys
9 ## sets variable $returned_filename
10 ##
11 get_next_filename() {
12   next_filename=$1
13   dir=`dirname $next_filename`
14   file=`basename $next_filename`
15   number=${file:0:2}
16   suffix=${file:3}
17   while [ -f $next_filename ]; do
18     let "number += 1"
19     next_filename="$dir/$number.$suffix"
20   done
21 }
22
23 ##
24 ## installs packages (passed in as $@) if not present
25 ##
26 require_packages() {
27         for pkg in "$@"; do
28            installed=`dpkg -s $pkg | grep 'ok installed'`
29        if [ -z "$installed" ]; then 
30            booleanBox "install $pkg?" "This backup action requires package $pkg. Do you want to install it now?"
31            if [ $? = 0 ]; then
32               apt-get install $pkg
33               echo "hit return to continue...."
34               read
35            fi
36        fi
37     done
38 }
39
40 doradiobox() {
41         defaultchoice="red is.pretty on"
42         choices="green is_nice_too off blue i_love_blue off yellow is.bright off orange make.me.hungry off"
43         radioBox "radio title" "choose one color" $defaultchoice $choices
44         case $? in
45            0) ;;
46            1) echo "color choice cancelled..."; sleep 1;;
47            255) echo "something went wrong, exiting..."
48               exit 1 ;;
49         esac
50         result="$REPLY"
51         msgBox "message title" "you said $result."
52 }
53
54 donew() {
55     menuBox "new action menu" "select an action to create"  \
56       return "return to main menu" \
57       sys "general hardware and system info" \
58       mysql "mysql database backup" \
59       ldap "ldap database backup" \
60       rdiff "incremental filesystem backup"
61   
62     [ $? = 1 ] && return;
63     result="$REPLY"
64         case "$result" in
65            "sys") sys_wizard;; 
66            "mysql") mysql_wizard;;
67            "ldap") ldap_wizard;;
68            "rdiff") rdiff_wizard;;
69            "return") return;;
70         esac
71 }
72
73 do_rm_action() {
74    booleanBox "remove action" "Are you sure you want to remove action file $1?"
75    if [ $? = 0 ]; then
76      rm $1;
77    fi
78 }
79
80 do_run() {
81    backupninja --run $1
82    echo "Hit return to continue..."
83    read
84 }
85
86 do_run_test() {
87   backupninja --test --run $1
88   echo "Hit return to continue..."
89   read
90 }
91
92 do_disable() {
93   mv $1 $1.disabled
94 }
95
96 do_enable() {
97   mv $1 ${1%.*}
98 }
99
100 do_rename() {
101   dir=`dirname $1`
102   filename=`basename $1`
103   inputBox "rename action" "enter a new filename" $filename
104   mv $dir/$filename $dir/$REPLY
105 }
106
107 doaction() {
108   action=$1
109   base=`basename $action`
110   if [ "${base##*.}" == "disabled" ]; then
111      enable="enable";
112   else
113      enable="disable";
114   fi
115   while true; do
116     menuBox "action menu" "$action $first" \
117       main "return to main menu" \
118       view "view configuration" \
119       xedit "launch external editor" \
120       $enable "$enable action" \
121       name "change the filename" \
122       run "run this action now" \
123       test "do a test run" \
124       kill "remove this action" 
125     [ $? = 1 ] && return;
126     result="$REPLY"
127         case "$result" in
128            "view") dialog --textbox $action 0 0;; 
129            "xedit") $EDITOR $action;;
130            "disable") do_disable $action; return;;
131            "enable") do_enable $action; return;;
132            "name") do_rename $action; return;;
133            "run") do_run $action;;
134            "test") do_run_test $action;; 
135            "kill") do_rm_action $action; return;;
136            "main") return;;
137         esac
138   done
139 }
140
141 #####################################################
142 ## begin program
143
144 conffile="/etc/backupninja.conf"
145 if [ ! -r "$conffile" ]; then
146         echo "Configuration file $conffile not found." 
147         exit 1
148 fi
149 scriptdir=`grep scriptdirectory $conffile | awk '{print $3}'`
150 if [ ! -n "$scriptdir" ]; then
151         echo "Cound not find entry 'scriptdirectory' in $conffile" 
152         exit 1
153 fi
154 if [ ! -d "$scriptdir" ]; then
155         echo "Script directory $scriptdir not found." 
156         exit 1
157 fi
158 configdirectory=`grep configdirectory $conffile | awk '{print $3}'`
159 if [ ! -n "$configdirectory" ]; then
160         echo "Cound not find entry 'configdirectory' in $conffile" 
161         exit 1
162 fi
163 if [ ! -d "$configdirectory" ]; then
164         echo "Configuration directory $configdirectory not found." 
165         exit 1
166 fi
167
168 . $scriptdir/easydialog.sh
169
170 if [ "$UID" != "0" ]; then
171         msgBox "warning" "ninjahelper must be run by root!"
172         exit 1
173 fi
174
175 for file in `find $scriptdir -follow -name '*.helper'`; do
176    . $file
177 done
178
179 setApplicationTitle "ninjahelper"
180 setDimension 75 19
181
182 #####################################################
183 ## main event loop
184
185 while true; do
186
187 menulist=
188 action=
189 let "i = 1"
190 for file in `find $conf/etc/backup.d/ -type f | sort -n`; do
191   menulist="$menulist $i $file"
192   actions[$i]=$file
193   let "i += 1"
194 done
195
196 menuBox "main menu" "select an action to edit" $menulist \
197   new "create a new backup action" \
198   quit "leave ninjahelper" 
199
200 [ $? = 1 -o $? = 255 ] && exit 0;
201
202 choice="$REPLY"
203 if [ "$choice" == "new" ]; then
204   donew;
205 elif [ "$choice" == "quit" ]; then
206   exit 0;
207 else
208   action=${actions[$choice]};
209   if [ -f "$action" ]; then
210      doaction $action
211   else
212      msgBox "error" "error: cannot find the file '$action'"
213   fi
214 fi
215
216
217 done