cPanel Automated Backup Script

Posted by Nessa | Posted in , | Posted on January 3, 2007

137

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

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

$ftpport = The port for FTP access on the server

$ftpdir = The remote directory, starting from the root of the FTP directory. If FTP server is the local server, this must NOT be the home folder of the user, as cPanel will automatically delete the backup after it is completed.  Example: /backups (/home/$user/backups).  Directory must exist.

$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
  • Option to just back up MySQL databases or home directory
Be Sociable, Share!

Comments (137)

[...] wrote a great article for automated backups via a CRON. Take a look at cPanel Automated Backup Script :: v-nessa.net :: Programming is Sexy __________________ Axigy – Shared and Dedicated Web Hosting Solutions ■ Top Notch Dedicated [...]

[...] If you have cPanel here is a link to set up a cron job to run a backup of your site: Automated Cron Job [...]

Hey! How bout that automated MySql DB backup feature?!

:D

Thanks for this precious and smart coding gem!

it’s still work for cpanel and for mysql database?
sorry for my bad english..

[...] from V-Nessa’s site, I thought I would share the PHP script that I have now modified to include Secure Copy (SCP) [...]

The script cannot be downloaded.

Cheers

download link has been fixed

Using http://www.jonasjohn.de/lab/htmlsql.htm will help a lot to improve it.

I’ve been coding a some sort of centralized cPanel for administrative tasks and htmlsql makes the job as easy as if you’re working with an API ;)

I’m curious, with the Cron command, the username I’m assuming refers to the FTP username?

php -q /home/username/cpanel_backup.php

The username is your account username, the one you use to log into cpanel

Thanks for the reply, I don’t think it’s working yet I’ve set it up word for word, I’ve set it to backup once a week. I’m not sure to set ftp to active or passive though, does this matter?

My inmotion-hosted website has grown to the point where inmotion won’t back it up unless I pay for an expanded plan. I’d much rather just do the backups myself – but, every time I use the compress feature of the cpanel, it craps out somewhere between 2-3 GB compressed. (My site is now ~15GB.) So I can’t see how this script will work for me.

It makes sense that I do a full backup, and then incrementals for files that change or get added (including a MySQL backup for my phpBB forum). That way, inmotion would suffer less load, and I wouldn’t have to suck off the entire website every night. But since inmotion won’t permit ssh or even rsh, I can’t rsync.

It would be sweet if there were a program that looked at the directory and ftp’d only those files that changed (could that be yafc? I don’t know enough about it). Anyway, I’m still looking. It doesn’t benefit me that my inmotion plan provides unlimited storage if that plan makes no provision for me to easily make backups – without incurring additional cost.

Dan

Me being an employee of IMH, if your site is on a server where the backup option is disabled, it means your site is probably over 10G. Trying to run a cPanel backup on a shared server when you have a 10G+ site is a problem – it taxes the CPU and we generally end up killing the backup because the servers go nuts when customers with large sites do them. Please keep in mind that as with ALL hosts that allow unlimited disk space, there is always the expectation of appropriate use. It’s not appropriate to tar up 10G+ worth of data on a shared server, so we don’t allow it.

Is it possible to have the backups saved to a folder inside the root?

That’s what I’m trying to do, I set up an FTP account at home/username/folder. I filled out the FTP fields as well as the cPanel.

The reason for this is because my backups are magically disappearing occasionally.

Thank you in advance.

This is totally off the present subject, but I can’t seem to connect to anyone at IMH that will send a reply, so here it is: I have been trying to follow the IMH Community postings, for which you are listed as a moderator, however, I cannot see where any of the posts (including mine) have been looked at or answered in the last four months or so. I see where a number of people have joined but all of them also show zero posts, zero looks, and zero answers. It seems strange that someone would go to the rouble of joining a group and then not post anything. Has the IMH Community been replaced with some other form of communication, user group, blog, etc? Thanks.

Hey V-nessa thanks for sharing this.

Any updates on the home directory only backup and/ or the mysql database backup script updates?

Blessings

Bye !

Yeah, so I can’t get this to work. I set everything up the way you describe. When the cron runs I get an email (in debug mode) that is a ton of html (maybe fullbackup.html?).

I had to change some of the stuff in the ‘fputs($socket,”POST’ line because it was saying that file wasn’t found. I think it is probably looking for fullbackup.html and not dofullbackup.html, right?

So I don’t think I am getting any errors or notifications but I am also not getting any full backups. Any ideas?

Hi do you go out with older guys, I’m only 49. Haha just kidding my TechnoQueen. Seriously, will this script backup the MySql Databases and E-mail and filters – alias? As you know this works with the Cpanel Full backup when you do it manually. At least it does on my Hostgator account.

There needs to be an update with the following modified

After the $ftpmode line add

$ftpport = “21″; // FTP Port number
$ftpdir = “/backups”; // Your backup path

And then modify the line that has

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

with

$params = “dest=$ftpmode&email=$notifyemail&server=$ftphost&user=$ftpuser&pass=$ftppass&port=$ftpport&rdir=$ftpdir&submit=Generate Backup”;

to add in the two new settings and then you are good to go.

Cheers

GW

hmm my previous comment seems to have cut off text for the params, so here is what it should be, but you will need to remove the wrapping

$params = “dest=$ftpmode&email=$notifyemail&server=$ftphost
&user=$ftpuser&pass=$ftppass&port=$ftpport&rdir=
$ftpdir&submit=Generate Backup”;

Hope that reads ok now.

GW

could i back my whole hosting c panel backup??/

http://www.theworldofinternet.com

Hello, im using the script on cpanel 11.30.0.32

The script run and do the backup, the problem is that the script seems to put another ftppass… this is my result:
———————————————————————————————————
pkgacctfile is: /home/xxxxxxx/backup-8.6.2011_05-08-02_xxxxxx.tar.gz
md5sum is: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

size is: 3059501

homesize is: 36864
——————————————————–
Net::FTP>>> Net::FTP(2.77)
Net::FTP>>> Exporter(5.58)
Net::FTP>>> Net::Cmd(2.29)
Net::FTP>>> IO::Socket::INET(1.29)
Net::FTP>>> IO::Socket(1.29)
Net::FTP>>> IO::Handle(1.25)
Net::FTP=GLOB(0x90949f0)<<< 220———- Welcome to Pure-FTPd [privsep] [TLS]
———-
Net::FTP=GLOB(0x90949f0)<<< 220-You are user number 2 of 50 allowed.
Net::FTP=GLOB(0x90949f0)<<< 220-Local time is now 05:08. Server port: 21.
Net::FTP=GLOB(0x90949f0)<<< 220-IPv6 connections are also welcome on this server.
Net::FTP=GLOB(0x90949f0)<<>> USER xxxxxxxx
Net::FTP=GLOB(0x90949f0)<<>> PASS ….
Net::FTP=GLOB(0x90949f0)<<>> TYPE I
Net::FTP=GLOB(0x90949f0)<<>> CWD /
Net::FTP=GLOB(0x90949f0)<<>> ALLO 3059501
Net::FTP=GLOB(0x90949f0)<<>> PORT 127,0,0,1,140,241
Net::FTP=GLOB(0x90949f0)<<>> QUIT
Net::FTP=GLOB(0x90949f0)<<< 221-Goodbye. You uploaded 0 and downloaded 0 kbytes.
Net::FTP=GLOB(0x90949f0)<<< 221 Logout.
———————————————————————————————————

Can somebody help me?

my previous comment seems to have cut off text:

Net::FTP=GLOB(0x90949f0)>>> USER xxxxxx
Net::FTP=GLOB(0x90949f0)<<>> PASS ….
Net::FTP=GLOB(0x90949f0)<<< 530 Login authentication failed

cut off text again

Net::FTP=GLOB(0x90949f0)>>> USER xxxxxx

Net::FTP=GLOB(0x90949f0)
<<>> PASS ….
Net::FTP=GLOB(0x90949f0)<<< 530 Login authentication failed

AFFFF ther are cut off text and i cant fix it:

USER xxxxxx
User xxxxxx OK. Password required
PASS ….
Login authentication failed

Hi Nessa, you point out this site:
http://sitebackup99.com/index.php?language=English
great for automatic backups (scheduled) of the cpanel on a secure server.
bye, I hope I have done something pleasing

Thanks so much for your input GW, that addition fixed my script after days of testing.
Cheers

Very nice, just had to update the script with GW’s comments for it to do what I needed.

Hello Nessa!
Great Script!!
Is it possible to backup to set it up only to backup a specific folder inside the home directory?
Thanks!

Has anyone tried to get this running on bluehost with any luck?

backup to amazon S3 would be a nice new feature

Hey guys,

I don’t know whether this script is actively being maintained, but I’ve seen problems with the latest version of cPanel/WHM… there are other options that are also free.

We have a script for cPanel, and a script solely for MySQL Databases.
http://www.hostliketoast.com/2011/09/how-to-schedule-full-automatic-cpanel-backups-mysql-databases-files-cron/

Hope it helps.

If you need Amazon S3, you can try http://FTP2cloud.com as one option…

Cheers!
Paul.

All -

Sorry for the delay on this. The script has been updated to work with cPanel 11.34 and 11.36, and requires a couple modifications to your FTP settings. Please see setup info in the post, specifically related to $ftpport and $ftpdir!

Post a comment