backup.in 3.53 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
use English;

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#
# Back up the DB, rolling the base and update logs. The approach is to
# open up the index file and see what the name of the current update file
# is. It has a numeric extension. Rename the base log to base.XXX,
# and then snapshot the DB into backup.XXX So, the combination of
# update.XXX and base.XXX is the DB history since the last time the script
# was run. The file backup.XXX corresponds to the DB at this point in time.
#
# To restore (or track the changes of) a DB, simply take backup.XXX and 
# apply the changes that are stored in update.XXX+1 (which are the changes
# made since backup.XXX was made). This should give you a DB that is the
# same as backup.XXX+1. You can go back further, and just apply all the
# subsequent update.XXX files. 
# 

19 20 21 22 23
#
# Configure variables
#
my $TB		= "@prefix@";
my $DBNAME	= "@TBDBNAME@";
24
my $TBOPS       = "@TBOPSEMAIL@";
25 26

my $BACKUPDIR	= "$TB/backup";
27
my $LOGDIR	= "$TB/log/mysql";
28 29 30
my $BASE	= "base";
my $BACK	= "tbdb";
my $extension;
31 32

# un-taint path
33
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/usr/site/bin';
34 35
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

36 37 38 39 40 41 42 43 44
#
# Turn off line buffering on output
#
$| = 1; 

# Load the Testbed support stuff.
use lib "@prefix@/lib";
use libtestbed;

45 46 47 48 49 50 51 52
#
# Only real root can call this.
# 
if ($UID != 0) {
    print STDERR "You must be root to run this script!\n";
    exit(-1);
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#
# Create a temporary name for a log file and untaint it.
#
#
# Form a temp name.
#
my $logname = `mktemp /tmp/dbbackup.XXXXXX`;

if ($logname =~ /^([-\@\w.\/]+)$/) {
    $logname = $1;
} else {
    die "Bad data in $logname";
}

#
# Reopen both stdout and stderr so that we can record all the output for
# later mailing.
# 
open(STDERR, ">> $logname") or die("opening $logname for STDERR: $!");
open(STDOUT, ">> $logname") or die("opening $logname for STDOUT: $!");

if (! chdir($BACKUPDIR)) {
75
    fatal("Could not chdir to $BACKUPDIR: $!");
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
#
# Open up the index file to see what the current update file extension is.
# The base/backup files correspond to this most recent update file, and
# so should be named with that extension.
#
# Read through to the last line and get its extension. This becomes
# the extension for the other files. No locking is needed.
#
open(IDX, "< $LOGDIR/update.index") or
    fatal("Could not open $LOGDIR/update.index: $!");

while (<IDX>) {
    if ($_ =~ /^.*\.([0-9]*)$/) {
	$extension = $1;
    }
}
close(IDX);
print "Extension:        $extension\n";

my $backname  = "$BACK.$extension";
my $basename  = "$BASE.$extension";

print "Backup file name: $backname\n";
print "Base file name:   $basename\n";

103 104 105
#
# Move base log out of the way since flush-logs will reset it too.
#
106 107 108
if (-e "$LOGDIR/$BASE") {
    if (system("/bin/mv $LOGDIR/$BASE $LOGDIR/$basename")) {
	print STDERR "Could not move $LOGDIR/$BASE to $LOGDIR/$basename!";
109 110 111
    }
}

112 113 114
#
# Do a mysqldump. This will reset the log files.
#
115
if (system("mysqldump --all --flush-logs --lock-tables $DBNAME > $backname")) {
116
    fatal("mysqldump failed!");
117 118
}

119 120 121
#
# Compress the files.
# 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
122
if (system("gzip -9 $backname")) {
123
    fatal("gzip $backname failed!");
124 125 126
}

if (! chdir($LOGDIR)) {
127
    fatal("Could not chdir to $LOGDIR: $!");
128 129
}

130 131
if (-e "$basename" && system("gzip -9 $basename")) {
    fatal("gzip $basename failed!");
132 133
}

134 135
#system("cat $logname | /usr/bin/mail -s '\"DB Backup Finished\"' stoller");
unlink("$logname");
136
exit 0;
137

138 139 140 141 142
sub fatal($) {
    my ($msg) = @_;

    print STDERR "$msg\n";
    
143
    system("cat $logname | /usr/bin/mail ".
144
	   "-s '\"DB Backup Failed\"' \"$TBOPS\"");
145 146 147
    unlink("$logname");
    exit(1);
}