cPanel full backup back up

29 July 2008 - 13:07

I'm a firm believer in backing up websites. Of course, we use good quality web hosts that back up their servers. But it is not unknown for web hosts to go out of business leaving users with no way to get at their site content in order to move it to a new server. It's also useful to be able to restore files from your own backups instead of having to trouble your web host's tech support (and possibly pay for it.) So I like to keep my own backups.

A couple of years ago, we had a tool developed called Site Backup CP that makes it easy to backup websites on hosts running cPanel to your local Windows PC. That's still a good option if you are not too technically minded, use Windows, and your sites are small enough to back up over an ADSL connection. If you can use a Windows program, you can use Site Backup.

But Site Backup is a "pull" solution. It needs to run on the computer that will be receiving the backup, and that computer needs to be running all the time - even when you are away - if you want to schedule the backups. We have switched to using laptops, which are switched on only when we are using them, and which are often using a slow Wi-Fi (or expensive UMTS) connection - hardly suitable for downloading several hundred megabytes of backup. So I wanted a solution that could be scheduled on the web server, to back itself up independently.

After a lot of searching, I found a PHP script to automate cPanel backups to FTP using a cron job. This script actually crops up on a number of different sites and blogs besides the one linked to, so I'm not sure who the original author is. This script worked fine for a few months, but it stopped working this week on one of the web hosts we use, possibly as a result of a recent PHP update.

The failure was puzzling. The email return from cron showed that the backup worked, and cPanel logged in to the FTP server okay to upload it, but the authentication failed on the password. This was odd, as the script had worked before; the password was there in the script and obviously correct - it could be pasted into the cPanel full backup form where it worked perfectly.

I guessed that the problem was in the way that the original script simulates posting back to cPanel the details that would normally be entered on the web form. I modified the way this was done, and the script now seems to work again. While I was at it, I took the opportunity to separate the settings and login credentials from the main script, and to include an option to delete the previous backup before creating a new one (in order to avoid running out of FTP space by forgetting to delete old backups.)

The revised PHP script to automate cPanel full backups to FTP can be downloaded in a zip file, with installation instructions, from Tech-Pro Downloads. It's quite simple. Just create a folder for the script and copy in two PHP files. The first, cpbackup_cfg.php, shown below, must be edited to insert your site and FTP server login credentials.

<?php
######################################################################
## Automate cPanel Full Backup to FTP Script v2.0 - July 29, 2008
######################################################################
## Modified from script by author unknown by Julian Moss, Tech-Pro.net
## http://www.tech-pro.net/
## -------------------------------------------------------------------
## Released under the GNU General Public License version 2.0.
## This script comes with NO WARRANTY and NO TECHNICAL SUPPORT
######################################################################
##### Configure the following items per your site and FTP server #####
// Info required for cPanel access
$cpuser = "xxxxxxxxxx";     // Username used to login to CPanel
$cppass = "xxxxxxxxxx";     // Password used to login to CPanel
$domain = "yourdomain.com"; // Domain name where CPanel is run
$skin = "x2";               // Set to cPanel skin you use
// Info required for FTP host
$ftpuser = "xxxxxxxx";           // Username for FTP account
$ftppass = "xxxxxxxx";           // Password for FTP account
$ftphost = "ftp.backupsite.com"; // Full hostname or IP address for FTP host
$ftpmode = "passiveftp";         // FTP mode ("ftp" for active, "passiveftp" for passive)
$ftpdir = "/subdir";             // Subfolder in FTP site (optional)
// Notification information
$notifyemail = "webmaster@mydomain.com"; // Email address to send results
// Delete previous backup before running
// ** assumes only one file exists, called 'backup...'
$delbackup = 1; // Set to 1 to delete backup, 0 to leave it
// Secure or non-secure mode
$secure = 0; // 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;
?>

The second file, cp_backup.php, shown below, is the backup script itself.

<?php
######################################################################
## Automate cPanel Full Backup to FTP Script v2.0 - July 29, 2008
######################################################################
## Modified from script by author unknown by Julian Moss, Tech-Pro.net
## http://www.tech-pro.net/
## -------------------------------------------------------------------
## Released under the GNU General Public License version 2.0.
## This script comes with NO WARRANTY and NO TECHNICAL SUPPORT
######################################################################
include('cpbackup_cfg.php');
if ($secure) {
$url = "ssl://".$domain;
$port = 2083;
} else {
$url = $domain;
$port = 2082;
}
// *********** TRY TO DELETE OLD BACKUP *********
if ($delbackup) {
$conn_id = ftp_connect($ftphost);
if ($conn_id) {
$ok = ftp_login($conn_id, $ftpuser, $ftppass);
if ($ok) {
if (!empty($ftpdir)) {
$ok = ftp_chdir($conn_id,$ftpdir);
}
if ($ok) {
$dirlist = ftp_nlist($conn_id, "backup*");
if (!empty($dirlist[0])) @ftp_delete($conn_id,$dirlist[0]);
}
}
ftp_close($conn_id);
}
}
// *********** INITIATE CPANEL FULL BACKUP *********
$socket = fsockopen($url,$port);
if (!$socket) { echo "Cannot connect to $url\n"; exit; }
// Encode authentication string
$authstr = $cpuser.":".$cppass;
$pass = base64_encode($authstr);
$request_data = "dest=".urlencode($ftpmode)."&email=".urlencode($notifyemail);
$request_data .= "&server=".urlencode($ftphost)."&user=".urlencode($ftpuser);
$request_data .= "&pass=".urlencode($ftppass);
if (!empty($ftpdir)) $request_data .= "&rdir=".urlencode($ftpdir);
$request_data .= "&submit=".urlencode('Generate Backup');
// Make POST to cPanel
fputs($socket,"POST /frontend/".$skin."/backup/dofullbackup.html HTTP/1.1\r\n");
fputs($socket,"Host: $domain\r\n");
fputs($socket,"Authorization: Basic $pass\r\n");
fputs($socket,"Content-type: application/x-www-form-urlencoded\r\n");
fputs($socket,"Content-length: ".strlen($request_data)."\r\n\r\n".$request_data);
// Grab response even if we don't do anything with it.
while (!feof($socket)) {
$response = fgets($socket,4096);
if ($debug) echo $response;
}
fclose($socket);
?>
All you need to do after that is create a cron job to run the script on a regular basis.

Used tags: , , , , , , , , , , , ,

« How cool is Cuil? | Home | Firefox 3.0 breaks CS… »


no comments

Trackback link:

Please enable javascript to generate a trackback url


Leave a comment
  
Remember personal info?

Emoticons / Textile
  (Register your username / Log in)

Notify:
Hide email:

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.