summaryrefslogtreecommitdiffstats
path: root/abs/core-testing/LinHES-system/bin/backupcommon
blob: d11234108e88045e916770a287597f6289e45908 (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
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
#!/bin/bash

. /usr/LH/bin/library.sh || {
    echo 1>&2 "Can not load common library!"
    exit 1
}

# You need to be root at least via sudo for the backup utilities to work.
must_be_root

#----------------------------------------------------------------------------
DATABASE="mythconverg"
DATABASE_DIR="/data/srv/mysql/$DATABASE"
BACKUP_LIST="./root ./home ./etc
             ./var/lib/alsa/asound.state"
RESTORE_LIST="./root ./home ./etc/mythtv/modules ./etc/lirc
              ./etc/X11/xorg.conf ./etc/asound.conf ./etc/default/aumix
              ./var/lib/alsa/asound.state ./etc/mplayer/mplayer.conf
              ./etc/localtime ./etc/timezone ./etc/asound.state"
# Both BLACK_LIST and IGNORE_LIST need to have only one file per line
# They also can't have any extra whitespace...
# Files and directories we should refuse to restore
BLACK_LIST='./etc/fstab
./etc/modules
./proc
./dev
./lib
./var/lib
./myth
./myth/backup
./home/mythtv/.my.cnf
./root/.my.cnf
./home/mythtv/.Xauthority
./root/.Xauthority'
# Files we ignore as differences
IGNORE_LIST='./home/mythtv/.upgrade
./home/mythtv/.configure
./home/mythtv/.newcard
./home/mythtv/.xscreensaver
./home/mythtv/appletrailer.xml
./root/ati-driver-installer-8-01-x86.x86_64.run
./root/ati-driver-installer-8-3-x86.x86_64.run
./root/mythstreamweb.tar
./home/mythtv/.Xauthority
./root/.Xauthority'
BACKUP_DIR="/myth/backup"
BACKUP_EXTRAS="$BACKUP_DIR/backup.list"
RESTORE_EXTRAS="$BACKUP_DIR/restore.list"
BACKUP_TAR="$BACKUP_DIR/savedfiles.tar"
BACKUP_SQL="$BACKUP_DIR/$DATABASE.sql"
#DROP_SQL="/usr/local/share/knoppmyth/drop.sql"
UTIL_DIR="/usr/LH/bin"
# Do we really still need to update from myth-0.11 to myth-0.12 ?
UPDATE_SQL="/usr/share/mythtv/sql/0.11-to-0.12.sql"
UPDATE_FILES="$UTIL_DIR/restore_fixups.sh"
COMPRESSION=".gz"
SOUNDS="/usr/share/sounds"
SILENCE="$SOUNDS/half_second_of_silence.wav"
PLAYER="/usr/bin/aplay"
#----------------------------------------------------------------------------
AWK=/bin/awk
BASH=/bin/bash
BUNZIP2=/bin/bunzip2
BZIP2=/bin/bzip2
CAT=/bin/cat
CHMOD=/bin/chmod
CHOWN=/bin/chown
EGREP=/bin/egrep
GREP=/bin/grep
GUNZIP=/bin/gunzip
GZIP=/bin/gzip
LS=/bin/ls
MV=/bin/mv
RM=/bin/rm
SED=/bin/sed
TAR=/bin/tar
TR=/bin/tr
DIFF=/usr/bin/diff
FIND=/usr/bin/find
MD5SUM=/usr/bin/md5sum
MYISAMCHK=/usr/bin/myisamchk
MYSQL=/usr/bin/mysql
MYSQLADMIN=/usr/bin/mysqladmin
MYSQLDUMP=/usr/bin/mysqldump
MYTHSHUTDOWN=/usr/bin/mythshutdown
SORT=/usr/bin/sort
WC=/usr/bin/wc
SV=/sbin/sv

#----------------------------------------------------------------------------

require file+r+x $AWK $BASH $BUNZIP2 $BZIP2 $CAT $CHMOD $CHOWN $EGREP $GREP \
                 $GUNZIP $GZIP $LS $MV $RM $SED $TAR $TR $DIFF $FIND $MD5SUM \
                 $MYISAMCHK $MYSQL $MYSQLADMIN $MYSQLDUMP $MYTHSHUTDOWN \
                 $SORT $WC $SV

#require file+r "$DROP_SQL"

require dir+r+w+x /tmp "$BACKUP_DIR" "$DATABASE_DIR"

play_sound () {
    ($PLAYER $SILENCE $SOUNDS/$1 >& /dev/null)&
}

# Filter against an exclude list like the black list or the ignore list above
filter_list () {
    $GREP -vxF "$*" |
    $SORT -u
}

# Some people just can't read or follow directions... :-/
# This should track the directory names in the default backup list above
# We also use this to short circuit a certain incredibly dumb stunt
filter_redundant () {
    filter_list "$($TR -s ' ' '\n' <<<"$BACKUP_LIST ./myth")" |
    $EGREP -v '^\./(root|home|etc|myth)/' |
    $SORT -u
}

get_extras () {
    # One entry per line, and normalize the prefix
    $TR ' ' '\n' <"$1" |
    $AWK '/^$/ {next}
          /^\.\// {print $0 ; next}
          /^\// {print "." $0 ; next}
          { print "./" $0}'
}

[ -f "$BACKUP_EXTRAS" ] &&
    BACKUP_LIST="$BACKUP_LIST $(get_extras $BACKUP_EXTRAS |
                                filter_redundant)"

[ -f "$RESTORE_EXTRAS" ] &&
    RESTORE_LIST="$RESTORE_LIST $(get_extras $RESTORE_EXTRAS |
                                  filter_list "$BLACK_LIST")"

# Build tar exclusion parameters out of $BLACK_LIST
EXCLUSION=""
case $0 in
*restore)
    for file in $BLACK_LIST ; do
        EXCLUSION="$EXCLUSION --exclude $file "
    done
    ;;
*)
    ;;
esac

shrink () {
    case "$COMPRESSION" in
    .gz)
        $GZIP -9 "$@"
        ;;
    .bz2)
        $BZIP2 -9 "$@"
        ;;
    *)
        ;;
    esac
}

expand () {
    case "$*" in
    *.gz)
        $GUNZIP "$@"
        ;;
    *.bz2)
        $BUNZIP2 "$@"
        ;;
    -c\ *)
        $CAT $2 /dev/null
        ;;
    -t\ *)
        return 0
        ;;
    *)
        echo 1>&2 "Error, unknown file type!"
        return 1
        ;;
    esac
}

single_format () {
    candidates=$($LS -1 "$1.gz" "$1.bz2" "$1" 2>/dev/null)
    case $($WC -l <<<"$candidates") in
    1)  return 0  # One is good!
    	;;
    0)  echo "Error, no $1 found!"
    	return 1
        ;;
    *)  echo "Warning, multiple formats for $1 found!"
    	echo "Candidates are: $candidates"
        ;;
    esac
}

compression_type () {
    for compression in .gz .bz2 "" ; do
        if [ -f "$1$compression" ] ; then
            echo "$compression"
            return 0
        fi
    done
    return 1
}

backup_roller () { # Gets the rollover sequence to use.
    prev_i=$1 ; shift
    for i in "$@" ; do
        for c in .gz .bz2 "" ; do
            for f in $BACKUP_SQL $BACKUP_TAR ; do
                $RM -f $f$c$prev_i
                if [ -f "$f$c$i" ] ; then
                    echo "Moving $f$c$i to $f$c$prev_i"
                    $MV -f $f$c$i $f$c$prev_i
                fi
            done
        done
        prev_i="$i"
    done
}

# Given "subset A B" return true if is A a subset of B
subset () {
    cnt=$($DIFF $1 $2 | $GREP '^<' | $WC -l)
    [ "$cnt" -eq 0 ] && return 0
    $DIFF $1 $2
}

mysql_cmd () {
    $MYSQL -u root $DATABASE -sBe "$*"
}

mysql_stdin () {
    $MYSQL -u root $DATABASE -sB
}

check_files () {
    OBJECT_LIST="$*"
    LIVE_FILES=/tmp/live_files_$$
    SAVED_FILES=/tmp/saved_files_$$

    echo "Checking for the existance of the backup tar file..."
    single_format "$BACKUP_TAR"
    c=$(compression_type "$BACKUP_TAR") ||
        { echo "Error, missing tar file - '$BACKUP_TAR$c'." ; return 1 ; }
    echo "Using file $BACKUP_TAR$c"
    echo "Backup tar file exists. Checking the compression..."
    expand -t $BACKUP_TAR$c ||
        { echo "Error, bad compressed tarball - '$BACKUP_TAR$c'." ; return 1 ; }
    echo "Compression looks OK. Checking backup tar file contents..."

    echo "Generating a list of the backup contents..."
    {
    expand -c $BACKUP_TAR$c |
      $TAR tf - $OBJECT_LIST $EXCLUSION |
      $SED '/\/$/s///' |
      filter_list "$IGNORE_LIST" >$SAVED_FILES
    } 2>&1 | $SED -e '/Error exit delayed from previous errors/d'

    echo "Generating a list of the directory contents..."
    cd /
    $FIND $OBJECT_LIST \( -type d -or -type f -or -type l \) -print |
      filter_list "$IGNORE_LIST" >$LIVE_FILES

    echo "Comparing directory versus backup contents..."
    case $0 in
    *backup) # backup must contain everything selected from the directories
        subset $LIVE_FILES $SAVED_FILES
        ;;
    *restore) # directories must contain everything selected from the backup
        subset $SAVED_FILES $LIVE_FILES
        ;;
    *)
        $DIFF $LIVE_FILES $SAVED_FILES
        ;;
    esac
    FILE_STATUS=$?
    $RM $LIVE_FILES $SAVED_FILES
    if [ $FILE_STATUS -eq 0 ] ; then
        echo "Live and saved file lists match."
    else
        echo "Warning, file lists are not identical!"
    fi
    return $FILE_STATUS
}

has_records () {
    filename="$1"
    description="$2"
    if [ $($WC -l < "$filename") -eq 0 ] ; then
        echo "Warning, could not get record counts from $description!"
        return 1
    fi
    if [ $($AWK '{cnt+=$2} END {print cnt}' < "$filename") -eq 0 ] ; then
        echo "Warning, total record count from $description is zero!"
        return 1
    fi
    return 0
}

check_tables () {
    LIVE_TABLES=/tmp/live_tables_$$
    SAVED_TABLES=/tmp/saved_tables_$$

    echo "Checking for the existance of the DB dump file..."
    single_format "$BACKUP_SQL"
    c=$(compression_type "$BACKUP_SQL") ||
        { echo "Error, missing DB dump - '$BACKUP_SQL$c'" ; return 1 ; }
    echo "Using file $BACKUP_SQL$c"
    echo "DB dump file exists. Checking the compression..."
    expand -t $BACKUP_SQL$c ||
        { echo "Error, bad compressed DB dump - '$BACKUP_SQL$c'." ; return 1 ; }
    echo "Compression looks OK. Checking DB dump contents..."

    echo "Generating a list of tables and record counts in the DB dump..."
    expand -c "$BACKUP_SQL$c" |
      $AWK '/CREATE TABLE/ { tbl = $3; gsub("`","",tbl); records[tbl] = 0; } \
        /INSERT INTO/  { tbl = $3; gsub("`","",tbl); \
                         n = split(substr($0,index($0,"VALUES (")+7),vals,"\\),\\("); \
                         records[tbl] += n; } \
        END { for (tbl in records) print tbl, records[tbl]; }' |
      $SED 's/mythlog [0-9]*/mythlog 0/' |
      $SORT >$SAVED_TABLES
    has_records "$SAVED_TABLES" "DB dump" || return 1

    echo "Generating a list of tables and record counts in the live DB..."
    for tbl in $(mysql_cmd "show tables") ; do
        mysql_cmd "select '$tbl', count(*) from $tbl"
    done |
      $TR -s '\t' ' ' |
      $SED 's/mythlog [0-9]*/mythlog 0/' |
      $SORT >$LIVE_TABLES
    has_records "$LIVE_TABLES" "live DB" || return 1

    echo "Comparing live versus saved tables..."
    case $0 in
    *restore) # database must include everything from the backup
        subset $SAVED_TABLES $LIVE_TABLES
        ;;
    *)        # backup must exactly match the database
        $DIFF $LIVE_TABLES $SAVED_TABLES
        ;;
    esac
    TABLE_STATUS=$?
    $RM $LIVE_TABLES $SAVED_TABLES
    if [ $TABLE_STATUS -eq 0 ] ; then
        echo "Live and saved table lists match."
    else
        echo "Warning, table lists are not identical!"
    fi
    return $TABLE_STATUS
}

check_files_and_tables () {
    STATUS=0
    echo
    check_files "$@" || STATUS=1
    echo
    check_tables || STATUS=1
    echo
    return $STATUS
}

stop_mysqld () { $SV stop mysql ; }
start_mysqld () {
    $SV start mysql
    for t in 1 2 4 8 ; do
        ready=$(mysql_cmd 'select 1 from dual' 2>/dev/null)
        [ "$ready" = "1" ] && break
        sleep "$t"
    done
    [ "$ready" != "1" ] && echo "Error, DB not available after 15 seconds!"
}

stop_mythbackend () { $SV stop mythbackend ; }
start_mythbackend () { $SV start mythbackend ; }

lock_myth () { $MYTHSHUTDOWN --lock ; }
unlock_myth () { $MYTHSHUTDOWN --unlock ; }

true # Make sure that this shows success