Commit 3f326ae6 authored by Leigh Stoller's avatar Leigh Stoller

Clean up and modify so that mail is sent only when the backup fails.

parent 04cecbac
......@@ -10,7 +10,7 @@ my $DBNAME = "@TBDBNAME@";
my $BACKUPDIR = "$TB/backup";
# un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/usr/site/bin';
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
#
......@@ -21,11 +21,6 @@ if ($UID != 0) {
exit(-1);
}
if (! chdir($BACKUPDIR)) {
print STDERR "Could not chdir to $BACKUPDIR!\n";
exit(-1);
}
#
# Format the name of the backup with date.
# Untaint it since it was constructed with date. Dopey.
......@@ -36,21 +31,57 @@ if ($name =~ /^([-\@\w.]+)$/) {
$name = $1;
}
#
# 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";
}
#
# Turn off line buffering on output
#
$| = 1;
#
# 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)) {
print STDERR "Could not chdir to $BACKUPDIR!\n";
fatal();
}
#
# Do a mysqldump. This will reset the log files.
#
if (system("mysqldump --all --flush-logs --lock-tables $DBNAME > $name")) {
print STDERR "mysqldump failed!\n";
exit(1);
fatal();
}
#
# Compress it.
#
if (system("gzip $name")) {
print STDERR "gzip failed!\n";
exit(1);
fatal();
}
print STDOUT "DB backup ($DBNAME) complete!\n";
#system("cat $logname | /usr/bin/mail -s '\"DB Backup Finished\"' stoller");
unlink("$logname");
exit 0;
sub fatal {
system("cat $logname | /usr/bin/mail ".
"-s '\"DB Backup Failed\"' testbed@fast");
unlink("$logname");
exit(1);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment