I'm trying to produce a backup script to use with cron jobs, so I have a daily backup of my database send to me via e-mail. I've looked at various scripts and came up with this, but somehow the script always produces either empty sql files or zips with empty files without file extensions. I'm not a PHP wiz, so could someone help me out with this script?
$databases = array('mydatabase');
foreach ($databases as $dbname) {
$backupFile = $tmpDir.$dbname."_".date("Y-m-d-H-i").'.gz';
$command = "mysqldump --opt -h localhost -u $dbname -p $dbpass $dbname | gzip > $backupFile";
passthru($command);
$file_size = filesize($backupFile);
$handle = fopen($backupFile, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$backupFile."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$backupFile."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($recipient,$subject,$message,$headers)) {
echo "Success!";
} else {
echo "Error!";
}
}