Cron Job Shell Script To Auto Backup Web Site Files

Introduction

Here I am going to show you how to setup auto backup web site files using shell script cron job.

In the world of web, it is recommended that you take care of your web site files by taking backups of them frequently. If anything goes wrong so that you can take an appropriate action on your web site files. At least you can restore to your latest stable backup files and make your site live again.

While most web hosting companies do a daily backup of a customer’s database and site files, relying on them to make backups and provide them at no cost is risky also.

Here I am going to show you how to create a simple shell script to take backup of your database as well as web site files. This example will show you how to take backup of your WordPress database and site files, but the same shell script will work for any kind of web sites with a small tweak (of course you need to have some basic knowledge in shell scripting in Unix environment or you can hire a developer for this).

auto backup of web site using shell script cron job

I will also setup a cron job in crontab so that you don’t need to run the shell script manually for taking up backups. You may forget to run the script and you will miss the backup for that day if you do not run the backup script on a particular day. Even if you setup the cron job, you can also run this shell script to take backup as per your convenient time.

I am assuming that you are not using your root user for daily activities in your server environment, and you have created a user with super user privileges to perform daily activities.

Prerequisites

Unix System, Shell Programming

Backup Shell Script

Let’s create a shell script in Unix programming for taking the backup of the database and web site files.

MySQL Database Backup

Let’s start with simple Unix shell programming to create a backup script.

Create a directory under your user profile (/home/<username>) by executing the command:

$ mkdir scripts

Let’s create a shell script site-backup.sh under the scripts folder:

$ vi scripts/site-backup.sh

Now press i to change as INSERT mode.

First of all I am going to extract the database name, database username and database password from the wp-config.php file of WordPress CMS (Content Management System:

WPDBNAME=`cat /var/www/html/site/wp-config.php | grep DB_NAME | cut -d \' -f 4`
WPDBUSER=`cat /var/www/html/site/wp-config.php | grep DB_USER | cut -d \' -f 4`
WPDBPASS=`cat /var/www/html/site/wp-config.php | grep DB_PASSWORD | cut -d \' -f 4`

The above lines are extracting the required values which will be required to backup your database file. Adjust the path (/var/www/html/site/) according to your server location where your wp-config.php file is found.

Instead of hardcoding the database values I am directly extracting the required credentials for connecting to database from the wp-config.php file and it has the following benefits:

  • If you change the database credentials in wp-config.php file, you don’t need to change in the shell script
  • If you are explaining this shell script to someone else, then you are not accidentally showing your database credentials to him/her
  • You can safely keep this shell script in your local system without having a fear of exposing your database credentials

Next is the backup path where you want to store your backup file. Make sure you have created the directory backup under your user profile:

backup_path="/home/<username>/backup"

Then I am building a MySQL backup file name. I am also appending current date so that everyday your backup does not get overwritten by the new one.

mysql_backup_file=$backup_path/site-"date "+%d-%m-%Y"".sql

Finally, the following command takes the backup and stores into the backup directory.

mysqldump -u$WPDBUSER -p$WPDBPASS $WPDBNAME > $mysql_backup_file

Notice in the above command there is should not be any space between -u and $WPDBUSER. The same applies for -p and $WPDBPASS.

Later you can download the whole source code from the Source Code section.

WordPress Site Backup

First, I declare the site files path from where all site files will be added to a zip file. Make sure to adjust the file path according to your system.

site_file_path="/var/www/html/site"

Next, I build the zip file name for web site backup. Here also I have appended the date in dd-mm-yyyy format with the file name so that everyday your old backup does not get overwritten by the new one.

site_backup_file=$backup_path/site-"date "+%d-%m-%Y"".zip

Finally, I execute the following command to create a zip file of the web site files. Make sure you have installed zip package (CentOS command to install – yum install zip).

zip -r $site_backup_file $site_file_path

-r option will recursively add files and folders in the zip file. The first argument $site_backup_file is the file created out of the path $site_file_path.

Now escape from the INSERT mode from the editor by pressing escape key and type :wq to save the file.

The whole shell script with the content is given below:

#!/bin/bash

echo "Starting MySQL backup. Please wait ..."
echo -e #new line

WPDBNAME=`cat /var/www/html/site/wp-config.php | grep DB_NAME | cut -d \' -f 4`
WPDBUSER=`cat /var/www/html/site/wp-config.php | grep DB_USER | cut -d \' -f 4`
WPDBPASS=`cat /var/www/html/site/wp-config.php | grep DB_PASSWORD | cut -d \' -f 4`

backup_path="/home/<username>/backup"

mysql_backup_file=$backup_path/site-"`date "+%d-%m-%Y"`".sql

mysqldump -u$WPDBUSER -p$WPDBPASS $WPDBNAME > $mysql_backup_file

echo -e
echo "MySQL backup done - $mysql_backup_file"

echo -e #new line

echo "Starting web site backup. Please wait ..."
echo -e #new line

site_file_path="/var/www/html/site"
site_backup_file=$backup_path/site-"`date "+%d-%m-%Y"`".zip

zip -r $site_backup_file $site_file_path

echo -e
echo "Web site backup done - $site_backup_file"
echo -e
echo "Thank you!"

Setup Cron Job

Now I will setup a cron job to automate the backup process.

Open the crontab using the command:

$ crontab -e

And put the following line in it:

05 12 * * * /home/<username>/scripts/site-backup.sh

Now escape from the INSERT mode from the editor by pressing escape key and type :wq to save the file.

The above line means that the site-backup.sh file will be executed at 12:05 am everyday. You can adjust the time according to your needs.

Note that you don’t need sudo while you are opening crontab because you are running this shell script using your username not as a root user.

Even you can run the shell script to take backup.

Shell Script Permission

Now you can change the execution permission of your shell script because by default the shell script doe not have the execute permission.

$ chmod 760 scripts/site-backup.sh

Testing Backup Script

Execute the following command to run the shell script and your backup will be done:

$ ./scripts/site-backup.sh

You will find the following files under backup folder:

$ ls -l backup/

The output what you will see as given below:

total 638020
-rw-rw-r--. 1 33480506 Oct 6 12:05 site-06-10-2021.sql
-rw-rw-r--. 1 619848391 Oct 6 12:05 site-06-10-2021.zip

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *