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

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

my $BACKUPDIR	= "$TB/backup";

# un-taint path
13
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/usr/site/bin';
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

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

#
# Format the name of the backup with date.
# Untaint it since it was constructed with date. Dopey.
# 
my $name = "tbdb-" . `date +20%y%m%d-%H.%M.%S`;

if ($name =~ /^([-\@\w.]+)$/) {
    $name = $1;
}

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#
# 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();
}

65 66 67 68 69
#
# Do a mysqldump. This will reset the log files.
#
if (system("mysqldump --all --flush-logs --lock-tables $DBNAME > $name")) {
    print STDERR "mysqldump failed!\n";
70
    fatal();
71 72 73 74
}

if (system("gzip $name")) {
    print STDERR "gzip failed!\n";
75
    fatal();
76 77
}

78 79
#system("cat $logname | /usr/bin/mail -s '\"DB Backup Finished\"' stoller");
unlink("$logname");
80
exit 0;
81 82 83

sub fatal {
    system("cat $logname | /usr/bin/mail ".
84
	   "-s '\"DB Backup Failed\"' testbed\@fast");
85 86 87
    unlink("$logname");
    exit(1);
}