用PHP实现cPanel到异地FTP的自动全站备份

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用PHP实现cPanel到异地FTP的自动全站备份相关的知识,希望对你有一定的参考价值。

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!
  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! "; 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. ?>

以上是关于用PHP实现cPanel到异地FTP的自动全站备份的主要内容,如果未能解决你的问题,请参考以下文章

mysql自动备份脚本及异地定时FTP

mysql自动备份脚本及异地定时FTP

ftp异地备份

centos 7自动备份文件到远端FTP服务器

用winrar和ftp命令实现自动备份文件并自动上传到指定的ftp服务器

linux 定时自动异地备份