reenadak

Backing up databases

Sep 25th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. <?php
  2.  
  3. date_default_timezone_set('Asia/Calcutta');
  4. require_once "../../variables.php";
  5. $gmessage = "Backing up databases\r\n\r\n";
  6.  
  7. function backup($sitename, $db_user, $db_pass)
  8. {
  9. $db_server = $db_user.'.db.3740856.hostedresource.com'; // Database server, usually "localhost"
  10. $db_name = $db_user; // Database name, leave empty for 'all databases'
  11. $to ="[email protected], [email protected]"; // Address to wihch mail is to be sent
  12. $subject = "Backup of ".$sitename." at ".date('Y-m-d-H-i-s'); // subject of mail
  13. $headers = "From: ".$sitename."@dak.me\r\n";
  14.  
  15. $bound_text = "Bound Text";
  16. $bound = "--".$bound_text."\r\n";
  17. $bound_last = "--".$bound_text."--\r\n"; // ========= I don't understand these three lines and their purpose ===
  18.  
  19. $date_stamp = date('Y-m-d-H-i-s');
  20. $backup_filename = ($db_name == '' ? 'all_databases' : $db_name) . '_' . $date_stamp . '.sql.gz';
  21. $cmd = 'mysqldump -u ' . $db_user . ' -h ' . $db_server . ' --password=' . $db_pass . ' ' . ($db_name == '' ? '--all-databases' : $db_name) . ' | gzip > ' . $backup_filename;
  22. $dump_status = (passthru($cmd) === false) ? 'No' : 'Yes';
  23.  
  24. $headers .= "MIME-Version: 1.0\r\n"
  25. ."Content-Type: multipart/mixed; boundary=\"$bound_text\"";
  26.  
  27. $message .= "If you can see this MIME than your client doesn't accept MIME types!\r\n"
  28. .$bound;
  29.  
  30. $message .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"
  31. ."Content-Transfer-Encoding: 7bit\r\n\r\n"
  32. ."Thank you for using dak database script from http://dak.me by <A HREF='http://mukeshdak.com'>Mukesh Dak</A>\r\n"
  33. .$bound;
  34.  
  35. $file = file_get_contents("http://zdak.in/dcode/cron/".$backup_filename); // ======= don't forget to update this path ===========
  36.  
  37. $message .= "Content-Type: application/x-zip-compressed; name=$backup_filename\"\r\n"
  38. ."Content-Transfer-Encoding: base64\r\n"
  39. ."Content-disposition: attachment; file=$backup_filename\r\n"
  40. ."\r\n"
  41. .chunk_split(base64_encode($file))
  42. .$bound_last;
  43.  
  44. if($dump_status="Yes")
  45. {
  46. if(mail($to, $subject, $message, $headers))
  47. {
  48. // $gmessage .= "Backup of database ".$db_name." of ".$sitename." has been sent to ".$to."\r\n";
  49. unlink($backup_filename);
  50. } else {
  51. $gmessage .= 'MAIL FAILED';
  52. }
  53. }
  54. else{
  55. $gmessage .= "<p>There is some problem in taking dump of your database";
  56. }
  57. return "Backup of database ".$db_name." of ".$sitename." has been sent to ".$to."\r\n";
  58. } // function backup ends here
  59.  
  60. ConnectDB();
  61.  
  62. $result = mysql_query("SELECT * FROM ".TBL_DAKDATABASE." WHERE prepared='1'") or die("<p>Error in Table Selection: ".mysql_error());
  63.  
  64. while($row = mysql_fetch_assoc($result))
  65. {
  66. $gmessage .= backup($row['linkurl'], $row['title'], $row['category']);
  67. }
  68. mysql_close(); // Close database connection
  69. //$gmessage .= backup('ShreeCement', 'shreecement', 'spiceS500');
  70. // backup of this database is causing problem
  71.  
  72. $gmessage .= "\r\nDatabase backup job completed ".$_SERVER["PHP_SELF"]."\r\n\r\n";
  73. $gmessage .= "\r\n Script: http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."\r\n\r\n";
  74.  
  75. mail("[email protected]", "Subject: Backupsuccessful", $gmessage, "From: [email protected]" );
  76. // send backup details to gmail without attachment
  77. echo "If you dont see any errors, everything went as planned";
  78. ?>
Add Comment
Please, Sign In to add comment