File: backout.csh

package info (click to toggle)
afbackup 3.1beta1-1
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,500 kB
  • ctags: 1,685
  • sloc: ansic: 22,406; csh: 3,597; tcl: 964; sh: 403; makefile: 200
file content (430 lines) | stat: -rwxr-xr-x 9,699 bytes parent folder | download | duplicates (3)
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#! /bin/csh -f

alias usage 'echo Usage: `basename $0` "[ -nl ] [ -<past-backup-no> ] [ -R <root-directory> ] \" ; echo "               [ -h <backuphost> ]  <path-part> [ <path-parts> [ ... ] ]" ; echo "      " `basename $0` -a "[ -<past-backup-no> ] [ -R <root-directory> ] \" ; echo "               [ -h <backuphost> ]"; echo "      " `basename $0` "-{ef}  [ -R <root-directory> ] [ -h <backuphost> ] \" ; echo "               < <startup-info-file>" ;  exit 1'

if ($#argv < 1) then
  usage
endif

if (! $?BACKUP_HOME) then
  set pnam=$0
  set pnam = $pnam:t
  if ($pnam == $0) then
    foreach dir ($path)
      if (-x $dir/$0) then
	set p=$dir/$0
	break
      endif
    end
    if ($p == ./$pnam) then
      set p=`pwd`
      setenv BACKUP_HOME  $p:h
    else
      set p=$p:h
      setenv BACKUP_HOME  $p:h
    endif
  else
    if (./$pnam == $0) then
      set p=`pwd`/$pnam
    else
      if (`echo $0|cut -c1` != "/") then
        set p=`pwd`/$0
      else
        set p=$0
      endif
    endif
    set p=$p:h
    setenv BACKUP_HOME $p:h
  endif
endif

set path=($BACKUP_HOME/bin $path)
rehash

unsetenv AWK

# the solaris awk is doin' dawn f...... BS
foreach awk (nawk gawk awk)
  foreach dir ($path)
    if (-x $dir/$awk) then
      setenv AWK $dir/$awk
      break
    endif
  end
  if ($?AWK) break
end

if (! $?AWK) then
  echo 'No awk ? Is this really a lovely UNIX ?'
  echo 'Sorry. I have to exit.'
  exit 1
endif

set configfile=$BACKUP_HOME/lib/backup.conf

alias getparam $AWK' '"'"'/^[ 	]*'"'"'\!:1'"'"'/{split($0,a,"'"'"'\!:1'"'"'[ 	]*"); print a[2]}'"' $configfile"

set allf=/tmp/allf.$$
set spf=/tmp/spf.$$

set host="`getparam 'BackupHost:?'`"
set rootdir="`getparam 'RootDirectory:?'`"
set indexfilepart="`getparam 'IndexFilePart:?'`"
set numindexestostore="`getparam 'NumIndexesToStore:?'`"
set compresscmd="`getparam 'CompressCmd:?'`"
set uncompresscmd="`getparam 'UncompressCmd:?'`"
set logfile="`getparam 'LoggingFile:?'`"
set numparts="`getparam 'NumBackupParts:?'`"

if ("$uncompresscmd" == "" ) then
  set uncompresscmd=gunzip
endif

set list=0
set count=0
set prev=0
set unlink="u"
set all=0
set emergency_recovery=0
set filelist_recovery=0

set files=()

set oarg=""
foreach a ($argv)
  if ($oarg != "") then
    switch($oarg)
      case "R":
	set rootdir="$a"
	breaksw
      case "h":
	set host="$a"
	breaksw
    endsw

    set oarg=""
    continue
  endif

  if (`echo $a|grep '^[-][0-9][0-9]*$' |wc -l` > 0) then
    set prev=`echo $a|cut -c2-`
  else
    set opts=(`getopt ulnaefRh $a`)
    if ("_$opts[1]" != "_--") then
      foreach b ($opts)
        if ("_$b" == "_--") then
          break;
        endif
	switch ("_$b")
	  case "_-l":
            set list=1
	    breaksw
	  case "_-n":
            set count=1
	    breaksw
	  case "_-u":
            set unlink="u"
	    breaksw
	  case "_-a":
            set all=1
	    breaksw
	  case "_-f":
            set filelist_recovery=1
	  case "_-e":
            set emergency_recovery=1
	    breaksw
	  case "_-R":
	    set oarg="R"
	    breaksw
	  case "_-h":
	    set oarg="h"
	    breaksw
	  default:
	    set files=("$files" "$a")
        endsw
      end
    else
      set files=("$files" "$a")
    endif
  endif
end

if ($#files == 0 && ! $all && ! $emergency_recovery) then
  usage
endif

set estat=$status

onintr emexit

unalias cd

\rm -f $allf $spf

if ($emergency_recovery) then
  goto emergency_recovery
endif

set num=`cat $BACKUP_HOME/var/num`
@ num -= $prev >& /dev/null

if ($status != 0 || (! -e "$indexfilepart"$num && ! -e "$indexfilepart"$num".z")) then
  echo Sorry, no backup found with number "$num", exiting ...
  set estat=2
  goto emexit
endif

if (-e "$indexfilepart"$num) then
  set indexfiles="$indexfilepart"$num
else
  set indexfiles="$indexfilepart"$num".z"
endif
  
if ($numparts > 1) then
  if (-r $BACKUP_HOME/var/part) then
    set part=`cat $BACKUP_HOME/var/part`
    if (`echo $part|grep '[0-9][0-9]*'|wc -l` < 1) then
      set part=1
    endif
  else
    set part=$numparts
  endif
  if ($part < $numparts) then
    @ num--
    if (-e "$indexfilepart"$num) then
      set indexfiles=("$indexfilepart"$num $indexfiles)
    else
      set indexfiles=("$indexfilepart"$num".z" $indexfiles)
    endif
  endif
endif

if ($all) then
  goto backout_all
endif

echo -n Searching for files in backup ...

set pat='('`echo "$files"|awk '{for(i=1;i<NF;i++) printf "%s|",$i; printf "%s\n",$NF}'`')'
touch $allf
foreach idxfile ($indexfiles)
  if ($idxfile:e != "z") then
    grep -h '^[1-9][0-9]*[.][1-9][0-9]*:' $idxfile | egrep "$pat" >> $allf
  else
    cat $idxfile | $uncompresscmd | grep -h '^[1-9][0-9]*[.][1-9][0-9]*:' \
			| egrep "$pat" >> $allf
  endif
end


echo " done."

set numfiles=`cat $allf|wc -l`
if ($numfiles < 1) then
  echo Sorry, didn\'t find any matches. Nothing is backed out.
  set estat=3
  goto emexit
endif

echo -n Found $numfiles matches
set numfiles=(`awk '{print $1,$1}' $allf|tsort|wc -l`)
echo -n " in" $numfiles files
set numtapes=(`cut -d. -f1 $allf|awk '{print $1,$1}'|tsort|wc -l`)
echo " on" $numtapes tapes.

if ($count) goto emexit

if ($list) then
  cut -d" " -f2- $allf | more
  goto emexit
endif

echo Going to restore files.

set fault=0

while (1)
  set numfiles=`cat $allf|wc -l`
    if ($numfiles < 1) break
  \rm -f $spf
  set posspec=`head -1 $allf|cut -d: -f1`
  set actcart=`echo $posspec|cut -d. -f1`
  set actfile=`echo $posspec|cut -d. -f2`

  echo
  echo Extracting files from cartridge $actcart, file $actfile ...

  egrep '^'$actcart"."$actfile": " $allf | cut -d" " -f2- > $spf

  if (! -d $rootdir) then
    mkdir -p $rootdir
  endif

  cd $rootdir

  backup -xvg$unlink -T $spf -C $actcart -F $actfile -h $host

  if ($status != 0) then
    set fault=1
    echo " "
    echo Something went wrong backing out the files.
    echo See previous output for details.
  endif

  echo done with cartridge $actcart, file $actfile.

  \rm -f $spf
  egrep -v '^'$actcart"."$actfile": " $allf > $allf.new
  \mv -f $allf.new $allf
end

echo
if ($fault != 0) then
  echo " "
  echo Not all files have been backed out successfully.
  echo See previous output for details.
else
  echo All matching files successfully restored.
endif

emexit:

\rm -f $allf $spf $allf.new

exit $estat

backout_all:

if ($#indexfiles == 2) then
  if (! -e $indexfiles[1] && ! -e $indexfiles[2]) then
    echo Error: no logging files found. Extract the archive by hand.
    exit 91
  endif
endif
if ($#indexfiles == 1) then
  if (! -e $indexfiles) then
    echo Error: no logging files found. Extract the archive by hand.
    exit 91
  endif
endif

set newstart=`cat $indexfiles | head -1 | awk '{print $1}'`

set actcart=`echo $newstart | cut -d. -f1`
set actfile=`echo $newstart | cut -d. -f2 | cut -d: -f1`

while (1)
  if (! -d $rootdir) then
    mkdir -p $rootdir
  endif

  cd $rootdir

  echo -n Going to back out from cartridge $actcart, file $actfile ...

  backup -xvnau -h $host -F $actfile -C $actcart |& grep '^[1-9][0-9]*[.][1-9][0-9]*:' | tail -1 >! $spf

  set errst=$status
  set lastline=`cat $spf`
  \rm -f $spf

  if ($errst != 0) then
    echo -n " an error occured, trying to continue anyway ..."
  endif

  echo " done."

  set newstart=`echo $lastline | awk '{print $1}'`
  set lastfile=`echo $lastline | awk '{print $NF}'`
  set lastfilepat=`echo $lastfile | sed 's;/;\\/;g'`

  set nextline=`cat $indexfiles | sed '1,/^'$newstart'[ 	]*'"$lastfilepat"'$/ d' | grep '^[1-9][0-9]*[.][1-9][0-9]*:' | head -1`

  if (`echo $nextline|wc -w` < 1) then
    break
  endif

  set newstart=`echo $nextline | awk '{print $1}'`
  set actcart=`echo $newstart | cut -d. -f1`
  set actfile=`echo $newstart | cut -d. -f2 | cut -d: -f1`
end

if ($errst) then
  echo " "
  echo "The Backout ended with an error."
  echo "Try to analyze the logs on the backup-server."
  exit $errst
endif

exit 0


emergency_recovery:

# dummy line to make GNU sed happy
echo dummyline >! $allf
cat >> $allf

set num=`grep -i '^~~[	 ]*backup.*[1-9][0-9]*[	 ]*$' $allf |tail -1|awk '{print $NF}'`
if (`echo $num|grep '^[1-9][0-9]*$'|wc -l` < 1) then
  echo "Error: Cannot find necessary information for restore, sorry."
  exit 99
endif

set filenum=$num
set indexfiles="$indexfilepart"$filenum
while (-e $indexfiles || -e $indexfiles".z")
  echo "Warning: logging file $indexfiles exists, trying next number."
  @ filenum++
  set indexfiles="$indexfilepart"$filenum
end

touch $indexfiles
echo "Logging of the filenames goes to $indexfiles"
echo $filenum >! $BACKUP_HOME/var/num

set exitst=0

if (! -d $rootdir) then
  mkdir -p $rootdir
endif

cd $rootdir
while(1)
  if (`grep '^~~[ 	]*[Bb]ackup.*[ 	]*'$num'[ 	]*$' $allf|wc -l` == 0) then
    break
  endif

  sed '1,/^~~[ 	]*[Bb]ackup.*[ 	]*'$num'[ 	]*$/ d' $allf >! $spf
  \rm -f $allf
  set cart=`grep -i '^~~[ 	]*cartridge' $spf|head -1|awk '{print $NF}'`
  set file=`grep -i '^~~[ 	]*file' $spf|head -1|awk '{print $NF}'`
  mv $spf $allf
  if (`echo $cart|grep '^[1-9][0-9]*$'|wc -l` < 1) then
    echo "Error: Cannot find cartridge number, sorry."
    set exitst=99
    continue
  endif
  if (`echo $file|grep '^[1-9][0-9]*$'|wc -l` < 1) then
    echo "Error: Cannot find tape file number, sorry."
    set exitst=99
    continue
  endif

  if ($filelist_recovery) then
    echo "Going to restore filelist from cartridge $cart, file $file."
    backup -tn -h $host -F $file -C $cart >>& $indexfiles
    set s=$status
  else
    echo "Going to restore files from cartridge $cart, file $file."
    backup -xvnau -h $host -F $file -C $cart >>& $indexfiles
    set s=$status
  endif
  if ($s) then
    echo "An error occured. See the file $indexfiles for details."
  endif
end

\rm -f $allf $spf

exit $exitst