cPanel Automated Backup Script
Posted by Nessa | Posted in php,scripts | Posted on January 3, 2007
101
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
No related posts.

















You’re the shizzle V…and you know it! ;-)
This saved me a ton of time!
[...] 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 [...]
Okay…I linked to this post, and since your site is back up, I have removed the copied text.
Thanks Nessa…
[...] 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 [...]
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 :)
[...] server with this script. Matt at eJabs pointed this out a while back and I have since used this useful script by [...]
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.
[...] http://www.v-nessa.net/2007/01/03/cpanel-automated-backup-script/ [...]
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
[...] Cpanel automated backup script [...]
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.
[...] então que surge uma solução simples para automatizar este processo, com base nas tarefas agendadas [...]
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