Keeping your cPanel websites backed up regularly
Posted Wednesday 3rd February 2010 17:59:54 by Mike Hayes
If you have ever developed a website for personal reasons and actually put it online, then you have probably come across the web hosting control panel "cPanel" - it is the market leader and solely offered by many shared hosting companies.
We use cPanel for some of our smaller websites or websites that are still in development such as Themepark Manager - it does its job, we are mostly interested in (S)FTPing in, using phpMyAdmin (if there is no shell access) and getting on with development. So for us, most parts of the control panel are not of any use - one feature we do use though is the backing up part. cPanel enables you to create a backup of your website, including databases, in one click - this can then be downloaded or stored automatically at a remote location.
Backups are very important - your web host is not usually responsible for backing up your data and even when they claim to you should probably be making your own backups for safe keeping. If you only update your website every now and again then you can probably get away with manually generating a backup then saving it to your home computer every now and again, however for ourselves and many others this is not enough. This is where the feature-full cPanel is lacking, there is no feature to schedule automatic backups. With a quick bit of PHP and some remote storage (plus cronjobs enabled, which most web hosts do these days) you can have all of your cPanel websites backed up on a schedule chosen by you to a remote location, where your data can be safe from any corruption or loss on the main host.
So, right now we have 6 websites which are either very small or still in development which we host on some affordable but stable UK shared hosting (from EZPZhosting who run a wonderful business). We don't expect to have any problems with our web host - their servers feature hard disks set up in RAID-10 arrays and are managed by more than capable Linux admins. You can never be too sure on the internet though and human error does occur sometimes. As these are smaller websites, most can be backed up on a daily schedule (rather every hour or so). We run backups to a few locations but our main storage provider is BQBackup who have servers in New York - a world away from our web hosting location in the UK.
The script and how to set it up
I would really advise that anybody running this script has some knowledge of PHP and cron jobs.. just so you know how things are working.
The script works by using PHP's cURL functions to connect to cPanel (as you would yourself) and request a backup to be generated to a certain specified location, in a directory dependant on the website. We then have this script set up to run somewhere on a cronjob daily at some time in the early hours when server loads are probably down and our website traffic is lower.
Click here to view the script
So you will need to set up your accounts in the $accounts array, the first one is shown as an example - you can then copy that single line to a new one below to backup more sites from the one script running.
On line 19 you can also set your e-mail address to be notified of each backup from cPanel, this can be handy to make sure your backups are being made when they should be.
Now you should be ready to go! Find the cronjobs section in cPanel and set this script up to run, for example "/usr/local/bin/php /home/<username>/backup_cron.php" on a schedule you would like it to. Do not put this script in your public_html directory, you do not want it to run through the HTTP server (normally).
If you theme for cPanel is NOT x3 then you will need to alter this on line 34, this is not a part of the settings at the top of the file because this script was quickly whipped up for us - feel free of course to alter it to your own needs though ![]()
We hope this helps anyone running on cPanel out there who wants to run automated backups - not the best description but for budding PHP developers it should certainly be a quick help! Feel free to contact us if you have any better solutions. Of course for our busier websites we have alternative backup solutions on our Linux dedicated servers, but for shared hosting using cPanel (where we have no root access) this works a treat.
Gaming network
4,693 registered accounts
35 players online
Latest blog post
Keeping your cPanel websites backed up regularly
Posted 03/02/10 17:59:54
Latest news post
Servetech IT acquires browser game Gangs of America
Posted 03/02/10 18:08:55