Shell script for website backup automation

As a server administrator you should take regular backups of your website and databases. I have write a shell script to take backup of our server website, it was reduce the administrator work and keep your website backup.

In this example i have backup all our website content with zip format and stored to backup location and keep 10 days backup.

1) Backup shell script

I have created the file called “” on bin directory and put the below code and the file permission should be 755 so that you can execute the file.

The below shell script take all website backup into separate files.

DATE=$(date +%d-%m-%Y)

# To create a new directory into backup directory location.
mkdir -p $BACKUP_DIR/$DATE

# take each website backup in separate name, use below format.
tar -zcvpf $BACKUP_DIR/2daygeek-$DATE.tar.gz /home/2daygeek
tar -zcvpf $BACKUP_DIR/mageshm-$DATE.tar.gz /home/mageshm
tar -zcvpf $BACKUP_DIR/dialariyalur-$DATE.tar.gz /home/dialariyalur
tar -zcvpf $BACKUP_DIR/ariyalurlive-$DATE.tar.gz /home/ariyalurlive

# Delete files older than 10 days
find $BACKUP_DIR/* -mtime +10 -exec rm {} \;

2) Cron job for scheduling backup

To schedule the job at your convenient time, use cron.I have set the cron every day 7′o clock to take backups of databases

0 19 * * * /bin/

3) Website backup is under processing

Just run the file, it will be backup one by one website contents and stored to specified location, here you can see 2daygeek and mageshm website is currently compressed.


4) Website backup stored successfully to mentioned location

The above shell script created the new directory called “03-12-2013” and stored the website contents backups. Here you can see the four websites was backuped.

# ll -h
total 44M
drwxr-xr-x 2 root root 4.0K Dec  3 07:23 ./
drwxr-xr-x 3 root root 4.0K Dec  3 07:23 ../
-rw-r--r-- 1 root root  23M Dec  3 07:23 2daygeek.tar.gz
-rw-r--r-- 1 root root 617K Dec  3 07:23 ariyalurlive.tar.gz
-rw-r--r-- 1 root root 8.2M Dec  3 07:23 dialariyalur.tar.gz
-rw-r--r-- 1 root root  12M Dec  3 07:23 mageshm.tar.gz

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

2 Comments on “Shell script for website backup automation”

  1. Excuse me, when I type “mkdir -p $BACKUP_DIR/$DATE” after that display “can’t create directory ‘/root’:permission denied”. How can I do?

Leave a Reply

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