Guest User

Untitled

a guest
Jan 23rd, 2019
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. <?php
  2. /* Database Backup to email by Xavi Esteve */
  3. // ======================================
  4. // ======== MODIFY SETTINGS HERE ========
  5. // ======================================
  6. $config = [
  7.  
  8. // MySQL
  9. 'db_host' => 'localhost',
  10. 'db_user' => null, // default, used if nothing declared in 'databases'
  11. 'db_pass' => null, // default, used if nothing declared in 'databases'
  12. 'db_name' => null, // default, used if nothing declared in 'databases'
  13.  
  14. // SMTP
  15. 'smtp_server' => 'smtp.gmail.com',
  16. 'smtp_port' => 587,
  17. 'smtp_user' => 'example@gmail.com',
  18. 'smtp_pass' => 'password',
  19.  
  20. // Email
  21. 'from_name' => 'Name',
  22. 'from_address' => 'example@gmail.com',
  23. 'to_name' => 'Name',
  24. 'to_address' => 'example@gmail.com',
  25.  
  26. // Server
  27. 'temp_path' => './', // path to store (temporarily) the backups, must be writable, leave at './' to use current directory
  28. 'swiftmailer_path' => './vendor/swiftmailer/swiftmailer/lib/swift_required.php',
  29.  
  30. // Databases
  31. 'databases' => [
  32. [
  33. 'db_name' => 'database1',
  34. 'db_user' => 'user1',
  35. 'db_pass' => 'password1',
  36. ],
  37. // [
  38. // 'db_name' => 'database2',
  39. // 'db_user' => 'user2',
  40. // 'db_pass' => 'password2',
  41. // ],
  42. // ...
  43. ],
  44. ];
  45. // ======================================
  46. // ========= STOP EDITING =========
  47. // ======================================
  48.  
  49.  
  50. require $config['swiftmailer_path'];
  51.  
  52. class DatabaseBackup{
  53. public $config = [];
  54.  
  55. public function __construct($config){
  56. $this->config = $config;
  57. echo date('Y-m-d H:i:s').' Database Backup started'.PHP_EOL;
  58. }
  59.  
  60.  
  61. public function backup($configs = []){
  62. $transport = (new Swift_SmtpTransport($this->config['smtp_server'], $this->config['smtp_port']) )
  63. ->setUsername($this->config['smtp_user'])
  64. ->setPassword($this->config['smtp_pass']);
  65.  
  66. // TODO: (password-protected ZIP): $this->pass = hash('sha256', date('Ymd').$this->salt );
  67.  
  68. $subject = date('Y-m-d H:i:s')." - Database Backup";
  69. $body_txt = 'Hello,
  70. Please find the backups for your database/s as of '.date('l jS \of F Y \a\t H:i:s').PHP_EOL;
  71. $body_html = '<p>Hello,</p>
  72. <p>Please find the backups for your database/s as of '.date('l jS \of F Y \a\t H:i:s').'</p>';
  73.  
  74. $message = $message = (new Swift_Message($subject) )
  75. ->setFrom([ $this->config['from_address'] => $this->config['from_name'] ])
  76. ->setTo([ $this->config['to_address'] => $this->config['to_name'] ])
  77. ->setBody($body_txt)
  78. ->addPart($body_html, 'text/html');
  79.  
  80.  
  81. foreach ($configs as &$config) {
  82. $config = array_merge($this->config, $config);
  83.  
  84. echo date('Y-m-d H:i:s').' Backing up '.$config['db_name'].'…'.PHP_EOL;
  85.  
  86. $config['backupfile'] = $config['temp_path'].date("Ymd\THis") . '-' . $config['db_name'] . '.sql';
  87. $config['backupzip'] = $config['temp_path'].$config['backupfile'] . '.tar.gz';
  88.  
  89. system("mysqldump -h ".$config['db_host']." -u ".$config['db_user']." -p".$config['db_pass']." ".$config['db_name']." > ".$config['backupfile']."");
  90. system("tar -czvf ".$config['backupzip']." ".$config['backupfile']."");
  91.  
  92. $message->attach(Swift_Attachment::fromPath($config['backupzip']));
  93. }
  94.  
  95.  
  96. $mailer = new Swift_Mailer($transport);
  97. $result = $mailer->send($message);
  98.  
  99. // Delete files from server
  100. foreach ($configs as &$config) {
  101. unlink($config['backupfile']);
  102. unlink($config['backupzip']);
  103. }
  104.  
  105. return $result;
  106. }
  107. }
  108.  
  109. $dbb = new DatabaseBackup($config);
  110. echo date('Y-m-d H:i:s').' '.$dbb->backup($config['databases']).PHP_EOL;
Add Comment
Please, Sign In to add comment