Skip to content

Instantly share code, notes, and snippets.

@Mark-H
Created May 7, 2012 14:47
Show Gist options
  • Save Mark-H/2628177 to your computer and use it in GitHub Desktop.
Save Mark-H/2628177 to your computer and use it in GitHub Desktop.
MODX Snippet to take a database backup and to email that.
<?php
$mailto = (isset($mailto)) ? $mailto : '@@ DEFAULT EMAIL HERE @@';
$warningMail = '@@ EMAIL TO SEND WARNING TO IN CASE OF A SMALL BACKUP FILE @@';
include MODX_CORE_PATH.'/config/'.MODX_CONFIG_KEY.'.inc.php';
$host = $database_server; // database host
$dbuser = $database_user; // database user name
$dbpswd = $database_password; // database password
$mysqldb = $dbase; // name of database
$path = MODX_CORE_PATH . '/_sqlbackups'; // full server path to the directory where you want the backup files (no trailing slash)
$ignoreTables = array(
'modx_manager_log'
);
// modify the above values to fit your environment
$filename = $path . '/backup-' . date('Y-m-d_H-i') . '.sql';
if ( file_exists($filename) ) unlink($filename);
$cmd = 'mysqldump --user="' . $dbuser . '" --password="' . $dbpswd . '" --host="' . $host . '" ';
$cmd .= '--skip-add-locks --skip-add-drop-table ';
foreach ($ignoreTables as $table) {
$cmd .= '--ignore-table="'.$mysqldb.'.'.$table.'" ';
}
$cmd .= $mysqldb . ' > ' . $filename;
system($cmd,$result);
$size = filesize($filename);
if ($size >= 1048576) {
$size = round($size/1048576) . " MB";
} elseif ($size >= 1024) {
$size = round($size/1024) . " KB";
} else {
// Backup smaller than 1024? Sounds like something is wrong, send email.
$message = '<p>The automated database backup, attached, was smaller than 1024 bytes. This probably indicates an error!</p>';
$message .= '<p>Return: '.$result.' | Filesize: '.$size.'</p>';
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY,$message);
$modx->mail->set(modMail::MAIL_FROM,$modx->getOption('emailsender'));
$modx->mail->set(modMail::MAIL_FROM_NAME,'Automated Backups');
$modx->mail->set(modMail::MAIL_SENDER,'Automated Backups');
$modx->mail->set(modMail::MAIL_SUBJECT,'WARNING: Small Backup for '.date('Y-m-d'));
$modx->mail->address('to',$warningMail);
$modx->mail->address('reply-to',$modx->getOption('emailsender'));
$modx->mail->mailer->AddAttachment($filename);
$modx->mail->setHTML(true);
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to email the automated backup: '.$modx->mail->mailer->ErrorInfo);
}
$modx->mail->reset();
}
$message = '<p>Backup of '.date('Y-m-d H:i').' attached.</p>';
$message .= '<p>Error: '.$result.' | Size: '.$size.'</p>';
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY,$message);
$modx->mail->set(modMail::MAIL_FROM,$modx->getOption('emailsender'));
$modx->mail->set(modMail::MAIL_FROM_NAME,'Automated Backups');
$modx->mail->set(modMail::MAIL_SENDER,'Automated Backups');
$modx->mail->set(modMail::MAIL_SUBJECT,'Backup for '.date('Y-m-d'));
$modx->mail->address('to',$mailto);
$modx->mail->address('reply-to',$modx->getOption('emailsender'));
$modx->mail->mailer->AddAttachment($filename);
$modx->mail->setHTML(true);
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to email the automated backup: '.$modx->mail->mailer->ErrorInfo);
}
$modx->mail->reset();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment