1 |
#!/bin/sh |
---|
2 |
######################################################################################### |
---|
3 |
## |
---|
4 |
## configtool |
---|
5 |
## |
---|
6 |
## Commands: |
---|
7 |
## |
---|
8 |
## backup <modName> |
---|
9 |
## restore <modName> |
---|
10 |
## getconfig <modName> <filename> |
---|
11 |
## putconfig <modName> <filename> |
---|
12 |
## |
---|
13 |
## 2007-01-16 Andreas Vogel (omega) |
---|
14 |
## |
---|
15 |
######################################################################################### |
---|
16 |
|
---|
17 |
PreBackup () { |
---|
18 |
if [ -x $modDir/system/configprepare ] ; then |
---|
19 |
$modDir/system/configprepare backup $modName |
---|
20 |
ret=$? |
---|
21 |
else |
---|
22 |
ret=0 |
---|
23 |
fi |
---|
24 |
|
---|
25 |
return $ret |
---|
26 |
} |
---|
27 |
|
---|
28 |
PostRestore () { |
---|
29 |
if [ -x $modDir/system/configprepare ] ; then |
---|
30 |
$modDir/system/configprepare restore $modName |
---|
31 |
ret=$? |
---|
32 |
else |
---|
33 |
ret=0 |
---|
34 |
fi |
---|
35 |
|
---|
36 |
return $ret |
---|
37 |
} |
---|
38 |
|
---|
39 |
######################################################################################### |
---|
40 |
## |
---|
41 |
## Main |
---|
42 |
## |
---|
43 |
######################################################################################### |
---|
44 |
|
---|
45 |
cmd=$1 |
---|
46 |
modName=$2 |
---|
47 |
filename=$3 |
---|
48 |
warn=${4:-warn} |
---|
49 |
|
---|
50 |
## |
---|
51 |
## Use standard backup file if command is "backup" or "restore" |
---|
52 |
## |
---|
53 |
if [ "$cmd" == backup -o "$cmd" == restore ] ; then |
---|
54 |
filename="/raid/data/module/backup/$modName/backup-$modName.tar.gz" |
---|
55 |
|
---|
56 |
if [ "$cmd" == backup ] ; then |
---|
57 |
cmd=getconfig |
---|
58 |
elif [ "$cmd" == restore ] ; then |
---|
59 |
cmd=putconfig |
---|
60 |
warn=nowarn |
---|
61 |
fi |
---|
62 |
fi |
---|
63 |
|
---|
64 |
if [ -z "$filename" -o -z "$modName" ] ; then |
---|
65 |
echo "ERROR: illegal commandline: $0 $*" |
---|
66 |
exit 1 |
---|
67 |
fi |
---|
68 |
|
---|
69 |
MAGIC="CONFIG-$modName" |
---|
70 |
modDir=/raid/data/module/$modName |
---|
71 |
modVersion=$(awk '{print $2}' < "$modDir/VERSION") |
---|
72 |
|
---|
73 |
case "$cmd" in |
---|
74 |
getconfig|get) |
---|
75 |
dstFile=$filename |
---|
76 |
|
---|
77 |
if ! PreBackup ; then |
---|
78 |
echo "ERROR: PreBackup script failed!" |
---|
79 |
exit 1 |
---|
80 |
fi |
---|
81 |
|
---|
82 |
echo "$MAGIC $modVersion $(date '+%Y-%m-%d_%H:%M:%S')" > "$modDir/system/etc/MAGIC" |
---|
83 |
tar czpf "$dstFile" -C "$modDir/system" etc |
---|
84 |
status=$? |
---|
85 |
rm "$modDir/system/etc/MAGIC" |
---|
86 |
exit $status |
---|
87 |
;; |
---|
88 |
|
---|
89 |
putconfig|put) |
---|
90 |
srcFile=$filename |
---|
91 |
tmpdir=/tmp/tmp.configtool |
---|
92 |
|
---|
93 |
if [ ! -r "$srcFile" ] ; then |
---|
94 |
if [ "$warn" != nowarn ] ; then |
---|
95 |
echo "ERROR: source file '$sourceFile' not readable!" |
---|
96 |
exit 1 |
---|
97 |
else |
---|
98 |
exit 0 |
---|
99 |
fi |
---|
100 |
fi |
---|
101 |
|
---|
102 |
rm -rf "$tmpdir" |
---|
103 |
mkdir -p "$tmpdir" |
---|
104 |
if ! tar xzf "$srcFile" -C "$tmpdir" ; then |
---|
105 |
echo "ERROR: cannot unpack tar file!" |
---|
106 |
rm -rf "$tmpdir" |
---|
107 |
exit 1 |
---|
108 |
fi |
---|
109 |
|
---|
110 |
cfgMagic=$(awk '{print $1;exit}' < "$tmpdir/etc/MAGIC") |
---|
111 |
cfgVersion=$(awk '{print $2;exit}' < "$tmpdir/etc/MAGIC") |
---|
112 |
cfgDate=$(awk '{print $3;exit}' < "$tmpdir/etc/MAGIC") |
---|
113 |
|
---|
114 |
if [ "$cfgMagic" != "$MAGIC" ] ; then |
---|
115 |
echo "ERROR: wrong magic '$cfgMagic' - expected '$MAGIC'!" |
---|
116 |
rm -rf "$tmpdir" |
---|
117 |
exit 1 |
---|
118 |
fi |
---|
119 |
rm "$tmpdir/etc/MAGIC" |
---|
120 |
|
---|
121 |
if ! tar cpf - -C "$tmpdir" etc | (cd "$modDir/system" ; tar xpf -) ; then |
---|
122 |
echo "ERROR: cannot copy from '$tmpdir' to '$modDir/system'!" |
---|
123 |
rm -rf "$tmpdir" |
---|
124 |
exit 1 |
---|
125 |
fi |
---|
126 |
|
---|
127 |
rm -rf "$tmpdir" |
---|
128 |
|
---|
129 |
if ! PostRestore ; then |
---|
130 |
echo "ERROR: PostRestore script failed!" |
---|
131 |
exit 1 |
---|
132 |
fi |
---|
133 |
;; |
---|
134 |
|
---|
135 |
*) |
---|
136 |
echo "ERROR: invalid command '$cmd'!" |
---|
137 |
echo "Usage: $0 {backup|restore|getconfig|putconfig} <modname> <filename>" |
---|
138 |
exit 1 |
---|
139 |
;; |
---|
140 |
esac |
---|
141 |
|
---|
142 |
exit 0 |
---|