Automatic cPanel backup using cron


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

This is a php script to trigger a cpanel full backup to the servers home directory OR a remote ftp server


Copy this code and paste it in your HTML
  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!\n"; // 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. ?>

URL: http://www.justin-cook.com/wp/2006/12/27/automatic-cpanel-backup-domain-mysql-with-cron-php/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.