From 3f326ae6ede5ea6e9c1d00e868d48480e90594e7 Mon Sep 17 00:00:00 2001 From: "Leigh B. Stoller" Date: Tue, 16 Jan 2001 14:56:45 +0000 Subject: [PATCH] Clean up and modify so that mail is sent only when the backup fails. --- db/backup.in | 55 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/db/backup.in b/db/backup.in index 659ca0df8..07f385bab 100755 --- a/db/backup.in +++ b/db/backup.in @@ -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); +} -- GitLab