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


It's useful script, Does it works well nowadays? any updated code available?
@iTechRoom – Yes it does, although I had to edit the code to add FTP port and dir as this seems to be required by more recent versions of cPanel.
Add the following after $ftpmode on line 18…
$ftpport = "21";
$ftpdir = "/";
And then alter $params on line 48ish to be…
$params = "dest=$ftpmode&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&port=$ftpport&rdir=$ftpdir&submit=Generate Backup";
Great script! I had problems with this to start with, then realised that the permissions weren't set correctly. I changed the php file to have 755. It then was allowed to execute…
Works a treat from then on!
Yes, does this script still work good? Or does it have an updated version?
Hi guys, why would the cron result be something like
/home/mine/backup-site.php: line 1: ?php: No such file or directory
/home/mine/backup-site.php: line 3: //: is a directory
/home/mine/backup-site.php: line 5: //: is a directory
/home/mine/backup-site.php: line 6: =: command not found
/home/mine/backup-site.php: line 6: //: is a directory
…
Do I need to chmod the file at all, and is that to 755 or something else? Help please.
Thanks
GW
Thanks for the cron job help!
I get the below error emailed to me and i have yet to work out why.
Net::FTP>>> Net::FTP(2.77)
Net::FTP>>> Exporter(5.58)
Net::FTP>>> Net::Cmd(2.29)
Net::FTP>>> IO::Socket::INET(1.31)
Net::FTP>>> IO::Socket(1.31)
Net::FTP>>> IO::Handle(1.28)
Net::FTP: Unexpected EOF on command channel at /usr/local/cpanel/bin/ftpput line 24 Can't call method "login" on an undefined value at /usr/local/cpanel/bin/ftpput line 28.
——————————————————–
Anyone ever solved this? running cPanel Version 11.28.64
managed to fix. One the FTP port is not set and when i set it i put $port as the veriable which is already used in another part of the script.
I changed to FTPport and then changed the $params to have port=FTPport and this seems to fix the above error and now working.
hello,
first thanks for the script is very usefull
i just want to know if is it possible the call the script from another place.
Newbie question: how do you use this? I only have a vague idea of what a script is and I DO NOT KNOW what to do with it.
Where do I put it?
How do I run it?
I tried "how to run a script" in Google but so far got a lot of stuff that assumes a lot of knowledge. Anyone care to explain or can point to a simple guide?
I'll keep looking in the meantime.
Thanks
Dr Paul
After making the changes for FTPPort, FTP Directory, and theme (x3) I got this working. thanks!
Hi Justin cook,
you are great i was searching for this script finally i get your script its fantastic before i have so many problems i can't get sometimes backup sometimes forget this is the easy way to get backup thanks so much
Thanks Justin, I modified this along with a few other snippets to create an automated way to load backups to the AMAZON S3 Account.
Clever work, thanks!
Jared
Hi, plz help me… I am using dest=homedir to store my backups to homedir … can anydby tell me how can i store it in a particular folder…
have used following but no luck
dest=home/user/backups
dest=/home/user/backups
As R3L1C said, the parameters have changed for the automated cPanel backup API. There's an updated version here together with a bit more info about FTP Servers, Passive Mode ports and setting up cron on Windows. Hope it helps.
I tried this script, and there were a few things that didn't work, probably because it's 4 years old. I updated it and tested it to work with cPanel version 11.30.1 (build 1). Here is the changed script to get it to work:
I tried this script, and there were a few things that didn't work, probably because it's 4 years old. I updated it and tested it to work with cPanel version 11.30.1 (build 1). Here is the changed script to get it to work:
// 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 = "cpaneluser"; // Username used to login to CPanel
$cppass = "cpanelpw"; // Password used to login to CPanel
$domain = "mycpanel.website.com:port"; // Domain name where CPanel is run and the port. I had to put the port here to get it to work.
$skin = "x3"; // Set to cPanel skin you use (script won't work if it doesn't match). Most people run the default x theme (I was running x3)
// Info required for FTP host
$ftpuser = "username"; // Username for FTP account
$ftppass = "password"; // Password for FTP account
$ftphost = "website.com"; // Full hostname or IP address for FTP host
$ftpmode = "ftp"; // FTP mode ("ftp" for active, "passiveftp" for passive)
$ftpport = "21"; // FTP Port (21 default)
$ftprdir = "backupdir"; // Backup Directory
// Notification information
$notifyemail = "notifyme@website.com"; // Email address to send results
// Secure or non-secure mode
$secure = 1; // 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=$ftprdir&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);
Would anyone be able to help me set up the backup on my servers?
I am willing to create an ftp account and give you access for the scripts to work.
hey guys,
I created a script that uses the CPanel native API so it's not running based on the webpages and the theme you're using. This should work with every CPanel (assuming it's fairly recent) out there.
I've also posted a video to show you how to implement it very easily. Let me know what you think:
http://www.hostliketoast.com/2011/09/how-to-schedule-full-automatic-cpanel-backups-mysql-databases-files-cron/
Well this script saved me time and money! Thanks man for posting the code!
I have it on my reseller account in Hostgator and all accounts are backed up every week using this script.
The only thing that bothers me is that a new client registered for a hosting package copied the script to his account (as I did in every other client) and when it tries to login to the client account it gets an access denied, and I don't know why… User and Pass are correct and everything works like a charm in every other account.
Here's what I get from the script:
HTTP/1.1 401 Access Denied Connection: close Set-Cookie: logintheme=cpanel; path=/; HttpOnly; port=2082 Set-Cookie: cprelogin=no; path=/; HttpOnly; port=2082 Set-Cookie: cpsession=j_s2aHJDFTDCYcww1cFyhXHd9nqyeRBhGcx1gVE9NgSnsQhxGdD1N1XpY4mFpY3D; path=/; HttpOnly; port=2082 Server: cpsrvd/11.30.3.5 Content-type: text/html
Great! Thanks for the the script
Dont seem to be working for me, Ive tried everything, Dont see no backups in any folders on main account or on my other cpanel account.
I aint getting any emails or error messeges either.
Ive tried numorus cron commands and just dont seem to be working. Help please?
I know an it's old script, but I thought I would share how to adapt this script to work with the current cpanel version.
Mandatory ftp port and remote dir during post has broken the script.
Also cpanel have introduced session tokens.
A few new lines and minor modification of the post gets this beaut of a script up an running again.
Can someone post a updated version of this script ? i really need something like this to create automatic backups.
Is there any way to make backups from all accounts of a reseller account?
I would like to automatically backup selected accounts using cPanel and then download the resulting .tar.gz file to my local hard drive. I am presuming that I should use a cron job. Please would you advise me what command I should use in the cron job setting (I can probably manage the time/frequency element and what code/script I should load and where I should load it. I am a novice so please help.