backup.in 5.58 KB
Newer Older
1
#!/usr/bin/perl -wT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3 4

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2003, 2006 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6 7
# All rights reserved.
#
8 9
use English;

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#
# 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. 
# 

25 26 27 28 29
#
# Configure variables
#
my $TB		= "@prefix@";
my $DBNAME	= "@TBDBNAME@";
30
my $TBOPS       = "@TBOPSEMAIL@";
31 32

my $BACKUPDIR	= "$TB/backup";
33
my $LOGDIR	= "$TB/log/mysql";
34 35 36
my $HOTCOPY     = "/usr/local/bin/mysqlhotcopy";
my $MYSQLDUMP   = "/usr/local/bin/mysqldump";
my $TAR         = "/usr/bin/tar";
37
my $BASE	= "base";
Mac Newbold's avatar
Mac Newbold committed
38
my $UPD		= "update";
39
my $SLOW	= "slowqueries";
40 41
my $BACK	= "tbdb";
my $extension;
42
my $dohotcopy   = 0;
43 44

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

48 49 50 51 52 53 54 55 56
#
# Turn off line buffering on output
#
$| = 1; 

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

57 58 59 60 61
#
# Function prototypes
#
sub fatal($);

62 63 64 65 66 67 68 69
#
# Only real root can call this.
# 
if ($UID != 0) {
    print STDERR "You must be root to run this script!\n";
    exit(-1);
}

70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#
# 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)) {
92
    fatal("Could not chdir to $BACKUPDIR: $!");
93 94
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#
# 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";

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
#
# Use mysqlhotcopy if installed.
#
if ($dohotcopy) {
    #
    # We use hotcopy, so make sure the target directory we are going to use
    # does not already exist.
    #
    if (-e "tbdb") {
	system("/bin/rm -r tbdb") == 0
	    or fatal("Could not remove old backup directory!");
    }
    $BACK .= ".tgz";
}

129 130
my $backname  = "$BACK.$extension";
my $basename  = "$BASE.$extension";
131 132
my $updname   = "$UPD.$extension";
my $slowname  = "$SLOW.$extension";
133 134 135

print "Backup file name: $backname\n";
print "Base file name:   $basename\n";
Mac Newbold's avatar
Mac Newbold committed
136
print "Update file name: $updname\n";
137
print "Slow file name:   $slowname\n";
138

139 140 141
#
# Move base log out of the way since flush-logs will reset it too.
#
142 143 144
if (-e "$LOGDIR/$BASE") {
    if (system("/bin/mv $LOGDIR/$BASE $LOGDIR/$basename")) {
	print STDERR "Could not move $LOGDIR/$BASE to $LOGDIR/$basename!";
145 146
    }
}
147 148 149 150 151
if (-e "$LOGDIR/$SLOW") {
    if (system("/bin/mv $LOGDIR/$SLOW $LOGDIR/$slowname")) {
	print STDERR "Could not move $LOGDIR/$SLOW to $LOGDIR/$slowname!";
    }
}
152

153 154 155 156 157 158 159 160 161 162 163 164 165 166
if ($dohotcopy) {
    #
    # Do a hotcopy. This will reset the log files.
    #
    if (system("$HOTCOPY --noindices --flushlog $DBNAME $BACKUPDIR")) {
	fatal("$HOTCOPY failed!");
    }

    #
    # Tar and compress the directory.
    # 
    if (system("$TAR -zcf $backname $DBNAME")) {
	fatal("$TAR -zcf $backname failed!");
    }
167
}
168 169 170 171 172 173 174 175
else {
    #
    # Do a mysqldump. This will reset the log files.
    #
    if (system("$MYSQLDUMP --all ".
	       "--flush-logs --lock-tables $DBNAME > $backname")) {
	fatal("mysqldump failed!");
    }
176

177 178 179 180 181 182
    #
    # Compress the files.
    # 
    if (system("nice gzip $backname")) {
	fatal("gzip $backname failed!");
    }
183 184
}

185 186
# Should do this too, but right now I do it by hand ...
if (0) {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
187
    system("find . \( -name 'tbdb.*.gz' \) -mtime +40 -print -delete");
188 189
}

190
if (! chdir($LOGDIR)) {
191
    fatal("Could not chdir to $LOGDIR: $!");
192 193
}

194
if (-e "$basename" && system("nice gzip $basename")) {
195
    fatal("gzip $basename failed!");
196 197
}

198
if (-e "$updname" && system("nice gzip $updname")) {
Mac Newbold's avatar
Mac Newbold committed
199 200 201
    fatal("gzip $updname failed!");
}

202
if (-e "$slowname" && system("nice gzip $slowname")) {
203 204 205 206 207 208 209 210 211 212
    fatal("gzip $slowname failed!");
}

# Should do this too, but right now I do it by hand ...
if (0) {
    system("find . \( -name 'base.*.gz' -o -name 'update.*.gz' ".
	   "         -o -name 'slowqueries.*.gz' \) ".
	   "-mtime +35 -print -delete");
}

213 214 215 216 217 218
# Do this now that the backup is complete.
if ($dohotcopy && -e "$BACKUPDIR/tbdb") {
    system("/bin/rm -r $BACKUPDIR/tbdb") == 0
	or fatal("Could not remove old backup directory!");
}

219 220
#SENDMAIL("stoller@flux.utah.edu",
#	 "DB Backup Finished", "", undef, undef, ($logname));
221

222
unlink("$logname");
223
exit 0;
224

225 226 227 228
sub fatal($) {
    my ($msg) = @_;

    print STDERR "$msg\n";
229 230

    SENDMAIL($TBOPS, "DB Backup Failed", $msg, undef, undef, ($logname));
231 232 233
    unlink("$logname");
    exit(1);
}