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.)

  • Share/Bookmark

153 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?

  66. Srini said,

    on March 19th, 2008 at 3:25 am

    Justin, Thanks for the script!!!! I get the error below like Ron though,

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

    No file on target location. Any fix?

    THANKS
    Srini

  67. Tom Brett said,

    on March 19th, 2008 at 5:00 am

    http://www.drivehq.com is compatible with this script … and it's free too!

  68. Jussi said,

    on March 27th, 2008 at 8:28 am

    Looked sweet, but my PHP wasn't able to perform. Here's how to backup your cPanel account automatically with curl (provided you have curl enabled in your account).

  69. John said,

    on March 31st, 2008 at 5:58 am

    I am getting the following errors when the script runs, any help people could off would be appreciated.

    //home/powder/fullbackup.php: line 1: ?php: No such file or directory
    //home/powder/fullbackup.php: line 3: //: is a directory
    //home/powder/fullbackup.php: line 4: syntax error near unexpected token
    `('

  70. XiMeNSiOnS said,

    on April 3rd, 2008 at 11:15 pm

    Well I started developing a way for the backup on the server your backing up to delete after its been moved to the remote FTP server. Only problem I'm having is getting permissions to delete the the file. I have tried php functions such as chmod, also tried making a script to log in FTP and chmod that way but still getting failure. If anyone has a good technique tell me and I can finish this baby up and release it to you guys. I'm sure some of you would appreciate your original server not being bogged up with backups which you have already moved to another location :)

  71. chr said,

    on April 16th, 2008 at 10:17 am

    Would this work for a reseller account and backup all the accounts under it?

  72. Mieke said,

    on April 19th, 2008 at 6:05 pm

    I have been looking for something like this for weeks :o )

    Could you tell me:
    my isp allows me to up-and download 10GB/month. Would backing up to a remote server have effect on my counter?

  73. Gabi said,

    on April 22nd, 2008 at 11:57 am

    Hi there,

    If I login to CPanel with the reseller account I can switch throw my clients account and create for each of them backups on a remote server. It would be nice to be able to add a cronjob to run this script but using my reseller credential. I don't know the c-panel user/pass for all my clients!
    Any idea ?
    Thank you in advance !
    Gabi

  74. tc1967uk said,

    on May 11th, 2008 at 1:56 am

    Is it possible to set it to copy all databases, but only certain directories?

  75. NeoMorph said,

    on June 5th, 2008 at 2:34 pm

    WOW… I've also been looking for a way to backup my gaming forum site for a while now and come nowhere and then I stumbled onto this.

    Had to figure out I was using x3 skin and put .com instead of .org on my FTP account (DOH!) but now it will handle it automatically.

    Thanks for this wonderful piece of code…

    NeoMorph
    (admin – ethicalgamer.com)

  76. Tom said,

    on June 15th, 2008 at 7:44 pm

    Justin, thanks for the nice script. I was looking for something to do a full backup of one of my active sites. The script worked with no issues.

  77. alber.to said,

    on June 19th, 2008 at 4:09 pm

    hi justin.

    is it possibile to send the generated backup file to an email address?

  78. tc967uk said,

    on June 26th, 2008 at 12:44 am

    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.

    Hi John

    I can't get your script to work. I put it in a text file, renames it with "php" as the file extension, uploaded it and set a cron job to run it but nothing happens. Are there any variables in it I should have amended, and if so, can you specify with examples please?

  79. dennyhalim said,

    on June 26th, 2008 at 6:46 am

    can we use sftp?

  80. Mike said,

    on July 17th, 2008 at 8:09 am

    Hi Justin,

    Thanks for the great script, I appreciate your efforts. I was wondering if you've seen this problem yet. When I run the script everything seems to go as planned except the file that is uploaded to my ftp server is empty (0kb). There is no other file uploaded besides this. Any ideas?

    Thanks for the help.


  81. on July 22nd, 2008 at 8:32 am

    I am never done this before, but can someone please tell me how can I start the cron job.

    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

    What program should I use to do that? Or where do I insert this?


  82. on July 23rd, 2008 at 1:12 am

    i am getting this error in my email:
    /bin/sh: 15: command not found

    can you tell me what does it means?

  83. Richard said,

    on August 8th, 2008 at 1:22 am

    I have the script in place and it looks to work perfectly, if i look in cpanel it says the backup is generating and I see the ackup file being generated on the server, then when it finishes where it said the backup was in progress, it just disappears and the file that was being generated also just disappears

    Has this happened to anyone else, if not any clues as to what the problem could be?

    Thanks

  84. Frankie said,

    on August 19th, 2008 at 9:51 am

    works great! thanks a lot for posting.

  85. Tracy said,

    on August 27th, 2008 at 1:12 pm

    someone posted something about this error:
    /frontend/…/dofullbackup.html file does not exist.
    i had the same thing and the problem was the path to the fullbackup.html was incorrect because it was using a different skin.
    to check for sure what this path should be go to your cpanel, go to backup, hover your mouse over the "download generate full backup" button…look at the url that is behind that button..that is the path you should use in this php file.
    search the file to find where fullbackup.html is called and update it there.

    mine was originally set to "x" which is the default…but my skin is actually in a directory called "x3"

  86. Jesper said,

    on September 5th, 2008 at 8:03 pm

    Thank you, just what I was looking for, I installed it and at first try, it worked!

  87. Sasha said,

    on September 8th, 2008 at 8:36 am

    Hi, Justin! Is there any way to make similar to this script for automatic creation of databases and assigning users to them. Or please, direct to how create scripts for automatical use of Cpanel. Rights for creating database outside CPanel I don't have and there is a need for multiple creation of db.
    THanks.
    P.S. Sorry, for writting in this section. Din't find any other fitting my question.

  88. Brad said,

    on September 30th, 2008 at 6:46 pm

    Hi, is there anyway to get this script to do the backup but not the ftp portion. We do not have or want an internal ftp server so we want to initiate the downlaod from our internal backup server. I have tried putting homedir in the ftpmode but does not seem to work/

  89. Car Donation said,

    on October 7th, 2008 at 4:56 am

    Great post. This is just what I was on the lookout for.


  90. on October 17th, 2008 at 7:04 am

    Snx for Automatic cPanel backup info. it realy healp me !

  91. rich said,

    on October 26th, 2008 at 5:18 am

    Question,

    I've read your script and it looks like it is for a single domain on a server running cPanel. Is there and easy way to use cron for backing up all the domains and sql databases for numerous domains that reside on a server?


  92. on November 1st, 2008 at 11:10 pm

    [...] Here is an easy DIY way to back up your whole site with cPanel. [...]

  93. EoBots said,

    on November 8th, 2008 at 3:20 pm

    Wow, thank you, i just finnished installing it, i'll know if it worked in an hour =D

  94. tomas said,

    on November 9th, 2008 at 10:57 am

    Hi all,
    i had troubles with ftp upload because the script tried to upload in ftp home dir, where the remote user has not write access.
    I added a $rdir parameter with the remote dir (like the complete cpanel's script does).
    like this

    ftp params

    $rdir="home/choosendirforbackups/"

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

    hope this can be useful.
    T

  95. Vann said,

    on November 12th, 2008 at 10:02 am

    Hi Justin,

    I've tried your script but I get this mailed to me by the Cron Daemon:

    " /bin/sh: 15: command not found "

    I checked with my hosting company and we did change the file path to the file, but this may be something else.

    Any idea what this means?

    THanks again for providing this script. Once I get the kinks worked out it will be really great!

  96. Al said,

    on November 14th, 2008 at 4:38 am

    Will this work for an SCP connection also?

  97. Sahdow said,

    on November 14th, 2008 at 3:01 pm

    Absolutely the coolist app I've found! You just saved me at least 6 hours a month of loggining into remote sites and backing them up.


  98. on November 14th, 2008 at 3:07 pm

    [...] Learn more about this cool app! [...]

  99. Jerome said,

    on November 22nd, 2008 at 10:55 pm

    hihi,

    this script is not working le.

    i tried and the CRONS LOG keep sending me some funny stuff like this.

    /home/atomenet/fullbackup.php: line 1: ?php: No such file or directory
    /home/atomenet/fullbackup.php: line 3: //: is a directory
    /home/atomenet/fullbackup.php: line 4: syntax error near unexpected token `('
    and some password rant

    anyway, i remember the first 4 lines are all // comments.

    wondering why

  100. Dan said,

    on December 16th, 2008 at 8:31 pm

    Im also having the same problem as Jerome does anyone know why im getting this?


  101. on December 18th, 2008 at 6:28 pm

    [...] read this great post today that shows how to automatically backup cPanel with PHP and a cron job. The guy that wrote this recommends that you keep the file on the server you want to backup, but I [...]

  102. James said,

    on December 20th, 2008 at 4:36 pm

    Hi Justin, many thanks for your script. Great work.

    I've set it up on my server and ran the cron job. I know that the cron job found the file and ran it – but I'm not sure if anything happened.

    I didn't receive the email from the script – and I can't find the back up file though on my destination ftp account.

    I'm sure that the cpanel username and password are correct.
    I've tried putting in "www" in front of the domain – and then taken it out when it didn't work.
    I've tried ftp and passiveftp – now reset to ftp.
    I'm sure the ftp username, password and the ftp hostname are correct …

    If I didn't receive the notification email, does that imply the script didn't run? Or I have a config value wrong?

    Could you please tell me where should I look to find the backup and what I should be looking for?

    Many thanks in advance,
    James


  103. on December 26th, 2008 at 3:53 pm

    [...] Option for those using Cpanel Hosting is to use this handy script from Justin Cook and have automatic backups of you entire website hosting account. The advantage to this method is [...]


  104. on December 29th, 2008 at 10:26 am

    [...] ini aku temukan di Blognya Justin Cook. Setelah dicopy paste dan edit di bagian username dan password cPanel serta FTP tujuan, maka upload [...]


  105. on January 4th, 2009 at 7:01 pm

    [...] server get set up ASAP.  Instead, we automated nightly FTP backups of each of our sites using this nifty script for automatic cPanel backups.  In the event of disaster, I simply take the .zip file with all of our code and databases, upload [...]

  106. Centos said,

    on January 6th, 2009 at 9:57 pm

    how about reseller backup?


  107. on January 16th, 2009 at 7:32 pm

    Thanks for the script Justin – have it setup now and look forward to hopefully have the script running with automated backups on a daily basis.

    Quick question: any options for sftp at some point – Dreamhost.com offers an excellent FTP backup service. Would be great to have safer options than ftp as treansfer protocol.

    Anyway, thumbs up so far! :-)

    /Mads


  108. on January 17th, 2009 at 10:35 am

    When trying to rung the script I get this output – any ideas??:

    Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /usr/my_user_name/fullbackup.php on line 7

    Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /usr/home/my_user_name/fullbackup.php on line 8

    Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /usr/home/my_user_name/fullbackup.php on line 11

    Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /usr/home/my_user_name/fullbackup.php on line 13

    Parse error: syntax error, unexpected T_VARIABLE in /usr/home/my_user_name/fullbackup.php on line 15

  109. Daisuke said,

    on January 18th, 2009 at 8:13 pm

    I tried to test it by executing the script manually and i get a server error:

    The server encountered an internal error or misconfiguration and was unable to complete your request.

    Any help with this ?


  110. on February 3rd, 2009 at 9:29 am

    [...] Here is an easy DIY way to back up your whole site with cPanel. [...]

  111. Curt W said,

    on February 9th, 2009 at 12:19 am

    Great script! Very useful since I don't have root access to back up automatically. Is there a script to grab the sql databases specifically? My file directory has gigs of data which doesn't need to be backed up too often.

  112. d33pa said,

    on February 15th, 2009 at 9:11 pm

    Hey, Thanks for this great script. Installed with ease.
    I know this has been asked before but didnt see any responses.
    Can the file transfer be done via sftp? I tried changing "ftp" to "sftp", to no avail. Any help will be greatly appreciated!


  113. on February 15th, 2009 at 9:17 pm

    [...] I came across this blog of this genius that has created a script to automate cPanel backups. Previously, I would have to do this manually. I have shared boxes, VPS and am currently in the process of shipping a pimped  box for colo. In the mean time, as on WHM on my shared account doesnt allow automated backups, i have installed this script to create full backups and push via ftp to my remote server! This also includes all domains, emails, "sql" as well. So happy I found this script. I am currently looking to see if I can use SFTP instead of ftp. Why not make the transfer as secure as possible.. Shout out to Justin Cook [...]

  114. AlexN said,

    on February 28th, 2009 at 7:50 pm

    I got an email saying
    Could not open input file: /home/fullbackup.php


  115. on March 8th, 2009 at 1:20 am

    [...] Web Hosting Galore  08 Mar 2009 @ 6:20 AM   Resellers Backing up All Clients  I found this script very handy when I was on a reseller package and trying to manage the clients. After about 5 clients or so, making backups of their accounts is not a very fun task. So I did some searching and found this [...]


  116. on March 8th, 2009 at 1:18 pm

    [...] Learn more about this cool app! [...]


  117. on March 9th, 2009 at 9:19 pm

    [...] would do a full cpanel backup, and FTP it to the location of your choice. The script can be found here. To set this up, it requires editing the file with your cpanel and FTP credentials, as well as a [...]


  118. on March 13th, 2009 at 8:36 am

    [...] Option for those using Cpanel Hosting is to use this handy script from Justin Cook and have automatic backups of you entire website hosting account. The advantage to this method is [...]

  119. ShoukaT said,

    on March 14th, 2009 at 7:45 am

    very cool script indeed

    thanks very much man…

  120. Thor said,

    on March 16th, 2009 at 12:54 am

    Thank you Justin Cook, you have saved me hours of work each week with this simple automation script. I'll only have to trim down the storage directory once a month. you LEGEND!. I can not thank you enough… Namaste

  121. Web Hosting said,

    on March 21st, 2009 at 11:44 pm

    well, this is just what I was looking for…

    time to try it :p

  122. Dave said,

    on April 19th, 2009 at 12:12 pm

    This script has worked great but recently started refusing to delete the GZip file in my home directory when it's done. Little help?


  123. on April 24th, 2009 at 1:13 am

    [...] are scripts to do backups for you automatically through a cron. Here is one that looks [...]

  124. Sherif said,

    on April 26th, 2009 at 9:08 pm

    Hi James
    Thanks for this script. I tried it and it works with no issues with my cpanel account.
    I will write a note about it in my blog.
    Thanks again.

  125. Travis Phipps said,

    on May 5th, 2009 at 10:51 pm

    Just wanted to add my 'thank you' for this excellent script. Only thing I had to do was change the 'theme.' My host is using the x3 theme. Everything worked great out of the box!


  126. on May 15th, 2009 at 1:14 pm

    [...] Another Option for those using Cpanel Hosting is to use this handy script from Justin Cook [...]


  127. on May 15th, 2009 at 1:16 pm

    [...] Learn more about this cool app! [...]


  128. on May 18th, 2009 at 7:53 am

    [...] Here is an easy DIY way to back up your whole site with cPanel. [...]


  129. on May 23rd, 2009 at 8:16 am

    Can anyone help me? I'm getting this message when I run the script (and the XXXX's are only there to protect the innocent!:

    Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/XXXX/fullbackup.php on line 38

    Warning: fsockopen() [function.fsockopen]: unable to connect to http://www.XXXX.com:2082 in /home/XXXX/fullbackup.php on line 38
    Failed to open socket connection… Bailing out!

    Thanks for any help that anyone can give.

  130. ubuntun3rd` said,

    on May 26th, 2009 at 8:05 pm

    Thank you so much for the script! That is amazing.

    BTW, wouldn't your second cron job (15 2 * * 1 /usr/local/bin/php /home/youraccount/fullbackup.php) actually run on Monday, as Sunday is 0?

  131. lee said,

    on May 28th, 2009 at 2:32 am

    can anyone recommend where i can ftp the backup to? Is it worth sending it to another account i have with the same hosting provider (that may be on the same server/location so if that place was destroyed the backup would be too?).

  132. alex said,

    on June 8th, 2009 at 5:55 am

    Hello. I tried your script, it sends me the log through email the it has successfully created the backup, but the backup just isn't there! No archive, no nothing! :( What could be the issue? If it helps, i'm using hostgator.
    Thank you.


  133. on June 10th, 2009 at 2:04 am

    [...] Here is an easy DIY way to back up your whole site with cPanel. [...]

  134. Ted Penner said,

    on June 14th, 2009 at 5:19 pm

    Great Work sir! Can you help me set this script up via a shared session?


  135. on June 18th, 2009 at 12:48 am

    [...] malo. Decidí investigar de respaldos automáticos con cpanel y me encontré con la entrada Automatic cPanel backup (domain & MySQL) with cron & PHP de Justin Cook. Allí hay un script para ejecutar la petición de respaldos completos de cpanel con [...]


  136. on June 30th, 2009 at 3:30 am

    [...] a vari post trovati in rete (uno link è questo), siamo a indicare come eseguire un backup in automatico (full backup) tramite [...]

  137. Steven said,

    on July 3rd, 2009 at 1:08 pm

    I installed this in my public_html directory to test.
    I get a internal server error 500 and 404.
    Any place I should be looking?

    Great script, even if it is not working, YET.
    Thanks!

  138. Steven said,

    on July 3rd, 2009 at 6:52 pm

    I've made a little progress.
    I know get this message.

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

  139. Roy P said,

    on July 13th, 2009 at 11:27 am

    Another Option for those using Cpanel Hosting is to use this handy script from Justin Cook

  140. Alan said,

    on July 14th, 2009 at 3:47 pm

    Problems with "Domain name where CPanel is run"
    $domain = "example.com"; //
    My hosted site(s) use only subdomains and the primary domain for cpanel does not resolve to site I want to backup.
    Is there any way to use the IP number or some other php method to identify the Cpanel installation. Using the IP or IP/cpanel does not work.

    The later results in the following error in the cron.

    Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/apcsit/apcsitbkupscript.php on line 39

    Warning: fsockopen(): unable to connect to 174.XXX.XXX.60/cpanel:2082 (php_network_getaddresses: getaddrinfo failed: Name or service not known) in /home/apcsit/apcsitbkupscript.php on line 39 Failed to open socket connection Bailing out!

    Thank you for any guidance or help that you all can provide. Get script – just what I need if I can get it going.


  141. on August 10th, 2009 at 2:10 am

    [...] Página web del script: Justin-Cook [...]

  142. luca said,

    on August 21st, 2009 at 11:00 am

    Parse error: syntax error, unexpected T_DNUMBER in /home/XXX/bk.php on line 12

    ideas?

  143. Psinetic said,

    on October 8th, 2009 at 1:58 am

    Justin just one quick question. I have five websites total on this host with four addon-slots. I have several mysql databases that need to be backed up as well as all of the files in the home directory. this script, it DOES back ALL of this up…right?

  144. Psinetic said,

    on October 8th, 2009 at 2:32 am

    So I ran this at a specific time to test it, and this was my output in the error log:

    [code]
    [08-Oct-2009 02:20:01] PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/php52/lib/php/extensions/ldap.so' - libldap-2.2.so.7: cannot open shared object file: No such file or directory in Unknown on line 0
    [08-Oct-2009 02:20:01] PHP Warning: Zend Optimizer for PHP 5.2.x cannot be found (expected at '/usr/local/Zend/lib/Optimizer-3.3.0/php-5.2.x/ZendOptimizer.so') - try reinstalling the Zend Optimizer in Unknown on line 0
    [08-Oct-2009 02:21:01] PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/php52/lib/php/extensions/ldap.so' - libldap-2.2.so.7: cannot open shared object file: No such file or directory in Unknown on line 0
    [08-Oct-2009 02:21:01] PHP Warning: Zend Optimizer for PHP 5.2.x cannot be found (expected at '/usr/local/Zend/lib/Optimizer-3.3.0/php-5.2.x/ZendOptimizer.so') - try reinstalling the Zend Optimizer in Unknown on line 0
    [08-Oct-2009 02:22:01] PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/php52/lib/php/extensions/ldap.so' - libldap-2.2.so.7: cannot open shared object file: No such file or directory in Unknown on line 0
    [08-Oct-2009 02:22:01] PHP Warning: Zend Optimizer for PHP 5.2.x cannot be found (expected at '/usr/local/Zend/lib/Optimizer-3.3.0/php-5.2.x/ZendOptimizer.so') - try reinstalling the Zend Optimizer in Unknown on line 0
    [08-Oct-2009 02:30:02] PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/php52/lib/php/extensions/ldap.so' - libldap-2.2.so.7: cannot open shared object file: No such file or directory in Unknown on line 0
    [08-Oct-2009 02:30:02] PHP Warning: Zend Optimizer for PHP 5.2.x cannot be found (expected at '/usr/local/Zend/lib/Optimizer-3.3.0/php-5.2.x/ZendOptimizer.so') - try reinstalling the Zend Optimizer in Unknown on line 0
    [/code]
  145. Ben Martin said,

    on October 17th, 2009 at 7:02 pm

    Hi,

    After a cPanel update, this backup script (which I used for a number of years) was no longer working.

    Since that cPanel update (we are now using cPanel v11.25.0), the cPanel Full Backup requires all parameters to be provided, so cPanel was giving gives that error when running the script:

    [QUOTE]
    Sorry, all fields are required when using FTP or scp.
    [/QUOTE]

    (Need to enable debug mode to see that output.)
    Previously, the cPanel Full Backup didn't required the port or the remote directory, so the script did not bother providing any.

    A very simple tweak to the script will provide the port and remote directory fields.
    I am posting it here in case it might help others using this script whom were facing the same issue.

    I added these 2 lines of code:

    [code]
    $ftpport = "21"; // Port (default = 21)
    $rdir = "/"; // Remote dir (defaut = / )
    [/code]

    and changed the parameters line to include that:
    [code]
    $params = "dest=$ftpmode&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&port=$ftpport&rdir=$rdir&submit=Generate Backup";
    [/code]

    For conveniance, the full (updated) script is now:

    [code]

    [/code]

    I hope this can help.

    -Ben

  146. Ben Martin said,

    on October 17th, 2009 at 7:05 pm

    Ah….!

    The blog stripped the php code from my post.
    I'll try again to put it there, otherwise you can simply make the modification yourselves! ;)

    Here is the updated code:

    [code]
    // 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)
    $ftpport = "21"; // Port (default = 21)
    $rdir = "/"; // Remote dir (defaut = / )

    // 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&port=$ftpport&rdir=$rdir&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);

    [/code]

    PS: You need to add the php tags <?php and ?> at the beginning and the end, respectively, of the code, obviously.

  147. ferdball said,

    on January 20th, 2010 at 6:01 pm

    Justin – I'm am going nuts! I LOVE that snippet of code that totall helped me out with a cpanel backup to a directory, I was bugging out creating a bunch of FTP accounts on my nas just to backup a bunch of cpanels. now I can have 1 login and just create dirs
    ———————————-
    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";
    ——————————-

    NOW, I've been using this database backup script for a long time too and would LOVE to have the same option, any way you could tweak a little code (im code stupid)

    here's the one I've been using forever but would love it to allow for a specified dir:
    ——————-
    #! /usr/bin/php -q

    ————————

  148. ferdball said,

    on January 20th, 2010 at 6:02 pm

    #! /usr/bin/php -q

  149. Jared said,

    on January 27th, 2010 at 1:49 am

    Ben – thanks for the updated code, it works perfectly with cPanel 11.25.0.

  150. Legion said,

    on February 1st, 2010 at 9:21 am

    How to do this script work if my hoster closed fsockopen function?

  151. Viktor said,

    on February 9th, 2010 at 1:22 pm

    I could not found out the right location, for the fullbackup.php file.
    "/usr/local/bin/php /home/youraccount/fullbackup.php"
    My Cpanel username is viktor. After the ftp login I see this files:
    .cpanel
    .fantasticodata
    .htpasswds
    .trash
    etc
    mail
    public_ftp
    public_html
    tmp
    www

    Thank you.

  152. Rahul Agrawal said,

    on February 18th, 2010 at 12:10 am

    Hey , i needed some help. I have a WHM With 1000 domains under it. I want to write a script which can upload the same file to all the domains in one go.

    How do i write the script to access file manager of the CPANEL and upload file ?

    Please help :)

    Thanks

  153. MAI said,

    on February 24th, 2010 at 10:35 am

    Justin, I realize this is been around a while, but you have inspired me to publish a webstore process my team has created. Where did you find this template or did you create it.

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word