created ninjahelper
[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   dir=`dirname $1`
94   base=`basename $1`
95   mv $dir/$base $dir/0-$base
96 }
97
98 do_enable() {
99   dir=`dirname $1`
100   base=`basename $1`
101   mv $dir/$base $dir/${base:2}
102 }
103
104 do_rename() {
105   dir=`dirname $1`
106   filename=`basename $1`
107   inputBox "rename action" "enter a new filename" $filename
108   mv $dir/$filename $dir/$REPLY
109 }
110
111 doaction() {
112   action=$1
113   base=`basename $action`
114   if [ "${base:0:2}" == "0-" ]; then
115      enable="enable";
116   else
117      enable="disable";
118   fi
119   while true; do
120     menuBox "action menu" "$action $first" \
121       main "return to main menu" \
122       view "view configuration" \
123       xedit "launch external editor" \
124       $enable "$enable action" \
125       name "change the filename" \
126       run "run this action now" \
127       test "do a test run" \
128       kill "remove this action" 
129     [ $? = 1 ] && return;
130     result="$REPLY"
131         case "$result" in
132            "view") dialog --textbox $action 0 0;; 
133            "xedit") $EDITOR $action;;
134            "disable") do_disable $action; return;;
135            "enable") do_enable $action; return;;
136            "name") do_rename $action; return;;
137            "run") do_run $action;;
138            "test") do_run_test $action;; 
139            "kill") do_rm_action $action; return;;
140            "main") return;;
141         esac
142   done
143 }
144
145 #####################################################
146 ## begin program
147
148 conffile="/etc/backupninja.conf"
149 if [ ! -r "$conffile" ]; then
150         echo "Configuration file $conffile not found." 
151         exit 1
152 fi
153 scriptdir=`grep scriptdirectory $conffile | awk '{print $3}'`
154 if [ ! -n "$scriptdir" ]; then
155         echo "Cound not find entry 'scriptdirectory' in $conffile" 
156         exit 1
157 fi
158 if [ ! -d "$scriptdir" ]; then
159         echo "Script directory $scriptdir not found." 
160         exit 1
161 fi
162 configdirectory=`grep configdirectory $conffile | awk '{print $3}'`
163 if [ ! -n "$configdirectory" ]; then
164         echo "Cound not find entry 'configdirectory' in $conffile" 
165         exit 1
166 fi
167 if [ ! -d "$configdirectory" ]; then
168         echo "Configuration directory $configdirectory not found." 
169         exit 1
170 fi
171
172 . $scriptdir/easydialog.sh
173
174 if [ "$UID" != "0" ]; then
175         msgBox "warning" "ninjahelper must be run by root!"
176         exit 1
177 fi
178
179 for file in `find $scriptdir -follow -name '*.helper'`; do
180    . $file
181 done
182
183 setApplicationTitle "ninjahelper"
184 setDimension 75 19
185
186 #####################################################
187 ## main event loop
188
189 while true; do
190
191 menulist=
192 action=
193 let "i = 1"
194 for file in `find $conf/etc/backup.d/ -type f | sort -n`; do
195   menulist="$menulist $i $file"
196   actions[$i]=$file
197   let "i += 1"
198 done
199
200 menuBox "main menu" "select an action to edit" $menulist \
201   new "create a new backup action" \
202   quit "leave ninjahelper" 
203
204 [ $? = 1 -o $? = 255 ] && exit 0;
205
206 choice="$REPLY"
207 if [ "$choice" == "new" ]; then
208   donew;
209 elif [ "$choice" == "quit" ]; then
210   exit 0;
211 else
212   action=${actions[$choice]};
213   if [ -f "$action" ]; then
214      doaction $action
215   else
216      msgBox "error" "error: cannot find the file '$action'"
217   fi
218 fi
219
220
221 done