Created
January 22, 2019 15:51
-
-
Save luckyshot/e9c170c93e1d6379e59b0bd7570c6e0b to your computer and use it in GitHub Desktop.
Quick & Simple MySQL database backups using PHP & Gmail
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Database Backup to email by Xavi Esteve */ | |
// ====================================== | |
// ======== MODIFY SETTINGS HERE ======== | |
// ====================================== | |
$config = [ | |
// MySQL | |
'db_host' => 'localhost', | |
'db_user' => null, // default, used if nothing declared in 'databases' | |
'db_pass' => null, // default, used if nothing declared in 'databases' | |
'db_name' => null, // default, used if nothing declared in 'databases' | |
// SMTP | |
'smtp_server' => 'smtp.gmail.com', | |
'smtp_port' => 587, | |
'smtp_user' => 'example@gmail.com', | |
'smtp_pass' => 'password', | |
'from_name' => 'Name', | |
'from_address' => 'example@gmail.com', | |
'to_name' => 'Name', | |
'to_address' => 'example@gmail.com', | |
// Server | |
'temp_path' => './', // path to store (temporarily) the backups, must be writable, leave at './' to use current directory | |
'swiftmailer_path' => './vendor/swiftmailer/swiftmailer/lib/swift_required.php', | |
// Databases | |
'databases' => [ | |
[ | |
'db_name' => 'database1', | |
'db_user' => 'user1', | |
'db_pass' => 'password1', | |
], | |
// [ | |
// 'db_name' => 'database2', | |
// 'db_user' => 'user2', | |
// 'db_pass' => 'password2', | |
// ], | |
// ... | |
], | |
]; | |
// ====================================== | |
// ========= STOP EDITING ========= | |
// ====================================== | |
require $config['swiftmailer_path']; | |
class DatabaseBackup{ | |
public $config = []; | |
public function __construct($config){ | |
$this->config = $config; | |
echo date('Y-m-d H:i:s').' Database Backup started'.PHP_EOL; | |
} | |
public function backup($configs = []){ | |
$transport = (new Swift_SmtpTransport($this->config['smtp_server'], $this->config['smtp_port']) ) | |
->setUsername($this->config['smtp_user']) | |
->setPassword($this->config['smtp_pass']); | |
// TODO: (password-protected ZIP): $this->pass = hash('sha256', date('Ymd').$this->salt ); | |
$subject = date('Y-m-d H:i:s')." - Database Backup"; | |
$body_txt = 'Hello, | |
Please find the backups for your database/s as of '.date('l jS \of F Y \a\t H:i:s').PHP_EOL; | |
$body_html = '<p>Hello,</p> | |
<p>Please find the backups for your database/s as of '.date('l jS \of F Y \a\t H:i:s').'</p>'; | |
$message = $message = (new Swift_Message($subject) ) | |
->setFrom([ $this->config['from_address'] => $this->config['from_name'] ]) | |
->setTo([ $this->config['to_address'] => $this->config['to_name'] ]) | |
->setBody($body_txt) | |
->addPart($body_html, 'text/html'); | |
foreach ($configs as &$config) { | |
$config = array_merge($this->config, $config); | |
echo date('Y-m-d H:i:s').' Backing up '.$config['db_name'].'…'.PHP_EOL; | |
$config['backupfile'] = $config['temp_path'].date("Ymd\THis") . '-' . $config['db_name'] . '.sql'; | |
$config['backupzip'] = $config['temp_path'].$config['backupfile'] . '.tar.gz'; | |
system("mysqldump -h ".$config['db_host']." -u ".$config['db_user']." -p".$config['db_pass']." ".$config['db_name']." > ".$config['backupfile'].""); | |
system("tar -czvf ".$config['backupzip']." ".$config['backupfile'].""); | |
$message->attach(Swift_Attachment::fromPath($config['backupzip'])); | |
} | |
$mailer = new Swift_Mailer($transport); | |
$result = $mailer->send($message); | |
// Delete files from server | |
foreach ($configs as &$config) { | |
unlink($config['backupfile']); | |
unlink($config['backupzip']); | |
} | |
return $result; | |
} | |
} | |
$dbb = new DatabaseBackup($config); | |
echo date('Y-m-d H:i:s').' '.$dbb->backup($config['databases']).PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment