MySQL
backup program
Here's
a little PHP script I wrote to back up MySQL databases. You can give
it several databases to back up. It will export all the data as an
SQL file (a mysqldump), compress it as a .gz file, save it on your
server, and also email it to you. Add it to your nightly crontab
file and all will be good in the world.
<?
// Mitch's database backup program (PHP) - dumps the SQL and also emails it to you
// 2005-12-09
//
// add to crontab with something like 45 0 * * * /usr/local/bin/php ~/dbbackup-scripts/msc_dbbackup.php
//Set your preferences here:
$backupdir="/usr/home/whatever/whatever/"; # ie. /usr/home/username/path/etc
$emailfrom = "\"Database Server\" ";
$emailto = "\"Your Name \" ";
$path_to_mysqldump = "/usr/local/bin/";
//define databases to be backed up
$i = 0;
//----------- Just add blocks like this to add servers
$i++;
$cfg['databases'][$i]['db'] = 'yourdatabase'; //the database name
$cfg['databases'][$i]['user'] = 'yourusername'; //the username
$cfg['databases'][$i]['host'] = 'yourdbserver'; //the database server
(name or localhost)
$cfg['databases'][$i]['password'] = 'yourdbpassword'; //the password
//---------
//---- Don't mess with stuff below here ----
$thedate = date(Ymd_His);
foreach ($cfg['databases'] as $database)
{
$db=$database['db'];
$user=$database['user'];
$host=$database['host'];
$password=$database['password'];
$filename="$db-$thedate.sql.gz";
$fullfilename=$backupdir . $filename;
dump_to_gz($db, $user, $password, $host, $backupdir, $filename, $path_to_mysqldump);
mail_attached($emailto, $emailfrom, "Backup: $filename", "This is a backup of $filename.", $backupdir, $filename, "application/x-gunzip");
}
//-------------------------------------------------------------------------------------------
function dump_to_gz ($db, $user, $password, $host, $backupdir, $filename, $path_to_mysqldump)
{
$dumpcommand = $path_to_mysqldump . "mysqldump -u$user -h$host -p$password $db";
$shellcmd = $dumpcommand . " | gzip > " . $backupdir . $filename;
//echo $shellcmd; //uncomment for debugging
system($shellcmd);
}
function mail_attached($to, $from, $subject, $messagetext, $filedir, $filename, $mimetype)
{
$fullfilename = $filedir . $filename;
$headers = "";
$unique_sep = md5(uniqid(time()));
$headers .= "From: $from\n";
$headers .= "MIME-Version: 1.0\nContent-Type:"." multipart/mixed;boundary=\"$unique_sep\";\n";
$message="";
$message .= "--$unique_sep\n";
$message .= "Content-Type: text/plain\n";
$message .= "Content-Transfer-Encoding: "."7bit\n\n";
$message .= $messagetext;
$message .= "\n\n";
if(file_exists($fullfilename)) {
$message .= "\n--$unique_sep\n";
$message .= "Content-Type: {$mimetype}; "."name=\"$filename\"\n";
$message .= "Content-Transfer-Encoding: "."base64\n";
$message .= "Content-Disposition: attachment; filename=\"$filename\"\n\n";
$filedata = implode(file($fullfilename), '');
$message .= chunk_split(base64_encode($filedata));
} else {
echo "Error: File doesn't exist. ";
}
$message .= "--$unique_sep--\n";
if(!mail($to, $subject, $message, $headers)) {
echo "Error: mail() function failed! ";
}
}
?>
|