Loading...   


############################################################
#::: Script: DB_Dumper.pl
#::: Purpose: Utility to easily manage database backups and compress.
#:::	Export Individual DB Tables...
#::: 	Currently Built and only supported for Windows
#::: Requires: WinRAR for Compression if you use it (Recommended)
#::: Author: Akkadius
############################################################

$localdrive = "C:"; #::: Where Windows and all Install Programs are...

use POSIX qw(strftime);
my $date = strftime "%m-%d-%Y", localtime;
print "\nTodays Date: " . $date . "\n";

if(!$ARGV[0]){
	print "\nERROR! Need arguments\n\n";
	print "#::: Help :::#\n";
	print "######################################################\n\n";
	print "Arguments\n";
	print "	loc=\"C:\\File Location\" 	- File path location to backup...\n";
	print "	compress 		- Compress Database with 7-ZIP, will fallback to WinRAR depending on what is installed (Must be installed to default program dir)...\n\n";
	print '	Example: perl DB_Dumper.pl Loc="E:\Backups"' . "\n\n";
	print "######################################################\n";
	exit;
}

$Debug = 1;
print "Arguments\n" if $Debug;
$n = 0;
while($ARGV[$n]){
	print $n . ': ' . $ARGV[$n] . "\n" if $Debug;
	if($ARGV[$n]=~/compress/i){ 
		print "Compression SET\n"; 
		$Compress = 1;
	}
	if($ARGV[$n]=~/loc/i){ 
		@B_LOC = split('=', $ARGV[$n]); 
		print "Backup Directory: " . $B_LOC[1] . "\n"; 
	}
	$n++;
}

if (-d $B_LOC[1]) {
	print "Directory currently exists... Adding files to it...\n\n";
}
else{
	print "Directory does NOT exist! Creating...\n\n";
	mkdir($B_LOC[1]) or die 'Failed to create folder, maybe created the folder manually at "' . $B_LOC[1]. '" ?';
}

#::: CONFIG VARIABLES - Parsed from eqemu_config.xml 

my $confile = "eqemu_config.xml"; #default
open(F, "<$confile") or die "Unable to open config: $confile - This must be in your EQEmu Server Folder with your XML config\n";
my $indb = 0;

while(<F>) {
	s/\r//g;
	if(/<database>/i) { $indb = 1; }
	next unless($indb == 1);
	if(/<\/database>/i) { $indb = 0; last; }
	if(/<host>(.*)<\/host>/i) { $host = $1; } 
	elsif(/<username>(.*)<\/username>/i) { $user = $1; } 
	elsif(/<password>(.*)<\/password>/i) { $pass = $1; } 
	elsif(/<db>(.*)<\/db>/i) { $db = $1; }
}

print "Backing up Database " . $db . "... \n\n"; 
system('mysqldump -u' . $user . ' -p' . $pass . ' ' . $db . ' > "' . $B_LOC[1] . '\\' . $db . ' ' . $date . '.sql"');
$fileloc = '' . $B_LOC[1] . '\\' . $db . ' ' . $date . '.sql';
print get_filesize_str($fileloc) . "\n";
$filesize = -s $fileloc;
print "Backup DONE... File size '" . $filesize . "' \n\n"; 

#::: WinRar Get, check compression flag
if($Compress == 1){
	if(-d $localdrive . "\\Program Files\\7-Zip"){
		print "You have 7-Zip installed as 64 Bit...\n\n";
		$S_ZIP = $localdrive . "\\Program Files\\7-Zip";
	}
	elsif(-d $localdrive . "\\Program Files (x86)\\7-Zip"){
		print "You have 7-Zip installed as 32 Bit...\n\n";
		$S_ZIP = $localdrive . "\\Program Files (x86)\\7-Zip";
	}
	elsif(-d $localdrive . "\\Program Files (x86)\\WinRAR"){
		print "You have WinRAR installed as 32 Bit...\n\n";
		$WinRar = $localdrive . "\\Program Files (x86)\\WinRAR";
	}
	elsif(-d $localdrive . "\\Program Files\\WinRAR"){
		print "You have WinRAR installed as 64 Bit...\n\n";
		$WinRar = $localdrive . "\\Program Files\\WinRAR";
	}
	else{
		print "No WinRAR installed... Will not compress...\n";
	}
	if($S_ZIP ne ""){
		print "Compressing Database with 7-ZIP... \n"; 
		system('"' . $S_ZIP . '\\7z" a -t7z -m0=lzma -mx=9 "' . $B_LOC[1] . '\\' . $db . ' ' . $date . '.7z" "' . $B_LOC[1] . '\\' . $db . ' ' . $date . '.sql" ');
		print "Deleting RAW .sql Dump... \n"; 
		#system('del "' . $B_LOC[1] . '\\' . $db . ' ' . $date . '.sql" ');
	}
	elsif($WinRar ne ""){
		print "Compressing Database with WinRAR... \n"; 
		system('"' . $WinRar . '\\rar" a "' . $B_LOC[1] . '\\' . $db . ' ' . $date . '.rar" "' . $B_LOC[1] . '\\' . $db . ' ' . $date . '.sql" ');
		print "Deleting RAW .sql Dump... \n"; 
		system('del "' . $B_LOC[1] . '\\' . $db . ' ' . $date . '.sql" ');
	}
}

sub get_filesize_str{
    my $file = shift();
    my $size = (stat($file))[7] || die "stat($file): $!\n";
    if ($size > 1099511627776)	{	return sprintf("%.2f TiB", $size / 1099511627776);  }
    elsif ($size > 1073741824)  {	return sprintf("%.2f GiB", $size / 1073741824);  }
    elsif ($size > 1048576) 	{   return sprintf("%.2f MiB", $size / 1048576);   }
    elsif ($size > 1024) 		{	return sprintf("%.2f KiB", $size / 1024);  }
    else  { return "$size byte" . ($size == 1 ? "" : "s"); }
}

Raw Paste Data