aboutsummaryrefslogtreecommitdiff
path: root/ninjahelper
blob: 9568b53b8197ec2e1abad75091731565e7496ae8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/bin/bash

####################################################
## Functions

##
## returns the next available file name given a file
## in the form /etc/backup.d/10.sys
## sets variable $returned_filename
##
get_next_filename() {
  next_filename=$1
  dir=`dirname $next_filename`
  file=`basename $next_filename`
  number=${file:0:2}
  suffix=${file:3}
  while [ -f $next_filename ]; do
    let "number += 1"
    next_filename="$dir/$number.$suffix"
  done
}

##
## installs packages (passed in as $@) if not present
##
require_packages() {
	for pkg in "$@"; do
	   installed=`dpkg -s $pkg | grep 'ok installed'`
       if [ -z "$installed" ]; then 
           booleanBox "install $pkg?" "This backup action requires package $pkg. Do you want to install it now?"
           if [ $? = 0 ]; then
              apt-get install $pkg
              echo "hit return to continue...."
              read
           fi
       fi
    done
}

doradiobox() {
	defaultchoice="red is.pretty on"
	choices="green is_nice_too off blue i_love_blue off yellow is.bright off orange make.me.hungry off"
	radioBox "radio title" "choose one color" $defaultchoice $choices
	case $? in
	   0) ;;
	   1) echo "color choice cancelled..."; sleep 1;;
	   255) echo "something went wrong, exiting..."
	      exit 1 ;;
	esac
	result="$REPLY"
	msgBox "message title" "you said $result."
}

donew() {
    menuBox "new action menu" "select an action to create"  \
      return "return to main menu" \
      sys "general hardware and system info" \
      mysql "mysql database backup" \
      ldap "ldap database backup" \
      rdiff "incremental filesystem backup"
  
    [ $? = 1 ] && return;
    result="$REPLY"
  	case "$result" in
	   "sys") sys_wizard;; 
	   "mysql") mysql_wizard;;
	   "ldap") ldap_wizard;;
   	   "rdiff") rdiff_wizard;;
	   "return") return;;
	esac
}

do_rm_action() {
   booleanBox "remove action" "Are you sure you want to remove action file $1?"
   if [ $? = 0 ]; then
     rm $1;
   fi
}

do_run() {
   backupninja --run $1
   echo "Hit return to continue..."
   read
}

do_run_test() {
  backupninja --test --run $1
  echo "Hit return to continue..."
  read
}

do_disable() {
  mv $1 $1.disabled
}

do_enable() {
  mv $1 ${1%.*}
}

do_rename() {
  dir=`dirname $1`
  filename=`basename $1`
  inputBox "rename action" "enter a new filename" $filename
  mv $dir/$filename $dir/$REPLY
}

doaction() {
  action=$1
  base=`basename $action`
  if [ "${base##*.}" == "disabled" ]; then
     enable="enable";
  else
     enable="disable";
  fi
  while true; do
    menuBox "action menu" "$action $first" \
      main "return to main menu" \
      view "view configuration" \
      xedit "launch external editor" \
      $enable "$enable action" \
      name "change the filename" \
      run "run this action now" \
      test "do a test run" \
      kill "remove this action" 
    [ $? = 1 ] && return;
    result="$REPLY"
  	case "$result" in
	   "view") dialog --textbox $action 0 0;; 
	   "xedit") $EDITOR $action;;
	   "disable") do_disable $action; return;;
   	   "enable") do_enable $action; return;;
   	   "name") do_rename $action; return;;
	   "run") do_run $action;;
	   "test") do_run_test $action;; 
	   "kill") do_rm_action $action; return;;
	   "main") return;;
	esac
  done
}

#####################################################
## begin program

conffile="/etc/backupninja.conf"
if [ ! -r "$conffile" ]; then
	echo "Configuration file $conffile not found." 
	exit 1
fi
scriptdir=`grep scriptdirectory $conffile | awk '{print $3}'`
if [ ! -n "$scriptdir" ]; then
	echo "Cound not find entry 'scriptdirectory' in $conffile" 
	exit 1
fi
if [ ! -d "$scriptdir" ]; then
	echo "Script directory $scriptdir not found." 
	exit 1
fi
configdirectory=`grep configdirectory $conffile | awk '{print $3}'`
if [ ! -n "$configdirectory" ]; then
	echo "Cound not find entry 'configdirectory' in $conffile" 
	exit 1
fi
if [ ! -d "$configdirectory" ]; then
	echo "Configuration directory $configdirectory not found." 
	exit 1
fi

. $scriptdir/easydialog.sh

if [ "$UID" != "0" ]; then
	msgBox "warning" "ninjahelper must be run by root!"
	exit 1
fi

for file in `find $scriptdir -follow -name '*.helper'`; do
   . $file
done

setApplicationTitle "ninjahelper"
setDimension 75 19

#####################################################
## main event loop

while true; do

menulist=
action=
let "i = 1"
for file in `find $conf/etc/backup.d/ -type f | sort -n`; do
  menulist="$menulist $i $file"
  actions[$i]=$file
  let "i += 1"
done

menuBox "main menu" "select an action to edit" $menulist \
  new "create a new backup action" \
  quit "leave ninjahelper" 

[ $? = 1 -o $? = 255 ] && exit 0;

choice="$REPLY"
if [ "$choice" == "new" ]; then
  donew;
elif [ "$choice" == "quit" ]; then
  exit 0;
else
  action=${actions[$choice]};
  if [ -f "$action" ]; then
     doaction $action
  else
     msgBox "error" "error: cannot find the file '$action'"
  fi
fi


done