This is a simple script derived from the cPanel process that generates full account backups, only in PHP form. This makes the task of backing up your site easier and more reliable by allowing you to generate and maintain your own full site backups.
Download v1.6
- Automatic backup via Cron job
- Generates an actual cPanel backup in tar.gz format
- Very easy to set up, and executable with only one simple command
- Email notification when the backup is complete
- FTP option to transfer the backup file to a specified FTP server
- Compatible with monsoon, x, x2, and x3 themes
Requirements:
- cPanel 10 or higher with Cron job and backup functionality enabled
- PHP 4.1.x or higher
- FTP access (optional)
How to Use:
- Download the script to your computer and extract the tarball
- Edit the cpanel_backup.php file with your cPanel settings:
$cpuser = Your cPanel username
$cppass = Your cPanel password
$domain = The domain name where cPanel is run, usually without the www’s
$skin = cPanel skin that you are currently running. This will usually be “x3″ if you’re on cPanel 11 and “x” if you’re still on 10 (which I hope you’re not) but your webhost may have changed this. You can double-check by logging into cPanel and viewing your server settings:
The next section will set up the FTP options to upload the backup tarball to a server. If the FTP location is the same as the server you are doing the backup on, you should leave these fields blank.
$ftpuser = Username for your FTP site
$ftppass = Password for your FTP site
$ftphost = FTP hostname (usually ftp.yourdomainname.com)
$ftpmode = The mode in which you would like to have the file transferred. I would recommend “passive” mode, especially for larger sites.
$notifyemail = The email address that the backup confirmation should be send to once the backup is complete
$secure = Whether or not to allow the script to access your cPanel through its secure ports
$debug = Whether or not to show the detailed backup results in your confirmation email
4. Once the php file has been configured, upload it to your webserver. For security’s sake, it is very important that you make sure this file is located outside your document root (public_html) with permissions of 600
5. Now, all you need to do is set the crontab. Log into your cPanel and go to the “Cron Jobs” section and choose “Standard.” Here is where you will create the actual cron job to run the script whenever you want:
The command may vary depending on the setup of PHP on your server, but your Cron command will be as follows:
php -q /home/username/cpanel_backup.php
Known issues:
- Users having the rvskin theme have indicated that using ‘x3′ at the theme may work, but others have said that the the script just doesn’t work at all. I’d say just try running a backup manually and see what theme is in your URL when you do it, and use that.
Upcoming Features:
- Support for SSH/scp
- Selection of remote directory for FTP and SSH/scp
- Option to just back up MySQL databases or home directory




























74 Comments | Add your own
You’re the shizzle V…and you know it! ;-)
This saved me a ton of time!
Okay…I linked to this post, and since your site is back up, I have removed the copied text.
Thanks Nessa…
Hi Vanessa, I contacted CPanel about the ability to create backup crons and they said that CPanel does not have this feature yet but that you made a nice script for it. I have given this to some of our customers who have found it very useful. I just have a couple questions I hope you don’t mind if I ask.
Does this script support SSL connections for customers who prefer to connect to CPanel securely? Also does it currently provide support for the new CPanel 11 theme?
The script does support SSL connections as well as the x3 theme. In the backup.php you have to set the following variables:
$secure = 1;
$skin = “x3″;
This is actually mentioned in the documentation on this very page, but you have to read it :)
Wow .. I’ve been dreading the thought of writing my own script – even more than the idea of remembering (or worse – forgetting) to log on and take a weekly site backup.
You’ve saved me heaps of time in taking backups – and even more time in the event that my whole site goes bang.
Thanks!!
V, is there any way to modify this script to backup just the DB’s and not the entire site. That would be very helpful!
Hello,
I don’t know you, but thanks for this.
It seems to be working great, except for if I am backing up my main account to the same account. The username/passwords for both cPanel and FTP should be the same, and I am entering them the same, but it doesn’t seem to be working for this account only. Any ideas?
I found out that with some servers, if you are running the backup to the same account you’re running the script for, you should leave all the ftp fields blank, including the value for $ftpmode. This was happening to some of my customers that use the script and once they removed the values in those fields it was working properly.
Also, it may also be that the backup just isn’t completing, or the backup file is too large for you to see. FTP has a 1.6G file limit so if your backup is larger than this it may be there, but you just can’t see it.
Hey I love the script it works very well. However I was wandering is there a way to make it so it overwrites the backup from the previous night? Or a better thing would be to make it automatically delete the backups that were more than 3 days old. Thank you for a wonderful script.
You can create a cron before the script runs to delete any backup-* files in the account root, or add this line near the beginning of the script:
system(’rm -f /home/username/backup-*’);
If you can’t use system() you can try exec() or passhthru(). In the past I’ve tried to get the script to log the filename it creates, but the problem with that is that the script isn’t actually creating the backup — cpanel is, so the script would not know what the filename will be after cpanel is done.
Thank you for taking the time to make this! I have a small problem with the script that I hope you can help with….
My dir structure at my host is /home/name/public_html
I placed this in the /home/name/ folder but the backups are created as pkgacctfile is: /home/backup-2.19.2008 so they are going into the home dir that I don’t have access to. Any ideas on this?
Thank you very much,
Jim
The backup should end up in /home/user…if you are running the cron job are root I’ve seen some cases where it will go into /home instead. What version of cPanel are you on?
Also, note that while the backup is running it will always be in /home until it finishes, after which it will be moved into your user home directory. Are you sure that the backup process is completing?
I am on v 11 of cPanel. I am pretty sure that it is finishing….. How can I tell for sure?
I’m not able to reproduce this so far, as each time the backup file is landing in /home/user as it should.
Did you set anything for $notifyemail? This should allow you to receive an email when the backup is done. If you set this and are not receiving the confirmation it means that your backup is not finishing for some reason, likely because of either a memory limit or the account you are backing up is too big.
Ok, I am getting the emails….. One thing I noticed, I was on the ftp and was the backup file. Then a couple minutes later it was gone…. Strange, but I am checking with some other sources from here too…..
Thank you for everything,
Jim
If you are backing up locally you should leave all the ftp info in the script blank, or the backup may be removed
Ok, that did it… But I also had to remove the passiveftp from the script in addition to all of the other ftp info.
Thank you very much. This was driving me batty!
Jim
A quick look under the hood allows you to add another param for the ftp destination directory. Very useful. I justed picked up 250GB webspace for $10 /year which I’m using just for backup. Setting a cron job means that I can push a couple of GB a week onto a remote site automagically.
I’m working on SSH/scp for the next release, and the remote directory was going to be part of that. It’s a good suggestion – I’ll work on putting that in for FTP on the next version of the script.
Harry,
Where might one get that much space for so cheap? I’d be interested in that just for backups.
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
#
. Christopher A. Wheel | April 16, 2008 at 4:39 pm | Permalink
Harry,
Where might one get that much space for so cheap?
#
I would be very careful about uploading the backup archives on such servers. The backup contains files with sensitive data. I would generate the backup and encrypt the file then upload them to a public server.
I use as backup my home PC …. once a week it stays open as I am at work and the backup is done.
Does this script replace Cpanel backup?
ie. Is the WHM Configure Backup status set to disable.
@gabi:
The script requires a cpanel login, so you have to either use the cpanel username with its respective password, or you can use the root password for all the logins if you have root access to your server
@dave
This script does not change the backup configuration within WHM
Hi Nessa
Me as a resseller I can make the c-panel backup for any of my clients manually, without having to know their c-panel user/password. It would be nice to be able to automate this process with a cron job for each client to upload periodically the backup on a ftp site
Hi,
I am a bit confused.
Do I disable cpanel backup or
1. run cpanel backup
2. run “cPanel Automated Backup Script”?
Thanks
Dave
Hi Dave
I am using this script and you do not have to disable anything on your C-Panel.
Edit the php script downloaded from here with the appropriate information. Save the php file somewhere on your web server (preferable outside your public_html folder) and run this script in a cronjob when ever you want. It is pretty clear in the picture above how to setup the cronjob.
This script is performing the C-Panel Backup that you make manually in an automated manner. It does not add new functionality to the backup technique.
Hope now is more clear for you! Have a nice weekend!
Gabi
Thanks very much Nessa for this script. Does this create a “full backup” that I am currently able to generate manually in cpanel backup? Does it include DB, email, web files, logs? And is this something that I can restore myself or would my host administrator have to do it for me? As I understand it, I can restore home directory backups myself but not full backups.
As mentioned, the script is an automated way and identical to logging into your cpanel and running a full backup. An administrator or anyone else with root access would need to restore it.
Hi Vanessa,
Could you tell me what this means?
Error in argument 1, char 3: option not found /
Usage: php [-q] [-h] [-s] [-v] [-i] [-f ]
php [args...]
-a Run interactively
-b | Bind Path for external FASTCGI Server mode
-C Do not chdir to the script’s directory
-c | Look for php.ini file in this directory
-n No php.ini file will be used
-d foo[=bar] Define INI entry foo with value ‘bar’
-e Generate extended information for debugger/profiler
-f Parse . Implies `-q’
-h This help
-i PHP information
-l Syntax check only (lint)
-m Show compiled in modules
-q Quiet-mode. Suppress HTTP Header output.
-s Display colour syntax highlighted source.
-v Version number
-w Display source with stripped comments and whitespace.
-z Load Zend extension .
-T Measure execution time of script repeated times.
it means that your command is wrong most likely…what does your cron job look like?
Hi,
This is the cron job we have set up.
/usr/bin/php -q/home1/securep4/cpanel_backup.php
Is this right?
You need to put a space between the command and the path as you would from the command line:
/usr/bin/php -q /home1/securep4/cpanel_backup.php
I did what you said and now I am getting this error.
Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home1/securep4/cpanel_backup.php on line 41
Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://http://secure-payments-gateway.com:2083 (php_network_getaddresses: getaddrinfo failed: Name or service not known) in /home1/securep4/cpanel_backup.php on line 41
Failed to open socket connection… Bailing out!
The first error is unrelated to the second — an fsockopen error indicates that it couldn’t connect to cPanel for one reason or another, likely because the hostname may be wrong.
Look at:
ssl://http://secure-payments-gateway.com:2083
That’s not a valid url. Please re-read the setup instructions I posted on this page. I have no idea why you put http:// in an FTP url….
Nice Script!
I am, however, having a little problem. Everything seems to be working; the cron job runs, the script is executed, I get an email and in that email it says that a backup file created but 0 bytes were uploaded or downloaded. Any assistance would be welcome. Thanks.
If the backup is going to be local you should leave all the ftp fields blank, including the value for $ftpmode
Thanks for the quick response. The backup is local and I did leave the ftp fields blank. I have also tried every combination of variables that I can think of. I have also read other blogs related to this subject and can only find one other person with this problem (no resolution was put forward for him). I’ll send you the report and if it makes a difference I’m hosting with In motion.
Thanks again for your help.
well geeez…why didn’t you say so =) you realize im a system admin at inmotion, right? I took a look at your script and removed the FTP variables and ran it again (php -q fullbackup.php) and I now see a backup file generating in your account. It’s only about half a mb since tar/gz compresses up to about 60%, and it looks like everything’s in there.
Thanks again for your help. Your script is going to save me tonnes of time.
I get a 404
no input file specified error? Any idea whats wrong?
means the path to the backup script is wrong
Thanks yeah I realized that sorry I wasted your time. NOw I ahve a different error here is my command
php /home/tournam4/cpanel_backup.php—
The server was not able to find the document
(./frontend/x3/backup/dofullbackup.html) you requested.
Please check the url and try again. You might also want to report this
error to your webhost.
Try to run the cron job like this
php -q /home/tournam4/cpanel_backup.php
Hi Vanessa,
Any blog with drinking games is OK by me. And yes your programming is sexy.
Speaking of which, we have an account with inmotion and I’m trying to get this backup script to run. I have the backup file in /etc and the cron job command path is php -q /etc/cpanel_backup.php. I’m getting the no file for input message, so my path must be wrong. Any ideas? Thanks in advance, and for taking the time to write this.
@don – no input file means that php couldn’t find the file specified. On a linux server when you put a forward slash as the beginning of a path, the server interprets that as ‘off the root’..so /etc/cpanel_backup.php is being looked for in the /etc folder at the root of the server. Your path, from what I currently see in your account, should be /home/yourusername/etc/cpanel_backup.php
Thanks very much for your reply.
I’m trying to run this backup script too but from the browser first just to make sure it’s working – then I’ll cron it – but for now I’m getting this error:
The server was not able to find the document (./frontend/x3/backup/dofullbackup.html) you requested
Any idea’s on how to fix this?
The fix for the skin X3 error is to set the skin to just “X” – then it will work fine!!
This errors when I try to test it from the browser – it deletes .tar.gz files older than 3 days but I can’t figure out why it does not like the wild card *.tar.gz. I have the -rm file remmed out so it won’t hurt anything. Does anyone know the correct syntax for wild cards? so *.tar.gz will work?
#!/usr/bin/php
$v)
{
$actual_difference = strtotime(date(”Y-m-d”)) – strtotime(date(”Y-m-d”, filectime($app_dir.$save_dir . $v)));
//print filectime($app_dir.$save_dir . $v).”;
if (is_file($app_dir.$save_dir . $v) && ($allowed_difference < $actual_difference))
{
//exec(”rm -f $app_dir$save_dir$v”);
print “Removed file $app_dir$v”;
}
else
{
print “Did not remove file $app_dir$v”;
}
}
}
?>
Here is the code again:
#!/usr/bin/php
$v)
{
$actual_difference = strtotime(date(”Y-m-d”)) – strtotime(date(”Y-m-d”, filectime($app_dir.$save_dir . $v)));
//print filectime($app_dir.$save_dir . $v).”;
if (is_file($app_dir.$save_dir . $v) && ($allowed_difference < $actual_difference))
{
//exec(”rm -f $app_dir$save_dir$v”);
print “Removed file $app_dir$v”;
}
else
{
print “Did not remove file $app_dir$v”;
}
}
}
?>
The code won’t post correctly – let me strip out the PHP tags:
#!/usr/bin/php
$app_dir = “/home/juicyco/*.tar.gz”;
$store_len = 3; // delete files older than 3 days from today
if ($store_len)
{
exec(”ls $app_dir”,$test_files);
$allowed_difference = 86400 * ($store_len – 1);
foreach ($test_files as $k=>$v)
{
$actual_difference = strtotime(date(”Y-m-d”)) – strtotime(date(”Y-m-d”, filectime($app_dir.$save_dir . $v)));
//print filectime($app_dir.$save_dir . $v).”;
if (is_file($app_dir.$save_dir . $v) && ($allowed_difference < $actual_difference))
{
//exec(”rm -f $app_dir$save_dir$v”);
print “Removed file $app_dir$v”;
}
else
{
print “Did not remove file $app_dir$v”;
}
}
}
@RalphF You have to set the $skin variable to the theme that you are using. This wasn’t working originally because you had it set to x3, and your host is still on x.
Is your host blocking exec()? Try replacing the command with a simple ls statement to see if you get any output:
exec(”ls -a $app_dir$save_dir$v”);
Hello,
Thanks for the script. Everything seems to work exept one thing. The backup file it doesnt moved to the backup folder i created which the script is inside, but it stays in the home /username folder for some reason.
I read the previus posts but i didnt found anything to solve it.
I use cpanel 11 x3.
Does anyone has an idea on this?
Thanks
this can’t be done yet – when i get off my ass and update it I’ll include the scp and copy options to specify locations. otherwise the backups just end up in your home directory.
I am having some problems running the backup to a remote ftp server. This script work perfectly fine locally. I use the same data but of course change the FTP host information and it give me this error:
Net::FTP=GLOB(0×8d48ca4)<<>> PASV
Net::FTP=GLOB(0×8d48ca4)<<>> STOR backup-12.17.2008_21-30-03_virtualc.tar.gz
Net::FTP=GLOB(0×8d48ca4)<<>> QUIT
Net::FTP=GLOB(0×8d48ca4)<<< 421 Connection timed out..
are you using port 22 or anything like that?
Thanks nessa. the issue was resolved. It was the configuration on the firewall and how the passive ports are supposed to be configured on the FTP server. Thanls! this is a great tool.
Hi there, great script!!! Just having one slight issue. I get the following error message in the email that sends as confirmation of the backup and then I can’t locate the backup on the server.
Can’t call method “login” on an undefined value at /usr/local/cpanel/bin/ftpput line 28.
Any ideas?
Tx
Hello.
I’m using this script to automatically perform full cPanel backups via cron job. It works quite nicely. However, space will become an issue real quick with these daily backups dumping into my root. I’m not using the ftp option to move them elsewhere at this time.
I would like to set up a cron job in cPanel to automatically delete all cPanel backups that are 3 days old or older.
Is this the correct command to do so or am I completely off base? If so, what is the correct command to work on cPanel 11 over Linux?
find /home/user/backup-* -mtime +3 -exec rm {}\;
The typical full backup reads like this /home/user/backup-2.4.2009_18-11-01_user.tar.gz I’m assuming the backup-* wildcard is correct usage as well.
I would greatly appreciate it someone could point me in the write direction. As this would be the icing on the cake.
Thanks.
My cron job reports this when the backup runs
HTTP/1.1 401 Access Denied Still Working
WWW-Authenticate: Basic realm=”cPanel”
Connection: close
Set-Cookie: logintheme=cpanel; path=/
Set-Cookie: cprelogin=no; path=/
Set-Cookie: cpsession=closed; path=/
Server: cpsrvd/11.24
Content-type: text/html
** Then the php code here **
And then the backup never gets completed. Any ideas?
I noticed one of the upcoming features is “Option to just back up MySQL databases or home directory”. I really need this because my full backup is very large in size. All I really want to backup is my public_html directory. Anything I can add or change in the script to backup this directory only?
Hi Vanessa, really good script indeed, I just tryed and it’s still running so I don’t know if this is really an issue or not but the tar.gz file does not increase in size, it’s 4ko since the beginning and doesn’t change… Is it normal?
Also, as I understand the FTP informations are for an external FTP to send the backup after right? Where will it be on the disk?
Anyway thanks a lot for the job you did (and still do?)!!
Hi Vanessa,
Thanks for the great script. It is really useful. Thanks a lot.
One question I hope you can help me with. I wonder how would I go about making it email me a full copy of the backup file. I know this is something in setting a cron job maybe but I am not sure how to do that. If this can be done any other easier way, would be amazing too. I am not really familiar with advanced cron jobs so any simple intructions would be great.
Another question. Would it be possible to make the script send the backup file to more than one ftp server? (e.g. save a copy on same server and another on a separate server). This is not very important if the email copy can be done but still interesting to be able to do it anyways.
Thank you so much again
8 Trackbacks/Pingbacks
[...] daily backups that need to be automated, like eJabs.To enable & automate this process please visit this post by my friend Vanessa. Vanessa works for my web hosting company, and provides excellent support. She [...]
[...] cPanel 11 came out I’ve been getting a ton of emails from people who are currently using my cPanel Automated Backup script asking if it’s compatible with the x3 theme. If your host is running on cPanel 10 or [...]
[...] server with this script. Matt at eJabs pointed this out a while back and I have since used this useful script by [...]
[...] http://www.v-nessa.net/2007/01/03/cpanel-automated-backup-script/ [...]
[...] Cpanel automated backup script [...]
[...] então que surge uma solução simples para automatizar este processo, com base nas tarefas agendadas [...]
[...] I chose would have to be fully automated to minimize human error. I finally ended up using this cPanel script from V-Nessa which was very simple to use and based on PHP. For each of my client accounts, I logged in their [...]
[...] I chose would have to be fully automated to minimize human error. I finally ended up using this cPanel script from V-Nessa which was very simple to use and based on PHP. For each of my client accounts, I logged in their [...]
Post a Comment