sonpython

sondaika_autobackup_script

Mar 23rd, 2015
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.65 KB | None | 0 0
  1. <?php
  2. /*
  3. cPanel Backup Script
  4. This script will auto create backup file, transfer to FTP server
  5. By Michael Phan @ sondaika.com
  6. */
  7.  
  8. // Change to your information
  9.  
  10. $cpanel_user = "cpaneluserexample"; // change to your cpanel user
  11.  
  12. $cpanel_password = "cpaneluser_password"; // change to your cpanel password
  13.  
  14. $domain = "exampledomain.com"; // your domain
  15.  
  16. $email_to_report = "[email protected]"; // Your email to receive notification when the script finish backup. Leave it blank if you dont want to receive notification.
  17.  
  18. $ftp = true; // transfer to FTP server or not
  19.  
  20. $ftpserver = "202.150.xxx.xxx"; // your ftp server address
  21.  
  22. $ftpusername = "ftp_example_username"; // your ftp username
  23.  
  24. $ftppassword = "ftp_example_password"; // your ftp password
  25.  
  26. $ftpdirectory = "/example_path/"; // your backup directory
  27.  
  28. $daytostore = 3; // how many days do you want to store the backup file, the files which older than this number of day will be deleted
  29.  
  30.  
  31. //This part is option, the bellow information is work for almost user
  32. $ftpport = "21"; // default port for FTP connection of most hosting providers now
  33. $theme = "paper_lantern"; // change to your cpanel theme name, default is most popular theme now named paper_lantern
  34. $secure = true; // use https or not
  35.  
  36.  
  37.  
  38. // =================================================================================
  39. // ========= do not change anything bellow unless you are a master of PHP ==========
  40. // =================================================================================
  41.  
  42.  
  43. // Backup
  44.  
  45. $auth = base64_encode("$cpanel_user" . ":" . "$cpanel_password");
  46.  
  47.  
  48. if ($secure) {
  49.     $url = "ssl://" . $domain;
  50.     $port = 2083;
  51. } else {
  52.     $url = $domain;
  53.     $port = 2082;
  54. }
  55.  
  56. $socket = fsockopen($url, $port);
  57. if (!$socket) {
  58.     exit("Failed to open socket connection.");
  59. }
  60.  
  61. if ($ftp) {
  62.     $params = "dest=ftp&server=$ftpserver&user=$ftpusername&pass=$ftppassword&port=$ftpport&rdir=$ftpdirectory&submit=Generate Backup";
  63. } else {
  64.     $params = "submit=Generate Backup";
  65. }
  66.  
  67. fputs($socket, "POST /frontend/" . $theme . "/backup/dofullbackup.html?" . $params . " HTTP/1.0\r\n");
  68. fputs($socket, "Host: $domain\r\n");
  69. fputs($socket, "Authorization: Basic $auth\r\n");
  70. fputs($socket, "Connection: Close\r\n");
  71. fputs($socket, "\r\n");
  72.  
  73. while (!feof($socket)) {
  74.     $response = fgets($socket, 4096);
  75.     echo $response;
  76. }
  77.  
  78. fclose($socket);
  79.  
  80.  
  81. // delete old file from ftp if FTP set to true
  82.  
  83. if ($ftp) {
  84.     // set up basic connection
  85.     $conn_id = ftp_connect($ftpserver);
  86.  
  87.     // login with username and password
  88.     $login_result = ftp_login($conn_id, $ftpusername, $ftppassword);
  89.  
  90.     if ($login_result) {
  91.  
  92.         $filelistname = ftp_nlist($conn_id, $ftpdirectory);
  93.  
  94.         foreach ($filelistname as $key => $value) {
  95.             $file = $ftpdirectory . $value;
  96.             $mdftime = ftp_mdtm($conn_id, $file);
  97.             if (($value!==".") && ($value !=="..") && ((time() - $mdftime) > $daytostore * 86400)) {
  98.                 // try to delete $file
  99.                 if (ftp_delete($conn_id, $file)) {
  100.                  echo "$file deleted successful\n";
  101.                 } else {
  102.                  echo "could not delete $file\n";
  103.                 }
  104.             }
  105.         }
  106.  
  107.         // close the connection
  108.         ftp_close($conn_id);
  109.     }
  110.     else {
  111.         echo "FTP connection Failed";
  112.     }
  113.    
  114. }
  115.  
  116. // send report email
  117.  
  118.  
  119. if ($email_to_report !== "") {
  120.     $attime = date('d-m-Y H:i:s');
  121.     $to      = $email_to_report;
  122.     $subject = 'Auto backup for ' . $domain . ' at ' . $attime . ' successful.';
  123.     $message = 'Auto backup for ' . $domain . ' at ' . $attime . ' successful.';
  124.     $headers = 'From: autobackupreport@' . $domain . "\r\n" .
  125.         'Reply-To: autobackupreport@' . $domain . "\r\n" .
  126.         'X-Mailer: PHP/' . phpversion();
  127.  
  128.     mail($to, $subject, $message, $headers);
  129. }
  130.  
  131. ?>
Advertisement
Add Comment
Please, Sign In to add comment