System Tools

Website backup using Cron Jobs

It's very important to make sure that your website can be restarted quickly in case of trouble !
Generally, as long as you are not confronted to a significant data loss, this topic may not interest you so much. 

What are the possible risks ?
- Issue during a website update or migration
- Hacking of a website and data deletion or modification
- Operation mistakes that corrupt data
- Parameter change that prevent to open the website or one of its module
- Server crash or configuration file damaged
- Hosting account suspension

Possible cases are numerous and you will necessarily encounter one of them some day. 

Sometimes, a simple harmless component update on your CMS can disrupt some website functions ! And uninstalling the concerned module may not solve the problem. It happens more often than may be imagined, even if you have done this upgrade on a test instance of that website before. 

Perfoming a backup of you website on a regular basis is mandatory !


It's quite easy to automate that process with all hosting solutions on the market : A simple Cron task allows you to perform scheduled backups in a reliable way.

All those who have a blind trust in the hosting company and its own backup must take care ! There is no contractual obligation for any hosting company to restore your data after any problem (even if the company is responsible of the data loss). So if you don't have subscribed a dedicated and paying backup service, you better should handle that yourself. 
YOU are fully responsible for your data and you cannot make any claim against your hosting company.

That reminds us an important rule concerning backup : Never store the backup in the same location where you host your website. If the server crash or if your account is suspended you will indeed lose the whole website and its backup in the same time.
Of course, you can perform a backup and store the file outside the public_html directory in your account ! But you must duplicate that backup onto another location, through FTP for example. So you'll have a reliable backup process with day-to-day backup file on your server (in order to restore it quickly) AND an external copy of that backup in case everything is lost on your server.
Before getting into Cron scripts here are some interesting tips :
- Always backup your website file system AND the associated database
- While generating backup files, always pay attention to place them OUTSIDE the public area of the website (mainly the public_html folder) to avoid your backup being opened or downloaded by any Internet visitor. 
- Schedule your task depending on how critical the data are. If you manage a static website, a weekly backup is enough. If your content is updated every day, performing a daily backup is recommended, at least for the database.
- Retrieve backup files locally on your computer or copy them onto another server (using FTP transfer through another Cron job).
- Enable the email reporting option for Cron tasks, so you will receive information on you email account to check if backup was performed without any errors.
- It's necessary to control the integrity of your backup file on a regular basis to ensure that your backup is reliable ! Just open the ZIP file and check if all important information are there. There's nothing worst than a performed backup, but which can not be used the day you need it. 

Define the frequency of the task

Adjust the backup process frequency depending on data update's rhythm and the website size. 
You can effortlessly use the CPanel interface which is quite simple to use. Indeed, formatting a Cron task command line with accurate parameters by hand may be complex to learn for beginners. CPanel allows you to customize the "Cron Job" using a graphical interface.

You'll find most common parameters in the main drop-down list, under "Common Settings". Then you can modify or adjust each option depending on the frequency you want (months, days, hours, minutes). 
cron optionsIf you use the first drop-down list, you will notice that it covers around 95% of the cases ! (every 5 minutes, every hour, one or two times a day, once a week, once a month, etc.) 
cron options menu

Formatting files names

That may seems trivial, but it's very important to clearly define how you will format the name of the backup file.

The objective is clearly not to protect your data with only one backup file. You always must to keep a history over one week at least. As you will have several websites and several databases, it's better to clarify things from the beginning. 
It's important to include some information into the file name :
  • The website name
  • The backup type (database or file system)
  • The date

Some examples :
Database backup of the website performed on February, 1st of 2013
File system backup of the website performed on March, 2nd of 2013

MySQL Database Backup

To use this script you need : 

- A directory (outside public_html) to place your backup files. (As the database contains information about user accounts and website configuration, you must protect it)
In our example, it will be /home/myaccount/BACKUP
- An account authorized to access to the database in read only. (A good choice is to create an account dedicated to the backup job with limited access, to avoid issues due to password or configuration change)
- Here we will use the account bkp_user  with the password bkp_pass and we have to backup a database named mywebsite_db

The Cron command line is nothing more than an Linux/Unix command or a shell script. So you can directly execute a command or a file containing a script. 
It is also possible to execute several commands with one single Cron line : You just have to separate each instruction with a semicolon (;)

Here is the script :

date=`date -I`; mysqldump -u bkp_user -pbkp_pass  mywebsite_db > /home/myaccount/BACKUP/BKP_MyWebSite_DB_$date.sql; gzip /home/myaccount/BACKUP/BKP_MyWebSite_DB_$date.sql
Explanations :
- The "date" variable is generated by the "date -I" command in order to obtain a string formatted like year-month-day, that is easy to understand, even for an automated script. This information will be appended at the end of the file name.
- mysqldump command require some parameters : The username (after -u), the password (just after -p without any space) and the database name. The character ">" means a redirection of the command's result to an SQL file. 
- gzip program then compress the SQL file.

File system backup

Now we want to backup the whole directory structure of our website ! To optimize the disk space for our backup files and to save time during the backup process, we want to exclude the folder PDF which contain a huge volume of data. Those files are not critical and we can easily get a copy of them. Indeed no need to backup that particular folder.

date=`date -I`; zip -r /home/myaccount/BACKUP/BKP_MyWebSite_FILES_$ public_html/* -x public_html/\*
Explanations :
- Same formatted date that will be appended at the end of the file name. 
- The zip command with "-r" parameter in order to process all folders and sub-folders (recursive mode).
- "-x" option that allow us to exclude a part of the file system. You should add a new "-x" option for each path to exclude from your backup process.

Optimize space disk management

From case to case, you may wish to keep your files more or less time. The space occupied by those backup files is in direct relation with your log rotation frequency and limited to the available free hosting space. 

First possibility : Automatic rotation using the file name

The concept is quite simple and very efficient : You just have to modify the format of the date appended at the end of the file name. Instead of year-month-day, you only keep the weekday name for example.
Thus, your file name will be
As the backup file is generated once a day, it will set up an automatic rotation of your files ! the next Monday, the new file will replace the older one ! So you will keep 7 days of history effortlessly.

Second possibility : Delete files with a script triggered on the date

Select your language :