Webmaster Deals, News, Tips And Tricks

Bookmark Us For Webmaster Deals, News, Tips And Tricks

Automate Your Website Backups

You have a lot of websites.  Backing them up manually is a pain!  You have to log into the cpanel of each website and use the backup feature, download the file and then move on to the next website and domain.  If you’re lazy, like me, then you don’t get around to doing that as often as you should.  Like me, you’re thinking that there has to be an easier way.  Good news!  There is!

I have been searching and searching all over for an easy way to backup all of my websites, automatically, on whatever schedule I specify and send that backup file to an external FTP server.  There are code snippits out there that will backup your files and databases automatically, but most will leave that file on the webserver that the website resides on.  There are so many reasons why that is a bad idea.  The server could crash.  It could be restored to a backup dated 2 weeks ago.  I’ve even heard of one web host that had to take several servers offline because a few accounts were used to SPAM.  Tough luck for all the other customers that weren’t SPAMMING.  So, it’s vital to have a backup ready to go, in case it’s needed, on a different server.  Preferrably, the server you backup to is in your own home.

That’s what the following code does.  I didn’t write it myself.  The credit is within the code.  I just took it and altered it to suit my purposes.  When I found it, the script created the backup file and would FTP it somewhere.  I added the custom destination folder path to the FTP server so that you or I could create a custom folder structure on an FTP server and keep backups for each domain in their own folders.  If you use CRON to execute this script on your server, it will automatically backup your files, databases, settings, etc. and FTP that file to the folder of your choice on any FTP server that is accessible to the internet.

 

Instructions:

1) Copy the code below to your clipboard and paste it into a new text file with a .php extension.

2) Alter the variables to suit your website and FTP server.  Don’t forget to create the proper folder structure on your FTP server in advance.

3) FTP that .php file to a folder within your website.  For security reasons, create an obscure folder name and place this file in it.

4) Use CRON in your cPanel to schedule execution of this file on a regular basis.

5) Repeat these steps for each website and domain you have.  When scheduling the CRON job, try and set a different time for each domain backup so that you don’t overload your FTP server.  These are backups, after all, and you want everything to go smoothly.

I want to thank the author of the original script.  It was difficult to find and I am no programmer, so it took a lot of research for me to alter it enough to suit my purpose.  I hope that it helps other lazy webmasters too.  :)

Click HERE To Get The PHP Code


 
// PHP script to allow periodic cPanel backups automatically.
// Based on script posted by max.hedroom in cpanel.net forums
// Additions by Zap at http://www.getrss.net/
// This script contains passwords.  KEEP ACCESS TO THIS FILE SECURE!
// ********* THE FOLLOWING ITEMS NEED TO BE CONFIGURED *********
// Info required for cPanel access
$cpuser = ""; // Username used to login to CPanel
$cppass = ""; // Password used to login to CPanel
$domain = ""; // Domain name where CPanel is run
$skin = "x"; // Set to cPanel skin you use (script won't work if it doesn't match)
 
// Info required for FTP host
$ftpuser = ""; // Username for FTP account
$ftppass = ""; // Password for FTP account
$ftphost = ""; // Full hostname or IP address for FTP host
$ftppath = "//"; // Full path on FTP server to save files to
$ftpmode = "ftp"; // FTP mode ("ftp" for active, "passiveftp" for passive)
 
// Notification information
$notifyemail = "you@yoursite.com"; // Email address to send results
 
// 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;
 
// *********** 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&rdir=$ftppath&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);
 
?>
 

No Comments

No comments yet.

Comments RSS TrackBack Identifier URI

Leave a comment

You must be logged in to post a comment.