Linux Unix help !!

"Give respect to Time, One day at right Time, Time will respect You"

Sunday, March 11, 2012

backup using script imp files linux

Many time we come across situation to take a backup of file daily/weekly or sometime every hour .

Yes it may be that your filesystem are auto backuped by your backup tools as Snapshot/Veritas netbackup etc.. but still we always come across situation to that we want our some critical file (/etc/passwd, /etc/httpd/conf/httpd.conf etc..)  that had changed 1 hour back .

Below a small and simple script that can help us a way .. just schedule this in your crontab .. as per your requirement .

## MyBackup.sh
#!/bin/bash
#Shirish Shukla
#######################################
## Files to be backuped
Sources="/etc/passwd, /etc/group, /etc/shadow"
## Backup Save at Target:
Targetpath=/usr/Backup
#######################################
if [ ! -d $Target-path ]
then
    mkdir -p $Targetpath
fi
#######################################
create=`date +%a-%d-%b-%Y-AT:%H:%M`
delete=`date --date '1 month ago' +%a-%d-%b-%Y`
#######################################
for fles in `echo $Sources`
do
   fle=`echo $fles | sed  "s/,//g"`
    if [ ! -e $fle ]
    then
          echo "file: $fle not EXIST"
    fi
    lst=` echo $fle | awk -F"/" '{print $NF}'`
    tar -jcf $Targetpath/$lst-$create.bz2 $fle
done
#######################################
## Now delete 1 month old backups if exists any
rm -fr  $Targetpath/*$delete*
#######################################

## Check Backup
# ls -lrt  /usr/Backup/
-rw-r--r-- 1 root root 500 Feb 11 11:47 shadow-Sat-11-Feb-2012-AT:11:47.bz2
-rw-r--r-- 1 root root 938 Feb 11 11:47 passwd-Sat-11-Feb-2012-AT:11:47.bz2
-rw-r--r-- 1 root root 500 Feb 11 11:47 group-Sat-11-Feb-2012-AT:11:47.bz2


## Schedule crontab to take it 2 hour
# chmod 755 /root/MyBackup.sh
# crontab -e
01  */2 * * *  /root/MyBackup.sh


> Shirish Shukla

1 comment:

Write Here .. your comments are always wellcome ..but no spam please !!

Followers

Pls LIKE my Story !!!