#!/usr/bin/perl
use DBI;
################################################
# Copyright Gil Gruson 2002 [gil@keskydee.com] #
################################################
# Configuration starts below:

# Database configuration:

# If backing up multiple databases by name without using
# the "all" option, type database names separated by a coma.
my $database="users,avshare,zip2,phorum,cam_dunsmuir,cam_tehachapi,cam_sandpatch,cam_test,gift,movie,mysql,online,features,auth,videos,movie,railcam_gallery"; 	# "all" will backup all databases.
my $user="root";		# MySQL user name.
my $pass="masonshay";	# MySQL password.
my $host="localhost";	# MySQL host name.

# Other settings:

my $email='';		# E-mail where to send file, or leave blank.
my $ftphost="192.168.1.103";		# FTP host, or leave blank.		
my $ftpuser="webmaster";		# FTP username, or leave blank.
my $ftppass="todd21";		# FTP password, or leave blank.
my $destpath="backup";	# Destination path for FTP on remote host.
my $path="/home/tclark/backup";		# Path to directory where script resides.
my $quiet="no";		# Screen output, yes or no.
my $compression="gz";	# Compression, must be "zip" or "gz" 
my $cleanup="yes";	# Cleanup files after completion.

# Full path to mysqldump program:
# (Often /usr/local/bin)(no trailing slash)
my $mysqldump="/usr/bin";

# End of configuration, do not modify anything below!
#-----------------------------------------------------------------
if ($email ne ""){use MIME::Lite;}
if ($ftphost ne ""){use Net::FTP;}
my $dbh,$sth,$i;
my @list=();
my @bases=();

# Get database list.
if ($database eq "all"){
	$dbh=DBI->connect("DBI:mysql:mysql",$user,$pass);
	$sth=$dbh->prepare("SELECT Db from db") or die "Can't prepare query: $dbh->errstr\n";
	$sth->execute or die "Can't execute query: $sth->errstr\n";
	while (@list=$sth->fetchrow_array){push(@bases,$list[0]);}
	$sth->finish;
	$dbh->disconnect;
}

else{
	@bases=split(",",$database);}

# Print database list.
if ($quiet eq "no"){
	print "\nFound the following databases:\n\n";
	for ($i=0;$i<=$#bases;$i++){
		print "$bases[$i]\n";}
	print "\n\n";
	}
# End if.

# Dump databases to file.
for ($i=0;$i<=$#bases;$i++){
	if ($quiet ne "no"){print "Backing-up $bases[$i].\n";}
	system ("$mysqldump/mysqldump -u$user -p$pass $bases[$i] > $bases[$i].sql");
}#End for.

# Compress sql files.
if ($compression ne "zip" && $compression ne "gz"){die "No compression mode specified.";}

if ($compression eq "zip" && $quiet eq "no"){system ("zip -9 -v databases *.sql");}
if ($compression eq "zip" && $quiet eq "yes"){system ("zip -9 databases *.sql");}
if ($compression eq "gz" && $quiet eq "no"){
	system ("tar -cpf databases.tar *.sql");
	system ("gzip -9 -v databases.tar");}
if ($compression eq "gz" && $quiet eq "yes"){
	system ("tar -cpf databases.tar *.sql");
	system ("gzip -9 -q databases.tar");}

if ($quiet eq "no"){print "\nCompression finished.\n\n";}

# Send email if required.
my $time=localtime;
if ($email ne ""){
my $filename;
if ($compression eq "zip"){$filename="databases.zip"} else {$filename="databases.tar.gz"}
my $msg;
	if ($quiet eq "no"){print "\nSending Email to $email\n";}	
	$msg=MIME::Lite->new(
		From	=>"$email",
		To	=>"$email",
		Subject	=>"MySQL Database Backup $time",
		Type	=>'TEXT',
		Data	=>'Backup file is attached..',
		Encoding=>'base64');
	$msg->attach(Type=>'binary',
			Path=>"$path/$filename",
			Filename=>"$filename");
	$msg->send;
}# End if send email.

# FTP file if required.
if ($ftphost ne ""){
	my $filename;
	if ($quiet ne ""){print "\nSending file by FTP to:\n$ftphost\n";}
	if ($compression eq "zip"){$filename="databases.zip"} else {$filename="databases.tar.gz"}
	$ftp=Net::FTP->new("$ftphost");
	$ftp->login("$ftpuser","$ftppass");
	$ftp->binary();
	$ftp->cwd("$destpath/");
	$ftp->put("$filename");
	$ftp->quit;
}# end if FTP.	

# clean-up.
if ($cleanup eq "yes"){
	system ("rm *.sql");
	if ($compression eq "zip"){system ("rm databases.zip");} else {system ("rm databases.tar.gz");}
}# End if.
if ($quiet eq "no"){print "\nFiles cleaned-up.\n";}

exit; 
