Automated cPanel Full Site Backup to Offsite FTP with PHP


/ Published in: PHP
Save to your folder(s)

Save this file as a .php somewhere on your webserver - NOT A WEB ACCESSIBLE LOCATION! Edit the lines in the top portion to suit your needs and call the file from bash like this: $> php cpanel-backup.php

You can use a cronjob to make this run automatically!


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. // PHP script to allow periodic cPanel backups automatically, optionally to a remote FTP server.
  4. // This script contains passwords. KEEP ACCESS TO THIS FILE SECURE! (place it in your home dir, not /www/)
  5.  
  6. // ********* THE FOLLOWING ITEMS NEED TO BE CONFIGURED *********
  7.  
  8. // Info required for cPanel access
  9. $cpuser = "username"; // Username used to login to CPanel
  10. $cppass = "password"; // Password used to login to CPanel
  11. $domain = "domain.com"; // Domain name where CPanel is run
  12. $skin = "x"; // Set to cPanel skin you use (script won't work if it doesn't match). Most people run the default x theme
  13.  
  14. // Info required for FTP host
  15. $ftpuser = "username"; // Username for FTP account
  16. $ftppass = "password"; // Password for FTP account
  17. $ftphost = "ftp.domain.com"; // Full hostname or IP address for FTP host
  18. $ftpmode = "ftp"; // FTP mode ("ftp" for active, "passiveftp" for passive)
  19.  
  20. // Notification information
  21. $notifyemail = "[email protected]"; // Email address to send results
  22.  
  23. // Secure or non-secure mode
  24. $secure = 1; // Set to 1 for SSL (requires SSL support), otherwise will use standard HTTP
  25.  
  26. // Set to 1 to have web page result appear in your cron log
  27. $debug = 1;
  28.  
  29. // *********** NO CONFIGURATION ITEMS BELOW THIS LINE *********
  30.  
  31. if ($secure) {
  32. $url = "ssl://".$domain;
  33. $port = 2083;
  34. } else {
  35. $url = $domain;
  36. $port = 2082;
  37. }
  38.  
  39. $socket = fsockopen($url,$port);
  40. if (!$socket) { echo "Failed to open socket connection… Bailing out!\n"; exit; }
  41.  
  42. // Encode authentication string
  43. $authstr = $cpuser.":".$cppass;
  44. $pass = base64_encode($authstr);
  45.  
  46. $params = "dest=$ftpmode&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&submit=Generate Backup";
  47.  
  48. // Make POST to cPanel
  49. fputs($socket,"POST /frontend/".$skin."/backup/dofullbackup.html?".$params." HTTP/1.0
  50. ");
  51. fputs($socket,"Host: $domain
  52. ");
  53. fputs($socket,"Authorization: Basic $pass
  54. ");
  55. fputs($socket,"Connection: Close
  56. ");
  57. fputs($socket,"
  58. ");
  59.  
  60. // Grab response even if we don't do anything with it.
  61. while (!feof($socket)) {
  62. $response = fgets($socket,4096);
  63. if ($debug) echo $response;
  64. }
  65.  
  66. fclose($socket);
  67.  
  68. ?>

URL: http://www.arnoldb.com/2009/07/21/cpanel-full-site-backup-with-php/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.