<?
// Import mailer
ini_set("memory_limit","128M");
require "/cms/phpmailer/class.phpmailer.php";
// Perform backup
$filename = "/tmp/" . date('Ymd-Hi') . ".sql";
exec("mysqldump -u root -p --all-databases > {$filename}");
exec("gzip $filename");
$filename .= ".gz";
$filesize = number_format(filesize($filename) / 1048576, 0);
// Send backup
$date = date('d/m/Y');
$m = new PHPMailer();
$m->IsSMTP(true);
$m->SMTPAuth = true;
$m->Username = 'username@gmail.com';
$m->Password = 'password';
$m->FromName = 'Database Backup Mailer';
$m->Host = 'ssl://smtp.gmail.com:465';
$m->AddAddress('secondarybackup@gmail.com');
$m->AddAttachment($filename);
$m->Subject = "Database Backup";
$m->MsgHTML(nl2br("The Database Backup for {$date} is attached, the filesize is: {$filesize}MB."));
if ($m->Send()) {
echo "Success";
unlink($filename);
} else {
echo "Error, could not send: {$m->ErrorInfo}";
}
?>
The script assumes that PHPMailer is installed in the same directory as the script in a folder called phpmailer and that MySQL has a password-less root account; change the line with mysqldump if your MySQL access details are different. Furthermore the script should be placed in folder where it can be accessed via HTTP so that you do not need to install the PHP CLI to make it work. The script can then be called using wget and added to cron to perform periodic backups.
0 comments:
Post a Comment