backup.in 2.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/perl -wT
use English;

#
# Configure variables
#
my $TB		= "@prefix@";
my $DBNAME	= "@TBDBNAME@";

my $BACKUPDIR	= "$TB/backup";
11
my $LOGDIR	= "$TB/log/mysql";
12 13

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

17 18 19 20 21 22 23 24 25
#
# Turn off line buffering on output
#
$| = 1; 

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

26 27 28 29 30 31 32 33 34
#
# Only real root can call this.
# 
if ($UID != 0) {
    print STDERR "You must be root to run this script!\n";
    exit(-1);
}

#
35 36 37 38 39 40
# Format the name of the backups with date.
#
my $extension = TBDateTimeFSSafe();
my $backname  = "tbdb-$extension";
my $basename  = "base-$extension";
    
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#
# 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)) {
63
    print STDERR "Could not chdir to $BACKUPDIR: $!\n";
64 65 66
    fatal();
}

67 68 69 70 71 72 73 74 75
#
# Move base log out of the way since flush-logs will reset it too.
#
if (-e "$LOGDIR/base") {
    if (system("/bin/mv $LOGDIR/base $LOGDIR/$basename")) {
	print STDERR "Could not move $LOGDIR/base $LOGDIR/$basename!";
    }
}

76 77 78
#
# Do a mysqldump. This will reset the log files.
#
79
if (system("mysqldump --all --flush-logs --lock-tables $DBNAME > $backname")) {
80
    print STDERR "mysqldump failed!\n";
81
    fatal();
82 83
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
84
if (system("gzip -9 $backname")) {
85 86 87 88 89 90 91 92 93 94 95 96
    print STDERR "gzip $backname failed!\n";
    fatal();
}

#
# Compress the base log too, since they get huge!
#
if (! chdir($LOGDIR)) {
    print STDERR "Could not chdir to $LOGDIR: $!\n";
    fatal();
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
97
if (-e $basename && system("gzip -9 $basename")) {
98
    print STDERR "gzip $basename failed!\n";
99
    fatal();
100 101
}

102 103
#system("cat $logname | /usr/bin/mail -s '\"DB Backup Finished\"' stoller");
unlink("$logname");
104
exit 0;
105 106 107

sub fatal {
    system("cat $logname | /usr/bin/mail ".
108
	   "-s '\"DB Backup Failed\"' testbed\@fast");
109 110 111
    unlink("$logname");
    exit(1);
}