PHP SQL DB Backup script


SUBMITTED BY: Guest

DATE: Oct. 22, 2013, 2:38 p.m.

FORMAT: PHP

SIZE: 2.3 kB

HITS: 1132

  1. <?
  2. $datestamp = date("m-d-Y_H-i-s"); // Current date to append to filename of backup file in format of YYYY-MM-DD
  3. /* CONFIGURE THE FOLLOWING SEVEN VARIABLES TO MATCH YOUR SETUP */
  4. $dbuser = "your_db_user"; // Database username
  5. $dbpwd = "Your DB Password"; // Database password
  6. $dbname = "your_database_name"; // Database name. Use --all-databases if you have more than one
  7. $filename= "/home/someuser/backup/db/backup-$datestamp.sql.gz"; // The name (and optionally path) of the dump file
  8. $to = "theemailaddresstosendtheDBto@thedomain.tld"; // Email address to send dump file to
  9. $from = "thefromaddress@thedomain.tld"; // Email address message will show as coming from.
  10. $subject = "Descriptive Subject Line to Make it Easy to See in Mail client - $datestamp"; // Subject of email
  11. $command = "/usr/bin/mysqldump -u $dbuser --password=$dbpwd $dbname | gzip > $filename";
  12. $result = passthru($command);
  13. $attachmentname = array_pop(explode("/", $filename)); // If a path was included, strip it out for the attachment name
  14. $message = "Compressed database backup file $attachmentname attached.";
  15. $mime_boundary = "<<<:" . md5(time());
  16. $data = chunk_split(base64_encode(implode("", file($filename))));
  17. $headers = "From: $from\r\n";
  18. $headers .= "MIME-Version: 1.0\r\n";
  19. $headers .= "Content-type: multipart/mixed;\r\n";
  20. $headers .= " boundary=\"".$mime_boundary."\"\r\n";
  21. $content = "This is a multi-part message in MIME format.\r\n\r\n";
  22. $content.= "--".$mime_boundary."\r\n";
  23. $content.= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
  24. $content.= "Content-Transfer-Encoding: 7bit\r\n\r\n";
  25. $content.= $message."\r\n";
  26. $content.= "--".$mime_boundary."\r\n";
  27. $content.= "Content-Disposition: attachment;\r\n";
  28. $content.= "Content-Type: Application/Octet-Stream; name=\"$attachmentname\"\r\n";
  29. $content.= "Content-Transfer-Encoding: base64\r\n\r\n";
  30. $content.= $data."\r\n";
  31. $content.= "--" . $mime_boundary . "\r\n";
  32. mail($to, $subject, $content, $headers);
  33. unlink($filename); //delete the backup file from the server
  34. ?>

comments powered by Disqus