December 27th, 2006

Automatic cPanel backup (domain & MySQL) with cron & PHP

OK, so I posed the question, and I found the answer. (Of course I had to find it myself, I just don't have people who regularly read my posts and do research for me!)

The situation is this: I use cPanel on my web hosting server. I use the cPanel backup tool to regularly backup my home directory (includes my web files, mail, etc), and my MySQL databases. I love the fact that you can use it to backup to a remote FTP server, and I do that on a fairly regular basis.

The major drawback is that you have to remember/schedule to perform this backup manually. This becomes especially difficult if you have multiple cPanel accounts. It would be amazing if you could just schedule the cPanel backup to run at regular intervals, perhaps with cron. Well, although there's no option for that in cPanel, the script below will allow you to do exactly that!

This backup script includes SSL support. This isn't necessary if you run the script on the server for which you're generating the backup; but the SSL support is very important if you're running the script somewhere else to connect to your cPanel hosting account.

<?php

// PHP script to allow periodic cPanel backups automatically, optionally to a remote FTP server.
// This script contains passwords.  KEEP ACCESS TO THIS FILE SECURE! (place it in your home dir, not /www/)

// ********* THE FOLLOWING ITEMS NEED TO BE CONFIGURED *********

// Info required for cPanel access
$cpuser = "username"; // Username used to login to CPanel
$cppass = "password"; // Password used to login to CPanel
$domain = "example.com"; // Domain name where CPanel is run
$skin = "x"; // Set to cPanel skin you use (script won't work if it doesn't match). Most people run the default x theme

// Info required for FTP host
$ftpuser = "ftpusername"; // Username for FTP account
$ftppass = "ftppassword"; // Password for FTP account
$ftphost = "ftp.example.com"; // Full hostname or IP address for FTP host
$ftpmode = "ftp"; // FTP mode ("ftp" for active, "passiveftp" for passive)

// Notification information
$notifyemail = "you@example.com"; // Email address to send results

// Secure or non-secure mode
$secure = 0; // Set to 1 for SSL (requires SSL support), otherwise will use standard HTTP

// Set to 1 to have web page result appear in your cron log
$debug = 0;

// *********** NO CONFIGURATION ITEMS BELOW THIS LINE *********

if ($secure) {
   $url = "ssl://".$domain;
   $port = 2083;
} else {
   $url = $domain;
   $port = 2082;
}

$socket = fsockopen($url,$port);
if (!$socket) { echo "Failed to open socket connection… Bailing out!\n"; exit; }

// Encode authentication string
$authstr = $cpuser.":".$cppass;
$pass = base64_encode($authstr);

$params = "dest=$ftpmode&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&submit=Generate Backup";

// Make POST to cPanel
fputs($socket,"POST /frontend/".$skin."/backup/dofullbackup.html?".$params." HTTP/1.0\r\n");
fputs($socket,"Host: $domain\r\n");
fputs($socket,"Authorization: Basic $pass\r\n");
fputs($socket,"Connection: Close\r\n");
fputs($socket,"\r\n");

// Grab response even if we don't do anything with it.
while (!feof($socket)) {
  $response = fgets($socket,4096);
  if ($debug) echo $response;
}

fclose($socket);

?>

To schedule the script to run regularly, save it as fullbackup.php in your top directory (not /public_html, which would be less secure), and enter a new cron job like the following:

15 2 * * * /usr/local/bin/php /home/youraccount/fullbackup.php

(Runs every night at 2:15 a.m.)

or

15 2 * * 1 /usr/local/bin/php /home/youraccount/fullbackup.php

(Runs every Sunday night at 2:15 a.m.)

65 Responses to ' Automatic cPanel backup (domain & MySQL) with cron & PHP '

Subscribe to comments with RSS or TrackBack to ' Automatic cPanel backup (domain & MySQL) with cron & PHP '.

  1. James Malcolmson said,

    on February 3rd, 2007 at 4:00 am

    Hi. Thanks for posting this script - it was exactly what I was after - I've added it to my server and am using an online file storage service to send the backup to. However, there is one slight problem that I'm hoping you may be able to help with….

    The file is not being backed up to the remote server because the remote server uses PassiveFTP. In the e-mail confirmation generated by your script it says:

    Failed to create ActiveFTP Data Connection. (Blocked by firewall?) Please use PassiveFTP, or download our file manager software.

    Is there anyway I can modify the script to enable, support and use passiveFTP?

  2. James Malcolmson said,

    on February 3rd, 2007 at 4:04 am

    sorry, just spotted the "add passiveftp" for passiveftp line - i'll try that.. :)

  3. Bob said,

    on February 10th, 2007 at 12:39 pm

    Awesome, just what I was looking for. I haven`t tried it yet. I assume the script when FTPing a backup (tar), it would replace an existing backup file with the same name on the remote host/server?

  4. Justin Cook said,

    on February 10th, 2007 at 1:52 pm

    That shouldn't be an issue, as the cPanel backup filenames contain the usernme, date and exact time, so they should all be unique

  5. Bob said,

    on February 10th, 2007 at 3:11 pm

    Actually, I would prefer they simply replace previous backups. I don`t want to see tar files piling up on the remote server and hogging all the space.

    I`m getting an error from the script report..

    Can't call method "login" on an undefined value at /usr/local/cpanel/bin/ftpput line 28.

    Thanks.

  6. Bob said,

    on February 10th, 2007 at 3:40 pm

    Never mind, I had the wrong ftp domain/ip in the script. Is there anyway that when cpanel has ftp`d the backup to the remote host/server, it removes the previous backup uploaded? I`m just trying to avoid having a bunch of backups taking all the space on the server. I only need one backup, that`s the most recent, not an archive of them. Thanks again.

  7. Justin Cook said,

    on February 12th, 2007 at 7:51 am

    I don't know, you'd have to script that yourself, add an extra step to delete the file(s).

    I wouldn't do that though, in case you need a backup from a week ago. Why not just archive them to DVD or whatever once the drive is getting full?

  8. James Malcolmson said,

    on February 12th, 2007 at 11:06 am

    Is there any way to further configure this script to enable it to upload the backup file to a particular folder on the FTP account?

  9. Justin Cook said,

    on February 12th, 2007 at 11:08 am

    To do that, I would create a unique FTP user on the FTP server, and make their home directory the backup dir

  10. James Malcolmson said,

    on February 12th, 2007 at 11:11 am

    ..and if that isn't possible - like for a file space associated with an e-mail account?

  11. Justin Cook said,

    on February 12th, 2007 at 11:26 am

    Try this (untested):
    below this line: $ftpmode = "ftp";
    add: $ftpdir = "/yourdir";

    and change this line:
    $params = "dest=$ftpmode&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&submit=Generate Backup";

    to:
    $params = "dest=$ftpmode&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&rdir=$ftpdir&submit=Generate Backup";

  12. Michael said,

    on February 15th, 2007 at 10:58 am

    This is the most exciting article I've read all week.. (I've been looking for exactly this for a while now).

    Thank you.

  13. Bobby said,

    on April 11th, 2007 at 5:35 am

    I would like to send the backup to my email rather than to an FTP server - is this possible and how can I do this/what modifications do I need to do to the above script?

  14. Justin Cook said,

    on April 11th, 2007 at 8:53 am

    Sorry, cPanel does not provide that functionality. And that wouldn't be a very reliable means of backup, especially as your website grows in size

  15. Bobby said,

    on April 11th, 2007 at 9:24 am

    Would you be able to suggest a (free?) online file storage service which allows ftp access so i can do this?

    Please.

  16. Justin Cook said,

    on April 11th, 2007 at 9:27 am

    both idrive.com and xdrive.com offer some free storage, but I'm not sure if either provide FTP access yet…

  17. Bobby said,

    on April 12th, 2007 at 1:05 am

    Ok, Ive got the script to work and I found a free FTP online file sharing service but everytime i run the script, it leaves behind in the /home/username/ directory an empty .tar.gz file (supposedly the backup, which has been ftp'd to the other server). Should it be doing that?

  18. Justin Cook said,

    on April 12th, 2007 at 7:14 am

    Which free ftp service are you using? Is it secure?

    And yes, I believe it will always leave an empty .gz file

  19. Bobby said,

    on April 12th, 2007 at 10:31 am

    I'm using FileHo!

    And I'm not sure if its secure - I don't think it is - but it does the job well.

  20. Markas said,

    on April 26th, 2007 at 11:06 pm

    I am trying to run this script in cPanel but I keep getting a Permission Denied error in my email…anyway idea why?

  21. Justin Cook said,

    on April 27th, 2007 at 7:12 am

    Check with your host, see if your cron privileges are ok

  22. Hamza said,

    on April 28th, 2007 at 3:15 am

    That cron code that you've given, where do we insert it?
    I mean, in the basic mode, or the advanced(unix) mode?

  23. Justin Cook said,

    on April 28th, 2007 at 7:04 am

    It would be in the advanced cron mode


  24. on April 28th, 2007 at 12:25 pm

    Thank you, this script worked great.

  25. Bill Whelan said,

    on May 10th, 2007 at 10:48 am

    Great script Justin. Is it possible to pull a backup of the MySQL datase only?
    My site rarely changes but the database changes every day. I would like to back them up individually and with different frequency.

    Thanks in advance

  26. Justin Cook said,

    on May 10th, 2007 at 12:16 pm

    No, this script could not be modified for that purpose, because cPanel does not have anything built into it to FTP the databases only

  27. Ivo said,

    on May 11th, 2007 at 12:32 pm

    hi, i execute my script like "php -q /home/eis-software/CronBack.php" and returns "No input file specified" what can i do?
    thanks in advance

  28. Bill Whelan said,

    on May 11th, 2007 at 3:25 pm

    Justin,

    After looking at the WHM backup configuration, it appears that you can backup the MySQL directory only. Are you sure this script cannot be used for that purpose? Screenshot of WHM backup config.

    Regards,
    Bill

  29. Justin Cook said,

    on May 13th, 2007 at 4:09 pm

    This script utilizes the cPanel FTP functionality, which backs up the entire user directory, files, DB, everything. That can't be changed


  30. on May 14th, 2007 at 5:34 am

    Hi,

    Thanks for this script. I've installed it on my server and it is successfully sending the backup to my remote backup server, however, it keeps leaving an empty zip file in the root of my local server. Any idea why this happening and how I can fix this issue? Thanks again.

    Summer

  31. Justin Cook said,

    on May 14th, 2007 at 7:03 am

    This is the default behaviour of cPanel. Please do not use spammy keyword-laden names in comments, thank you

  32. Ron said,

    on June 12th, 2007 at 3:57 pm

    I have used this script for a while and it has worked well. Thank You. Lately though I have been getting this error:

    Can’t call method “login” on an undefined value at /usr/local/cpanel/bin/ftpput line 28.

    Any Ideas?

    Thanks

    Ron

  33. Justin Cook said,

    on June 12th, 2007 at 6:05 pm

    sounds like your cpanel web host changed something!

  34. Ron said,

    on June 12th, 2007 at 7:44 pm

    Thanks - I guess there is not much I can do. It is on a reseller box.

  35. Milliways said,

    on June 23rd, 2007 at 8:15 pm

    Hi Justin,

    This looks a nice simple script, much better than the over-complicated offerings that others put out, which all seem to rely on downloading or accepting the zipped file in email, not practical when I have upwards of 50 MB zipped to backup!

    If it would work it would be ideal for my domain to run a weekly site back up. The problem is I just ran it via crontab and got back the error that the /frontend/…/dofullbackup.html file does not exist. Were you using the default configuration of a typical cPanel, or is it possible this is set up differently as some preference of the host? Thanks for any help you can offer.

  36. Justin Cook said,

    on June 24th, 2007 at 8:33 pm

    You should try running the backup through cPanel manually and see what happens. I would recommend that you check with your host though, as this seems like a pretty unique error

  37. marc said,

    on July 10th, 2007 at 3:16 pm

    Hi,

    nice script, but may I ask for what you use it? I just bought an Server, looked in my CPanel and there under backup are a lot of radio boxes where I can choose to have it backed daily, weekly etc etc…? Or do I think in the wrong direction?

  38. Justin Cook said,

    on July 10th, 2007 at 3:20 pm

    You don't technically have to login to cPanel, the script does all that for you

  39. marc said,

    on July 10th, 2007 at 3:59 pm

    Hmmm sorry I dont get it lol ;o)
    In WHM are options so I can specify intervals for the backup to run, so WHM runs an backup every day at 2.15 p.m as example…without me logging in manually…

    What is the part I am missing now? ^^

  40. Justin Cook said,

    on July 10th, 2007 at 4:05 pm

    If you can do it through WHM, you should be fine. However, if you need to do it through cPanel, and to a remote FTP server, then you would use the above script

  41. marc said,

    on July 10th, 2007 at 4:20 pm

    Yes true, that was the part were I was thinking in a wrong direction…Thanks


  42. on July 11th, 2007 at 11:14 am

    It would like to know if this script functions to make backup of cPanel of the customer for another external server, remembering that the customer who has cPanel does not have access to the SSH of the server of it therefore it is a resale.

  43. Justin Cook said,

    on July 11th, 2007 at 11:17 am

    Yes, it does that. You don't need to have SSH access.


  44. on July 11th, 2007 at 11:22 am

    Independent of the skin of cpanel, it makes backup full of the account?

  45. Justin Cook said,

    on July 11th, 2007 at 11:24 am

    That's right. You just enter the name of the cPanel skin you're using.


  46. on July 11th, 2007 at 11:25 am

    But to program cron it would not be necessary to have access to the Server where is the resale? Or in script it has as to make a programming to generate the schedules of backups?

  47. Justin Cook said,

    on July 11th, 2007 at 11:26 am

    you only need to have the username/password to access cPanel, even on the resale account. The script and cron job can reside on another server, or on the resale server, it doesn't matter


  48. on July 11th, 2007 at 11:28 am

    Beyond this script conheçe some another one or some software.exe that it makes this same function, therefore is approximately 600 accounts to make backups full to each 2 days


  49. on July 11th, 2007 at 11:30 am

    Then it does not need to intalar nothing of the customer against the account (resale) I only install this script in my server where it will have backups and ready….

  50. Justin Cook said,

    on July 11th, 2007 at 11:31 am

    that's right!


  51. on July 11th, 2007 at 11:42 am

    plus a doubt if for example I only have 600 domínios to make backup, as I make to make backup of 600 domínios the same at the same time using script?

  52. Justin Cook said,

    on July 11th, 2007 at 11:43 am

    Well, that all depends on the server you're backing up to, if it can handle that many connections


  53. on July 11th, 2007 at 11:46 am

    You it would know to say me as to use script for all the same the usários at the same time?

  54. Justin Cook said,

    on July 11th, 2007 at 11:49 am

    I don't know. If I were you, I wouldn't do all at the same time, I would stagger them


  55. on July 11th, 2007 at 11:57 am

    don't know. If I were you, I wouldn't do all at the same time, I would stagger them Certain, it is that you with this problem to solve and necessary to find a solution for these backups external now….Debtor for the aid and will have some idea would be been thankful


  56. on September 2nd, 2007 at 5:54 am

    Hi there,

    Thanks for the awesome script. It's exactly what i was looking around for and saved me hours of work :D

    Cheers, ToNy!

  57. Scott said,

    on September 26th, 2007 at 5:45 pm

    This is a good script and it inspired me to create CPSafe, which allows for multiple scheduled site and database backups to a remote FTP server. Thanks Justin!

  58. Rob said,

    on November 1st, 2007 at 9:00 pm

    Thank you!!!

  59. John said,

    on November 26th, 2007 at 9:18 am

    I created the following awhile back to perform hourly mysql backups, archive em, and ftp em.

    This will do just that and send the backups to the FTP server. If the FTP server is down it will just store it locally until the server is back up. When the server is backup it will send all the ones locally over to the FTP server the next time it is run.

    *Designed for PHP5…. :P

    &1 >"'.$localdir.'/'.$backup_name.'.sql"');
    exec('nice -n 19 bzip2 "'.$localdir.'/'.$backup_name.'.sql"');
    exec('chown -R '.$owner.':'.$owner.' "'.$localdir.'"');

    // FTP Connect, home server
    $ftp_id = ftp_connect($ftphost, 21, 20);
    $ftp_login = @ftp_login($ftp_id, $ftpuser, $ftppass);

    // Create the backup folder
    @ftp_mkdir($ftp_id, $ftpdir);

    if ($ftp_id) {
    // Read the backup directory and FTP the contents to home server
    if ($handle = opendir($localdir)) {
    while (false !== ($file = readdir($handle))) {
    if ($file != '.' && $file != '..') {
    if (ftp_put($ftp_id, ftpdir.'/'.$file, $localdir.'/'.$file, FTP_BINARY)) {
    unlink($localdir.'/'.$file);
    }
    }
    }
    @closedir($handle);
    }

    @rmdir($localdir);
    }

    // Close the FTP connection
    @ftp_close($ftp_id);
    ?>

  60. John said,

    on November 26th, 2007 at 9:24 am

    I created the following awhile back to perform hourly mysql backups, archive em, and ftp em.

    This will do just that and send the backups to the FTP server. If the FTP server is down it will just store it locally until the server is back up. When the server is backup it will send all the ones locally over to the FTP server the next time it is run.

    *Designed for PHP5…. :P

    http://www.xtraticwar.com/snippets/backup.php

  61. Bryan H said,

    on January 15th, 2008 at 9:43 pm

    Is there a maximum file size that can be used with this script? I cannot make a full cpanel backup manually through the cpanel interface because there is a 5GB limit. I have 20GB of files, mostly images. Can this script, through cpanel still perform a valid backup using this direct method? Thanks for any feedback and ideas if it does not work for me.

  62. Bryan H said,

    on January 17th, 2008 at 2:18 pm

    I am getting the following error, similar to Bob a few months back. I cannot figure out why. I can login to the web server manually from my desktop using the same parameters. I get a report to my email saying all backed up, but I assume the FTP is what is failing.

    Can't call method "login" on an undefined value at /usr/local/cpanel/bin/ftpput line 28.

    Thoughts on what to try?


  63. on February 10th, 2008 at 12:23 am

    Check out the following link. It's a PHP script that will download all your CPanel backups and databases off-site, automatically.

    http://www.cpdatabackup.com

  64. shoaibi said,

    on March 4th, 2008 at 7:23 am

    i am running it on my server, everything was setup as you told, the backup file is created during the backup process in ~/ but then automatically deleted at the end of completeion of the script, why so?

  65. meng said,

    on March 5th, 2008 at 9:15 pm

    i have a cpanel account where the host disabled the backup button in the cpanel.

    would i be able to use this script to do a backup?

Leave a reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image