-
Notifications
You must be signed in to change notification settings - Fork 1
/
svnbackup-backup.sh
executable file
·356 lines (308 loc) · 9.82 KB
/
svnbackup-backup.sh
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/bin/bash
# vim:ts=4:sw=4:et:
# script to make an automated commit of /etc and/or other configuration
# directories into subversion repository
# $Id: svnbackup-backup.sh 104 2011-03-15 15:28:30Z coolcold $
MYREVISION='$Id: svnbackup-backup.sh 104 2011-03-15 15:28:30Z coolcold $'
MYVERSION="0.2.2 , revision: ${MYREVISION}"
PROGNAME=$(basename $0)
#walking around sudo/su variables & subversion configs
HOME=/root
export HOME
#setting messages to be in English
LANG=C
export LANG
#LOCKFILE=/var/run/svnbackup.pid
CONFFILE=/etc/svnbackup.conf
#CONFFILE=svnbackup.conf
LOGGER='logger -t svnbackup --'
INITSVN=NO
TS_START=$(date +%s) #when we started
#error messages
ERROR_NOREPO="No repository found in"
ERROR_CANTGETPASS="Can't get password"
function mylog() {
if [ -z "$1" ];then return 0;fi #exiting if msg is empty
if [ "x$1" == "x-e" ];then
echo "$2"
$LOGGER "$2"
else
$LOGGER "$1"
fi
}
function myexit() {
extcode="$1"
if [ -z "$1" ];then extcode=0;fi
#doing some cleanup
if ! [ "x$2" == "xNO" ];then
if ! [ -z "$svncopy" ];then mylog "removing temporary copy ${svncopy}";rm -rf "${svncopy}";fi
mylog "removing lockfile $LOCKFILE";rm -rf $LOCKFILE
fi
exit $extcode
}
print_usage() {
echo "Usage: $PROGNAME [-c <config file path>] [-I]"
echo ""
echo "-c contains path to config file. default file is $CONFFILE"
echo "-I should be used for first run, to make subversion client save password and such"
echo " use -h to show this help"
}
print_help() {
echo "svnbackup - $MYVERSION"
echo ""
print_usage
echo ""
echo "This script intended to make an automated commit of /etc and/or other configuration"
echo "directories into subversion repository, so allowing to keep history of changes"
}
mylog "starting svnbackup script version: $MYVERSION"
#if [[ $# -eq "0" ]]; then
# #no arguments were specified...very, very bad
# print_help
# mylog -e "no configuration file was specified, please read help on usage"
# exit 1
#fi
# parsing arguments
while getopts ":hIc:" Option; do
case $Option in
h)
print_help
exit 0
;;
c)
CONFFILE=${OPTARG}
;;
I)
INITSVN="YES"
;;
*)
print_help
exit 0
;;
esac
done
shift $(($OPTIND - 1))
# end of arguments parsing
if [ ! -f $CONFFILE ];then
mylog -e "configuration file $CONFFILE doesn't exist. Check path you've specified?"
exit 1
fi
if ! . $CONFFILE;then
mylog -e "cannot include config file $CONFFILE . Check permissions?"
exit 2
fi
#validating
if [ -z $SERVER ];then mylog -e "this server name variable SERVER is not set, check your config $CONFFILE";exit 2;fi
if [ -z $LOCKFILE ];then mylog -e "lockfile variable LOCKFILE is not set, check your config $CONFFILE";exit 2;fi
if [ -z $STATEFILE ];then mylog -e "status file variable STATEFILE is not set, check your config $CONFFILE";exit 2;fi
if [ -z $SVNREPOTMP ];then mylog -e "tmp storage variable SVNREPOTMP for data is not set, check your config $CONFFILE";exit 2;fi
if [ -z $SVNPATH ];then mylog -e "svn repo path variable SVNPATH is not set, check your config $CONFFILE";exit 2;fi
#username should be really needed only on first run, then data should be cached
if [ -z $SVNUSER ];then mylog -e "username for svn repo access variable SVNUSER is not set, check your config $CONFFILE";exit 2;fi
if ! which svn >/dev/null;then mylog -e "subversion not found in your PATH, consider installing subversion"; exit 2;fi
if ! which rsync >/dev/null;then mylog -e "rsync not found in your PATH, consider installing rsync";exit 2;fi
svncopy="${SVNREPOTMP}/${SERVER}"
#getting include dirs
includes=$(grep "^include_dir=" ${CONFFILE}|sed 's/^include_dir=//';exit ${PIPESTATUS[0]})
includes_res=$?
if [ $includes_res -ne 0 ];then
#grep failed to find any include dirs :(
mylog -e "couldn't find any directories to include, exiting..."
exit 2
fi
#echo "includes are"
#echo "${includes}"
#array will be 1 based cuz count func returns total count
c=1
while read line
do
include_dir[$c]="${line}"
#echo "c is $c"
((c++))
#echo "elements count:${#include_dir[@]}"
done < <(echo "${includes}")
ic=$((${#include_dir[@]} - 1))
#echo "elements count:$ic"
#c=1
#while [ $c -le $ic ];do
# echo "i is $c, and element is ${include_dir[$c]}"
# ((c++))
#done
#setting lock
mylog "setting lockfile $LOCKFILE"
exec 200<> $LOCKFILE
flock -n -x 200
if [ $? -ne 0 ];then
mylog -e "setting lock on $LOCKFILE failed"
myexit 1 "NO"
fi
echo $$ >&200
#prepare local copy
function preparelc() {
rm -rf ${svncopy}
if ! mkdir -m 700 -p ${svncopy};then mylog -e "problem while creating directory ${svncopy}";return 2;fi
mylog "doing checkout from repository $SVNPATH/$SERVER into ${svncopy} ..."
#are were doing svn init?
if [ $INITSVN = "YES" ];then
if ! svn --username "${SVNUSER}" co $SVNPATH/$SERVER ${svncopy};then
mylog -e "checkout failed"
return 2
fi
else
if ! svn co --non-interactive $SVNPATH/$SERVER ${svncopy};then
mylog "checkout failed"
return 2
fi
fi
chmod 700 ${svncopy}
chown root:root ${svncopy}
}
#do repo & config init on localhost
function dolocalinit() {
rm -rf ${svncopy}
if ! mkdir -m 700 -p ${svncopy};then mylog -e "problem while creating directory ${svncopy}";return 2;fi
mylog "doing checkout from repository $SVNPATH/$SERVER into ${svncopy} ..."
if ! svn --username "${SVNUSER}" co $SVNPATH/$SERVER ${svncopy};then
mylog -e "checkout failed"
return 2
fi
chmod 700 ${svncopy}
chown root:root ${svncopy}
}
function synclc() {
#this function should sync local data onto checkouted ones
#this we'll do svn commit
mylog "doing local sync for $1"
if [ "x$1" == "x" ];then return 2;fi
if ! pushd ${svncopy} >/dev/null 2>&1;then
mylog -e "can't pushd into ${svncopy}"
return 2
fi
#creating subdirectory
newdir=".$1"
mkdir -p "$newdir"
if ! pushd "${newdir}" >/dev/null 2>&1;then
mylog -e "can't pushd into ${newdir}"
return 2
fi
if ! rsync -a --delete --exclude=.svn "$1/" . > /dev/null;then
mylog -e "rsync failed, can not continue"
return 3
fi
popd >/dev/null 2>&1
#pushd ${svncopy}/etc >/dev/null 2>&1
}
function dosvncommit() {
if ! pushd ${svncopy} >/dev/null 2>&1;then
mylog -e "can't pushd into ${svncopy}"
return 2
fi
svnstatus="$(svn status)"
if [ "x${svnstatus}" != "x" ]; then
#echo "The following changes were made to /etc:"
tmpfile=$(mktemp /tmp/svn.XXXXXX)
if ! svn status >${tmpfile} 2>&1;then
mylog -e "svn status failed, can't continue"
rm -rf ${tmpfile}
return 2
fi
#cat ${tmpfile}
#echo ""
svntoadd=$(cat ${tmpfile} | egrep '^\?')
for i in ${svntoadd}; do
if [ "${i}" != "?" ]; then
svn add ${i}>/dev/null
fi
done
svntodel=$(cat ${tmpfile} | egrep '^\!')
for i in ${svntodel}; do
if [ "${i}" != "!" ]; then
svn rm ${i} >/dev/null
fi
done
svntoupstatus=$(cat ${tmpfile} | egrep '^\~')
for i in ${svntoupstatus}; do
if [ "${i}" != '~' ]; then
rm ${i}
svn remove ${i} >/dev/null
#svn add ${i} >/dev/null
fi
done
if ! svn commit -m "Auto-commit on $(date)" > /dev/null;then
echo "commit failed. check access rights?"
rm -f ${tmpfile}
return 2
fi
rm -f ${tmpfile}
fi
popd >/dev/null 2>&1
}
function updatestatus() {
TS_END=$(date +%s)
TS_DIFF=$((TS_END - TS_START))
#format - "current timestamp" "time taken"
echo "$TS_END $TS_DIFF" > ${STATEFILE}
}
#checking - are we in init mode?
if [ $INITSVN = "YES" ];then
dolocalinit
localinit_res=$?
if [ $localinit_res -ne 0 ];then
mylog -e "init mode failed, may be this is caused by non-existent repository on remote side"
mylog -e "read error message and refer to manual pages"
myexit $localinit_res
else
mylog -e "init mode succeeded, you may continue in regular mode now"
myexit 0
fi
fi
#doing checkout from repo
preparelc_out=$(preparelc 2>&1)
preparelc_res=$?
if [ $preparelc_res -ne 0 ];then
mylog -e "checkouting from repo failed, exiting"
mylog -e "mesage is: $preparelc_out"
if echo "$preparelc_out"|fgrep -q "${ERROR_NOREPO}";then
mylog -e "Do you have created repository on $SVNPATH/$SERVER ? "
mylog -e "You may need to init subversion client configuration, view $PROGNAME -h and README for more information"
elif echo "$preparelc_out"|fgrep -q "${ERROR_CANTGETPASS}";then
mylog -e "Have you done initial run of ${PROGNAME} ? view $PROGNAME -h and README for more information"
fi
myexit 2
else
mylog "repository copy checkouted"
#are we initializing repo?
if [ $INITSVN = "YES" ];then
#just exiting cuz everything should be done already
mylog -e "exiting because of init mode"
myexit 0
fi
fi
#iterating over paths to be backuped
c=1
while [ $c -le $ic ];do
#echo "i is $c, and element is ${include_dir[$c]}"
so=$(synclc "${include_dir[$c]}" 2>&1)
synclc_res=$?
if [ $synclc_res -eq 3 ];then
mylog -e "dying because of rsync error:"
mylog -e "$so"
myexit 2
elif [ $synclc_res -ne 0 ];then
mylog -e "dying because of unknown local sync error"
myexit 2
fi
((c++))
done
#exit 2
co=$(dosvncommit 2>&1)
co_res=$?
if [ $co_res -ne 0 ];then
mylog -e "svn commit failed:"
mylog -e "$co"
myexit 2
fi
#since we got here, everything should be ok, updating success status
updatestatus
mylog "time taken: $TS_DIFF seconds"
myexit 0