cPanel Automated Backup Script

Posted by Nessa | Posted in uncategorized | Posted on 03-01-2007

83

cPanelThis 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:

  1. Download the script to your computer and extract the tarball
  2. 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:

cPanel 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:

cPanel Cron Job

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
Share and Enjoy:
  • Digg
  • del.icio.us
  • Technorati
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Sphinn
  • Mixx
  • blogmarks
  • Furl
  • Reddit
  • Slashdot

Comments (83)

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 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’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 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 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

Hi Vanessa!

Great script – it worked perfectly for me! I just modified the variables and set it as a cron job and it worked flawlessly.

Only problem is, like the others who have posted, is how to delete the old backups automatically as I would like to do daily backups and store x number of backups.

I really appreciate your time in making this script.

- Chris

I am getting same error as noted above

The server was not able to find the document (./frontend/x3/backup/dofullbackup.html) you requested.

I formatted the cron job as above but still get the error

/ramdisk/bin/php5 -q home1/managem4/etc/cpanel_backup.php

any ideas?

@Nathan – Check your cpanel theme and make sure its correct. Also, If you don’t have a backup icon in your cPanel, it means your host disabled this functionality and you cannot use the script.

Great Script!

I can’t get script to finish the FTP process to a remote server unless I kill the process via WHM on the host server.

Do you know why?

Thanks,
John

@John Harre – Adjust your cron to have the results reported to you via email, and let me know what it says. Sounds to me like FTP might be blocked

2RalphF
mb url must be:
./frontend/x3/backup/fullbackup.html

Hi V,
Thanks for the cool script! I’ve managed to create a whole new service based on this script so people can easily backup their site without having to tweak their own script and setup cron jobs.

I’ve set it up at http://www.cPanelAutoBackups.Com

Please let me know what you think!

Sorry the URL should be http://www.cpanelautobackup.com without the s. Tell me what you think V.

Post a comment