[SOLVED] HOW TO: Daily Remote Backup on a cPanel Host

davet

New member
I have found this info to be very helpful and it has saved us many times in the past from extended downtime for our sites.

This is how we backup our important sites daily to a remote FTP location.

Create a file in your /home/username/ directory named backup.php with the code below. Then setup a cron to run the file daily. We actually run it 4 times daily.

You don't need your own expensive FTP server either. I use an XP Pro machine with Filezilla server installed to it for our remote FTP server. The XP machine is located right next to my main workstation so I can burn copies to DVD if need be. I use port forwarding within my Verizon FIOS router to point the FTP port to the XP machine.

I can sleep at night now knowing I have a local copy of our most important data.

Don't rely on your hosting provider for backups. If you read most hosting providers' policies they're not responsible for them anyways.

Do the smart thing and start keeping backups of your site to a remote location, on your local computer or one you have physical access to.

Code:
<?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 = "cpanel-username"; // Username used to login to CPanel
$cppass = "cpanel-password"; // Password used to login to CPanel
$domain = "yourdomain.com"; // Domain name where CPanel is run
$skin = "x3"; // 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 = "remote-ftp-username"; // Username for FTP account
$ftppass = "remote-ftp-password"; // Password for FTP account
$ftphost = "remote-ftp-hostname"; // Full hostname or IP address for FTP host
$ftpmode = "ftp"; // FTP mode ("ftp" for active, "passiveftp" for passive)

// Notification information
$notifyemail = "user@domain.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&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);

?>
 
Some things of note;

1) This will only work for users operating cPanel
2) This will only work if your web host has not disabled the backup system (seems a lot of free or cheap hosts are disabling backups because of resources)
3) This can be resource intensive application if you have a large site. It must pack all files in the site, so if you have a 500MB site, it needs to make a zip file that large which can take valuable resources.

Verify with your host that you are allowed to run this file, and then also verify that you can run it in a cron. Since this does put a load on the server, if you run it during peak operations on a website that doesn't have a lot of resources available you can quickly find your website suspended by the host.

It's a nice script - just be aware of the "what if" scenario.
 
1) Correct which is why in the title it says "cpanel host"

2) Correct, some cpanel hosts disable the backup feature.

3) Yes anything over 4gig in size can cause a significant load and crash services.

Sites under 4gig shouldn't cause any significant load issues but handsonhosting you are right about checking with the host if you have a large site and want to run this script.
 
Yep - Just wanted to be sure others saw it ;)

And as for the 4GB limit - I've seen many hosts struggle with a 1GB backup, and I've crippled many a machine with just a 500MB backup (when we move sites from one host to our servers).

For most people, a 1GB transfer would take approx 30-40 minutes from one machine to another, so a 4GB would be a LONG time in transit, not to mention the potential of a corrupted backup.

Are you by any chance running an integrity script on the destination server to validate that the backup can be unpacked with no errors? Might be something to experiment with also, this way you're sure the backups work.

One other thing, it looks like it's making just a regular backup with the username. Is it time-stamping within the zip name? Also might be a good thing, this way you can have several backups on your remote machine without overriding the previously transferred file.
 
We backup our main site http://interactiveonline.com which is 3.2gig in size 4 times daily on a server that has 400 sites on it. It's a quad core processor with 8gig ram and doesn't have any problems with the backup but we do run the script as nice -n19 which runs it at the lowest priority.

We don't run an integrated script on the remote FTP server but I periodically extract the backups to see if they are valid. If you're aware of any such script please keep me posted.

Also yes it does create a time stamp within the file names. Here's an example:

backup-12.1.2009_18-00-02_ioc.tar.gz

Also if you think 4gig takes a long time you should see how long it takes to do our remote backups for the entire server... about 2.5 days.

Not sure how you're handling your remote backups of the entire server but I finally discovered a way to handle it without overloading it (even large sites). See SOLVED: Local & Remote cPanel Backups
 
We actually run r1soft's CDP software on all our machines (and dedicated servers). It does incremental backups every hour including MySQL Databases, and it's integrated with cPanel so a user can restore a single file, a directory or an entire site if they wanted, all from within their browser. Only Databases need to be requested restore through helpdesk, but even then the user can restore an individual table or the entire database. And with backups stored for 4 weeks, there's limited chance of "losing" that important file.

CDP also offers bare metal restores - so if the server crashes, we can sync the entire machine to a new machine and we're back in business quickly. It also offers the verification of the files after transfer etc.

I completed a 145GB site move the other day which took about 6 hours for the move using rsync, but then we had an awesome connection between the two servers. So many hosts limit their transfer speeds, but the place they were moving from didn't have a low cap so we were able to get business done!

Nice backup file with the timestamp. that makes things easy.

With regards to the unpack and verify, you could setup a cron job on the remote machine to run a verify. I could probably whip something together if you're on a Linux system but I think you mentioned Windows earlier. Not aware of any programs off the top of my head that would do something like that automated in Windows - manually, sure - using winzip - but nothing automated that I know off the top.

Two days on a backup is a pain, but at least you're doing backups! So many people don't even do that!
 
Interesting post. Never experienced the issues that they experienced. We have it running on over 300 servers for the past 3 years or more and haven't had any issues really. ONE issue I remember, resulted in a partial data loss, but that was 2 years ago I think. We have several users restoring sites daily, and even here at HostingDiscussion the backup system has saved this forum a few times. Keep in mind that the blog you saw was from 2008 also - many things could have changed in todays environment, but definitely we didn't experience any major issues with the software after testing and implementing.
 
Theres a built in feature in cpanel to do that.

To do a full backup yes, but not to schedule it to run daily, weekly or monthly.

The script does use the cpanel backup feature, but allows it to be scheduled.
 
Hi,

Consider using Rsync via SSH, as with rsync, it will only backup what have changed since last backup, thus light on load and resources.

Regards,
Bobby
 
Hi
cpSync software is free, however it is a closed-source script. The catch is that you need to buy the backup space from cpsync.net and the packages are already cheap
 
Top