Backup your website in cPanel the manual way and automatically with a script
cPanel gives you the option to back up your website, but if you are on a shared or reseller plan it only a manual process which you initiate.
Various cPanels offered by hosting providers are different, this one is offered by HostGator on a shared hosting plan. In cPanel scroll down to the Files section and there are 2 options; Backups and Backup Wizard.
Both options offer the same thing, either a back up or a restore from a backup. Backup options are essentially the whole nine yards or various components. You can back up the entire website including root folder with mysql database and email forwarders and filters known as a full back up, or just the home directory known as a partial backup or only just the mysql database or email forwarders or filters separately also as partial backups.
When backing up you can either save the backup to a specified area on your webserver or ftp it to a remote site or simply download it compressed to your desktop.
Above is an entire website compressed ready to be saved to the desktop.
This is a great thing but relies on you remembering to do it!, a better way would be to set up a script that runs every right and backs up your website to a specified directory which you can download at anytime, this method backups your home folder contents only so a mysql database back up will also be required separately if required.
Basically we need to set up a script and run that in “Cron” which is a linux tool that allows commands/scripts to be run at set intervals.
1. First create a directory in your root level / (in a Linux environment with cPanel this will be one level higher than public_html) and name it /backups.
2. Create a file called backup.sh in /backups, either through a simple text editor like NotePad/TexEdit or TextWrangler or via Vi or Pico in the command line and paste the following:
#!/bin/bash tar czf ~/backups/backup_`date +%Y_%m_%d`.tgz ~/public_html
This tells Linux that is a command and will save a compressed zipped file and file it in your /backups folder, name it backup_Year/Month/Day and it is backing up the entire contents of the /public_html folder.
3. Change the file permissions of backup.sh to 700 so just the owner has read/write/execute permissions. Do this through either file manager in cPanel or FTP .
4. The final thing to do is to set this script/command to run automatically in Cron in your cPanel. Log into your cPanel and scroll to Advanced and click on Cron:
And in the resulting screen, add your email address in, the frequency of the backup, once a day should be fine and lastly and most importantly in the command field –
~/backups/backup.sh
Click add New Cron Job and all should be OK.
Don’t run this command more than once per 15 minutes, part of the terms of service on some ISPs (mine is HostGator) won’t allow this frequency, daily is fine. And to check just look in your /backups folder to see if the backup completed – any errors will be redirected to your email.
Doing it this way will make a cumulative back ups per day eventually filling up your allowed hard disk quota, you either can either manually mange this issue by deleting old back ups periodically or you can adjust the script so only a recent back up is kept.
This can be acheieved by making an extra line to the script:
#!/bin/bash rm -rf ~/backups/* tar czf ~/backups/backup_`date +%Y_%m_%d`.tgz ~/public_html
So the additional line “rm -rf ~/backups/*” will remove any previous backups prior to backing up the site so only one daily back up is available. – Make sure you get the tilde “~” in there! The tilde represents your home folder.
Taking this concept a step further would be to keep a daily, a weekly and a monthly back up on hand; so you would create three directories first at your root level such as at /public_html/:
/backups/daily/
/backups/weekly/
/backups/monthly/
Create three scripts:
backupdaily.sh:
#!/bin/bash rm -rf ~/backups/daily/* tar czf ~/backups/daily/backup_`date +%Y_%m_%d`.tgz ~/public_html
backupweekly.sh:
#!/bin/bash rm -rf ~/backups/weekly/* tar czf ~/backups/weekly/backup_`date +%Y_%m_%d`.tgz ~/public_html
backupmonthly.sh:
#!/bin/bash rm -rf ~/backups/monthly/* tar czf ~/backups/monthly/backup_`date +%Y_%m_%d`.tgz ~/public_html
Last thing to do is to add the three scripts as “cron” jobs following the previous steps above and set the schedules to daily, weekly and monthly to the relevant back up scripts.
To have these backups also backed up on your local computer automatically check out the follow up post on scripting your ftp to login and download the backups for you.