Simple Backup Script With Compression And Date Checking

By | September 17, 2009

To secure the data we usually use backup process. In order make the backup process automatic in Linux we usually use scripts. Here i would like to describe a small backup script which backup apache document root to a secure backup location.

The script backup all the folder and file from the apache document root. On execution of the script, will create a backup folder with present date on the backup location. Then it will backup all files and folder into present date backup folder on backup location.

Working scenario of the script,

1. Define Directory Location,

SORC_DIR=/var/www/ #Source location from where we need to backup .

BACK_DIR=/backup/htdocs/ #Backup location to where we need to backup .

TAR_DIR=/backup/compress/htdocs/ #Archive location where the script compress and backup after 2 days.

TMP=/tmp/backdate.tmp #Temp file location

2. Backup directory creation with current date.

3. Copy all the folder and file from source directory to backup location (to inside date folder).

4. Compress the two days old backup location (date folder having backup data from source) and store it on a backup compress location.

5. Remove the old backup folder from first backup location leaving compress backup on the backup location.

Script

#!/bin/bash -e SORC_DIR=/var/www/ BACK_DIR=/backup/htdocs/ TAR_DIR=/backup/compress/htdocs/ TMP=/tmp/backdate.tmp mkdir -p  $BACK_DIR/`date +%Y-%m-%d` cp -rf $SORC_DIR $BACK_DIR/`date +%Y-%m-%d` find /backup/htdocs/ -mtime +2 | cut -d "/" -f4 > $TMP DATE_BACK_DIR=`tail -1 $TMP` COUNT=`wc -m /tmp/backdate.tmp | awk '{print $1}'` if [ $COUNT -eq 0 ];then echo "count 0" rm -rf $TMP else mkdir -p $TAR_DIR tar -cvjf $TAR_DIR/$DATE_BACK_DIR.tar.gz.bz $BACK_DIR/$DATE_BACK_DIR echo "$COUNT" rm -rf $BACK_DIR/$DATE_BACK_DIR rm -rf $TMP fi

Cheers!
Melbin Mathew
www.talk2melbin.com

Melbin Mathew

I am Melbin Mathew, Systems Engineer from Kerala, India. I live with my parents, daughter and my wonderful wife. I graduated in 2004 with a bachelor’s degree in Electronics and Hardware from Mahatma Gandhi University, Kottayam and completed certification in MCITP, RHCE, CCNA, VCP ...Read More
Category: Scripts

About Melbin Mathew

I am Melbin Mathew, Systems Engineer from Kerala, India. I live with my parents, daughter and my wonderful wife. I graduated in 2004 with a bachelor’s degree in Electronics and Hardware from Mahatma Gandhi University, Kottayam and completed certification in MCITP, RHCE, CCNA, VCP ...Read More

  • http://amandabynesnudiesny.xanga.com lilikindsli

    fiRhty I want to say – thank you for this!