使用cron的cPanel自动备份

Posted

tags:

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

This is a php script to trigger a cpanel full backup to the servers home directory OR a remote ftp server
  1. <?php
  2. /*
  3.  * fullbackup.php
  4.  * This is a php script to trigger a cpanel full backup to the servers home directory OR a remote ftp server
  5.  * KEEP ACCESS TO THIS FILE SECURE!
  6.  * Because this file contains passwords, it is safest located in the highest level root above /www/ or /public_html/
  7. */
  8.  
  9. $options = array(
  10. 'cpanel' => array(
  11. 'user' => '', // cpanel username
  12. 'pass' => '', // cpanel password
  13. 'domain' => '.com', // address to server running cpanel
  14. 'skin' => 'rvskin' ), // 'rvskinlight' for cpanel at https://example.com:2083/frontend/rvskinlight/index.html
  15. 'ftp' => array(
  16. 'enabled' => false, // true: backup to a remote server using settings below, false: ignore settings below and backup to home directory
  17. 'user' => '', // ftp username
  18. 'pass' => '', // ftp password
  19. 'host' => 'ftp.example.com', // address to ftp server
  20. 'mode' => 'passiveftp', // must be 'ftp', 'passiveftp' (most common), or 'scp'
  21. 'port' => '21', // usually '21' for passiveftp
  22. 'dir' => '' ), // (optional) existing subdirectory on ftp server
  23. 'email' => '@gmail.com', // (optional) an email will be sent to this address when the backup is finished
  24. 'ssl' => false, // must be true if this script is not on the same server as cpanel
  25. 'log' => false ); // output error/success messages to cron log?
  26.  
  27. $socket = fsockopen( ($options['ssl'] ? 'ssl:' : '') . $options['cpanel']['domain'], ($options['ssl'] ? 2083 : 2082) );
  28.  
  29. if( !$socket ) {
  30. if ($options['log']) echo "Failed to open socket connection... Exiting script! "; // cron log
  31. exit; // exit script
  32. }
  33.  
  34. // generate specific query for remote server or home directory
  35. if ($options['ftp']['enabled'])
  36. $query = 'dest=' . $options['ftp']['mode'] .
  37. '&server=' . $options['ftp']['host'] .
  38. '&user=' . $options['ftp']['user'] .
  39. '&pass=' . $options['ftp']['pass'] .
  40. '&port=' . $options['ftp']['port'].
  41. '&rdir=' . $options['ftp']['dir'];
  42. else
  43. $query = 'dest=homedir';
  44. // tack on the variables required by any query
  45. $query .= '&email=' . $options['email'] .
  46. '&submit=Generate Backup';
  47.  
  48. // simluate post to 'dofullbackup.html' in cpanel
  49. fwrite( $socket, 'POST /frontend/' . $options['cpanel']['skin'] . '/backup/dofullbackup.html?' . $query . " HTTP/1.0
  50. " );
  51. fwrite( $socket, 'Host: ' . $options['cpanel']['domain'] . "
  52. " );
  53. fwrite( $socket, 'Authorization: Basic ' . base64_encode( $options['cpanel']['user'] . ':' . $options['cpanel']['pass'] ) . "
  54. " );
  55. fwrite( $socket, "Connection: Close
  56.  
  57. " );
  58.  
  59. $response = stream_get_contents( $socket ); // record cpanel response
  60.  
  61. if ($options['log']) echo $response; // cron log
  62.  
  63. fclose($socket);
  64.  
  65. ?>

以上是关于使用cron的cPanel自动备份的主要内容,如果未能解决你的问题,请参考以下文章

在 cpanel 中使用 cron 作业运行 php 脚本

Cron mysqldump,无法读取“/etc/my.cnf.d”的目录(错误代码:2)

Cron 作业计划时间自动更改

PHP 使用PHP将自动cPanel完整站点备份到异地FTP

如何为从 cPanel 中删除“Core.XXXX”文件创建 CRON [重复]

Cpanel Cron Job Php 全局变量