#!/usr/bin/perl -w
################################################
########### ---===> SETTINGS <===--- ###########
################################################
$path = "/www/htdocs/xxxx/backup";
$webpath = "http://www.xxx.de/backup";
$host = "www.xxx.de";
$user = "xxx";
$pass = "xxx";
$db = "d0046c88";
$prefix = "cc";
$compressmethod = "gzip"; # can be bzip2, gzip or nothing
$compresslevel = 1; # 1-9 => Level 1-9, other values => use default
$nice = 15; # 10-20 => Priority 10-20, other values => use default: Note: the higher the value, the lower the priority
$options = "--lock-tables --disable-keys --add-drop-table --extended-insert --add-locks --all-databases --quick";
# '--opt' isn't always including the same options
################################################
########### ---===> THE CORE <===--- ###########
################################################
sub psfind
{
my $name = shift(@_);
return `ps -A` =~ /(?:$name)\s*$/im;
}
$filename = $prefix . "_" . time();
print "content-type: text/html\n\n";
$compresslv = ($compresslevel >= 1 && $compresslevel <= 9) ? " -$compresslevel" : '';
$nice = ($nice >= 10 && $nice <= 20) ? "nice -n$nice " : '';
use Switch;
switch ($compressmethod)
{
case "bzip2"
{
if (psfind('mysqldump|bzip2'))
{
print 'mysqldump or bzip2 running! Try again later.';
exit;
}
print 'MySQL-Export ...';
qx"${nice}mysqldump $options -h $host -u$user -p$pass $db 2>$path/error.txt >$path/$filename.sql";
print " done
\n";
print 'Compress the file as BZIP2 ...';
qx"${nice}bzip2$compresslv $path/$filename.sql";
print " done
\n";
print "Download
\n";
}
case "gzip"
{
if (psfind('mysqldump|gzip'))
{
print 'mysqldump or gzip running! Try again later.';
exit;
}
print 'MySQL-Export ...';
qx"${nice}mysqldump $options -h $host -u$user -p$pass $db 2>$path/error.txt >$path/dump.sql";
print " done
\n";
print 'Compress the file as GZIP ...';
qx"${nice}gzip$compresslv -c $path/dump.sql > $path/$filename.sql.gz";
print " done
\n";
print "Download
\n";
unlink "$path/dump.sql";
}
else
{
if (psfind('mysqldump'))
{
print 'mysqldump running! Try again later.';
exit;
}
print 'MySQL-Export ...';
qx"${nice}mysqldump $options -h $host -u$user -p$pass $db 2>$path/error.txt >$path/$filename.sql";
print " done
\n";
print "Download
\n";
}
}