Current File : /home/lifechur/backups/mysql.pl
#!/usr/bin/perl
# Syntax:

#use Date::Calc qw(Add_Delta_Days);
#use Date::Calc qw(Add_Delta_YMD);

use strict;

my($i,$j,$path,$TRUE,$FALSE);
my($dsformat,$tsformat,$datestamp,$timestamp);
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
my($divider,$dataroot, $rptroot,$rptfile,$errfile,$dblistopen);
my($reccount,$rsrccount);
my($dblist,$suffix,$dbuid,$dbpwd,$recipients,$dbindex,$dbcount,$command,$result);
my($dbn,$dbs,$dbitem,@dbserver,@dbname,$dunsel,$dumpfile,$dumplist,$tarfile);

#Capture the current date and time.
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

# Now we start the main body for the program.

# Initialize some variables.
$TRUE = 1;
$FALSE = 0;
$path = $ENV{PATH};
$dataroot = "/Users/reclarke/Documents/Developer/Data/";
$dsformat = "%04d-%02d-%02d";
$tsformat = "%04d-%02d-%02d-%02d.%02d.%02d";

$year += 1900;
$mon++;
$datestamp = sprintf($dsformat,$year,$mon,$mday);
$timestamp = sprintf($tsformat,$year,$mon,$mday,$hour,$min,$sec);
$suffix = sprintf($dsformat,$year,$mon,$mday);
$divider   = "-" x(80);

$rptroot = $0;
$dbuid = "lcmdbmgr";
$dbpwd = "treeoflife";
$recipients = "pastor.rocklyn\@lifechurchboston.org";
$rptroot =~ s%^([^/]*/)*([^/]*)\.pl$%$2%;
$dblist = "dblist.txt";
$rptfile = "mysql.report";
$errfile = "mysql.errors";
print "Database List: $dblist\n";
print "Report   File: $rptfile\n";

open(DBLIST,"< $dblist") || die "Cannot open $dblist for reading: $!";
open(RPTFILE,"> $rptfile") || die "Cannot open $rptfile for reading: $!";
open(ERRFILE,"> $errfile") || die "Cannot open $errfile for reading: $!";
print RPTFILE "Database Backup Report: $timestamp\n\n";
print ERRFILE "Database Backup Error Report: $timestamp\n\n";

$reccount = 0;						# Initialize record count
$dbindex = -1;						# Initialize the database index
$dblistopen = $TRUE;				# Flag indicates that the DBLIST file is open
# Loop through each record in the HTML file.
while (<DBLIST>) {					# read a line from file DBLIST into $_
	chomp($_);						# remove trailing \n from record
	$reccount++;					# Increment record count

	# Examine database list records
	
	if ($_ =~ /^\s*$/) {				# We don't process blank records
		}
	elsif ($_ =~ /^\s*#/) {			# We don't process comment records (records with leading "#")
		print "$_\n";
		}
	else {							# Anything else should be a database record
		$dbindex++;					# Increment our database count
		$dbcount = $dbindex+1;
		$dbs = $_;
	    $dbs =~ s/^\s*(\S*)\s*(\S*)/$1/;
	    $dbn = $_;
	    $dbn =~ s/^\s*(\S*)\s*(\S*)/$2/;
	    $dbserver[$dbindex] = $dbs;
	    $dbname[$dbindex] = $dbn;
	    print "\n";
	    print "Database $dbcount:\n";
	    print "Database Server: $dbs\n";
	    print "Database Name  : $dbn\n";
	}
}

# If we detected any databases at all then prepare to dump them
if ($dbindex > 0) {
	print "$dbcount databases detected.\n\n";
		
	# Switch to the backups directory.
	print "Switching to the backups directory:\n";
	print "cd /home/lifechur/backups/;\n";
	$result = `cd /home/lifechur/backups/`;

	# Create a staging area for the MySQL backups.
	print "Creating a staging area for mySQL backups:\n";
	print "mkdir mysql;\n";
	$result = `mkdir mysql`;
}

# Loop through each command line argument searching for key values.
$dbindex = -1;
foreach $dbitem(@dbname) {
	$dbindex ++;
	$dbcount = $dbindex+1;
	$dbs = $dbserver[$dbindex];
	$dbn = $dbname[$dbindex];
	$dumpfile = "mysql/$dbn.$suffix.sql";
	print "\nBacking up database $dbcount\n";

	# Dump the databases.
	print "mysqldump --opt -u$dbuid -p$dbpwd -h $dbs $dbn > $dumpfile\n";
	$result = `mysqldump --opt -u$dbuid -p$dbpwd -h $dbs $dbn --result-file $dumpfile 2>&1`;
	print RPTFILE "mysqldump --opt -u$dbuid -p$dbpwd -h $dbs $dbn > $dumpfile\n";
	print RPTFILE "\n$result\n";
}

# If we've dumped any databases at all merge them into a tar archive
if ($dbindex > 0) {
	$dumplist = "archives/mysql_dumps.$suffix.list";
	$tarfile = "archives/mysql_backup.$suffix.tar";

	# Create a listing of the database dump files.
	print "\nCreating a listing of the database dump files:\n";
	print "ls -al mysql/* > $dumplist\n";
	$result = `ls -al mysql/* > $dumplist`;
	print RPTFILE "\n$result\n";

	# Create a tar archive of the database dump files.
	print "\nCreating a tar archive of the database dump files:\n";
	print "tar -cf $tarfile mysql/*\n";
	$result = `tar -cf $tarfile mysql/*`;
	print RPTFILE "\n$result\n";

	# Mail the tar archive to interested parties.
	print "Mailing the tar archive:\n";
	print "/usr/bin/mutt -a $tarfile -s \"MySQL Backup\" $recipients < $dumplist\n";
	$result = `/usr/bin/mutt -a $tarfile -s \"MySQL Backup\" $recipients < $dumplist`;

	# Delete the staging area.
	#print "Deleting the staging area:\n";
	#print "rm -r mysql/\n";
	#$result = `rm -r mysql/`;

}

print "Final Record Count: $reccount\n";
if ($dblistopen) {close(DBLIST) || die "can't close  $path: $!\n";}
close(RPTFILE) || die "can't close  $path: $!\n";
exit(0);