Commit 53e4e868 authored by Leigh B. Stoller's avatar Leigh B. Stoller

Add an opsmode (-o) mode to flush/roll the DB logs on ops.

parent 0612de16
...@@ -30,7 +30,7 @@ LIB_SCRIPTS = libdb.pm Node.pm libdb.py libadminctrl.pm Experiment.pm \ ...@@ -30,7 +30,7 @@ LIB_SCRIPTS = libdb.pm Node.pm libdb.py libadminctrl.pm Experiment.pm \
emdb.pm emutil.pm Firewall.pm VirtExperiment.pm libGeni.pm emdb.pm emutil.pm Firewall.pm VirtExperiment.pm libGeni.pm
# Stuff installed on plastic. # Stuff installed on plastic.
USERSBINS = genelists.proxy dumperrorlog.proxy USERSBINS = genelists.proxy dumperrorlog.proxy backup
USERLIBS = libtbdb.pm libdb.py USERLIBS = libtbdb.pm libdb.py
# These scripts installed setuid, with sudo. # These scripts installed setuid, with sudo.
......
#!/usr/bin/perl -wT #!/usr/bin/perl -wT
# #
# EMULAB-COPYRIGHT # EMULAB-COPYRIGHT
# Copyright (c) 2000-2008 University of Utah and the Flux Group. # Copyright (c) 2000-2009 University of Utah and the Flux Group.
# All rights reserved. # All rights reserved.
# #
use strict;
use English; use English;
use Getopt::Std;
# #
# Back up the DB, rolling the base and update logs. The approach is to # Back up the DB, rolling the base and update logs. The approach is to
...@@ -20,7 +21,15 @@ use English; ...@@ -20,7 +21,15 @@ use English;
# made since backup.XXX was made). This should give you a DB that is the # 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 # same as backup.XXX+1. You can go back further, and just apply all the
# subsequent update.XXX files. # subsequent update.XXX files.
# #
sub usage()
{
print("Usage: backup [-o]\n");
exit(-1);
}
my $optlist = "do";
my $debug = 0;
my $opsmode = 0;
# #
# Configure variables # Configure variables
...@@ -34,6 +43,7 @@ my $LOGDIR = "$TB/log/mysql"; ...@@ -34,6 +43,7 @@ my $LOGDIR = "$TB/log/mysql";
my $HOTCOPY = "/usr/local/bin/mysqlhotcopy"; my $HOTCOPY = "/usr/local/bin/mysqlhotcopy";
my $MYSQLDUMP = "/usr/local/bin/mysqldump"; my $MYSQLDUMP = "/usr/local/bin/mysqldump";
my $SETSITEVAR = "$TB/sbin/setsitevar"; my $SETSITEVAR = "$TB/sbin/setsitevar";
my $DBCONF = "$TB/etc/mysqld.pwd";
my $TAR = "/usr/bin/tar"; my $TAR = "/usr/bin/tar";
my $BASE = "base"; my $BASE = "base";
my $UPD = "update"; my $UPD = "update";
...@@ -41,6 +51,9 @@ my $SLOW = "slowqueries"; ...@@ -41,6 +51,9 @@ my $SLOW = "slowqueries";
my $BACK = "tbdb"; my $BACK = "tbdb";
my $extension; my $extension;
my $dohotcopy = 0; my $dohotcopy = 0;
my $dbname = "mysql";
my $dbuser = "root";
my $dbpass;
# un-taint path # un-taint path
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/usr/site/bin'; $ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/usr/site/bin';
...@@ -68,6 +81,23 @@ if ($UID != 0) { ...@@ -68,6 +81,23 @@ if ($UID != 0) {
exit(-1); exit(-1);
} }
#
# Parse command arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
usage();
}
if (defined($options{"d"})) {
$debug = 1;
}
if (defined($options{"o"})) {
$opsmode = 1;
}
if ($opsmode && $dohotcopy) {
fatal("Cannot do hotcopy mode on ops");
}
# #
# Create a temporary name for a log file and untaint it. # Create a temporary name for a log file and untaint it.
# #
...@@ -89,15 +119,30 @@ if ($logname =~ /^([-\@\w.\/]+)$/) { ...@@ -89,15 +119,30 @@ if ($logname =~ /^([-\@\w.\/]+)$/) {
open(STDERR, ">> $logname") or die("opening $logname for STDERR: $!"); open(STDERR, ">> $logname") or die("opening $logname for STDERR: $!");
open(STDOUT, ">> $logname") or die("opening $logname for STDOUT: $!"); open(STDOUT, ">> $logname") or die("opening $logname for STDOUT: $!");
if (! chdir($BACKUPDIR)) { if ($opsmode) {
fatal("Could not chdir to $BACKUPDIR: $!"); require libtbdb;
} import libtbdb;
# if (`cat $DBCONF` =~ /^([\w]*)$/) {
# Let people know the system will be sluggish. $dbpass = $1;
# }
system("$SETSITEVAR web/message '<font color=red>". else {
"Nightly backup in progress; system might be sluggish</font>'"); fatal("Could not get mysql password from $DBCONF!");
}
if (TBDBConnect($dbname, $dbuser, $dbpass) < 0) {
fatal("Could not connect to ops database!");
}
}
else {
if (! chdir($BACKUPDIR)) {
fatal("Could not chdir to $BACKUPDIR: $!");
}
#
# Let people know the system will be sluggish.
#
system("$SETSITEVAR web/message '<font color=red>".
"Nightly backup in progress; system might be sluggish</font>'");
}
# #
# Open up the index file to see what the current update file extension is. # Open up the index file to see what the current update file extension is.
...@@ -138,7 +183,8 @@ my $basename = "$BASE.$extension"; ...@@ -138,7 +183,8 @@ my $basename = "$BASE.$extension";
my $updname = "$UPD.$extension"; my $updname = "$UPD.$extension";
my $slowname = "$SLOW.$extension"; my $slowname = "$SLOW.$extension";
print "Backup file name: $backname\n"; print "Backup file name: $backname\n"
if (!$opsmode);
print "Base file name: $basename\n"; print "Base file name: $basename\n";
print "Update file name: $updname\n"; print "Update file name: $updname\n";
print "Slow file name: $slowname\n"; print "Slow file name: $slowname\n";
...@@ -157,7 +203,15 @@ if (-e "$LOGDIR/$SLOW") { ...@@ -157,7 +203,15 @@ if (-e "$LOGDIR/$SLOW") {
} }
} }
if ($dohotcopy) { if ($opsmode) {
#
# This will reset the log files.
#
if (! DBQueryWarn("flush logs")) {
fatal("mysqladmin failed!");
}
}
elsif ($dohotcopy) {
# #
# Do a hotcopy. This will reset the log files. # Do a hotcopy. This will reset the log files.
# #
...@@ -226,7 +280,8 @@ if ($dohotcopy && -e "$BACKUPDIR/tbdb") { ...@@ -226,7 +280,8 @@ if ($dohotcopy && -e "$BACKUPDIR/tbdb") {
#SENDMAIL("stoller@flux.utah.edu", #SENDMAIL("stoller@flux.utah.edu",
# "DB Backup Finished", "", undef, undef, ($logname)); # "DB Backup Finished", "", undef, undef, ($logname));
system("$SETSITEVAR web/message -"); system("$SETSITEVAR web/message -")
if (!$opsmode);
unlink("$logname"); unlink("$logname");
exit 0; exit 0;
......
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