ext3 snapshot backup
#!/bin/bash
#
# created by nhko 090612
# version 0.3
#############################################################
# ** 추가된 기능 **
#############################################################
# 프로그램 중복실행 체크
# 백업 디스크 유무 체크
# rsync 패스 자동검색
# 프로그램 실행 전 check_disk.pl 수행
# 백업 파일명 수정
# source 를 /home/download 전체로 지정.
# 원하는 파일은 찾는 시간보다 한시간 후를 기준으로 해야 한다.
#############################################################
# Define the variable
DATE=`date +%y%m%d%H%M`;
SDATE=`date +%F-%T`;
/bin/mkdir -p /var/lock/snapshot
# Create array
declare -a FILENAME
FILENAME[1]=`date +%y%m%d%H%M`
# Check the rsync path
/usr/bin/whereis rsync | awk '{print $2}' > /var/lock/snapshot/rsyncpath
COPY=`cat /var/lock/snapshot/rsyncpath`;
# Check the Start time
/bin/mkdir -p /root/log
echo "Start Time... "$SDATE >> /root/log/snapshot_start.log;
# SYNC the DISK
/bin/sync; /bin/sleep 5;
# Check the free space of disk
/root/system_tools/monitor/check_disk.pl > /var/lock/snapshot/check_disk
if [ -s /var/lock/snapshot/check_disk ]
then
echo "Check the free space of disk"
exit 1;
fi
# Check the target disk
/bin/mount | grep snapshot > /dev/null 2>&1
if [ $? -ne 0 ]
then
echo "Target is not exist"
exit 1;
fi
# Check the process duplication
if [ -f /var/lock/snapshot/sn ]
then
echo "This Script is running"
/bin/rm -f /var/lock/snapshot/sn
echo "Touch file was deleted. Try again"
exit 1;
fi
# Make a touch file
/bin/touch /var/lock/snapshot/sn
############################################################################
# BACKUP PROCESS ###########################################################
# remount a target directory(ro -> rw)
/bin/mount -o remount,rw /dev/sdb8 /snapshot
echo "success the remount"
# delete a expire file(7일 보관)
/bin/rm -f /tmp/COUNTLIST
cd /snapshot; find . -maxdepth 1 -type d -mtime +7 | egrep -v 'backup|\.$' | awk '{print "/bin/rm -rf /snapshot/"$0}' > /tmp/COUNTLIST
/bin/chmod 700 /tmp/COUNTLIST && /tmp/COUNTLIST
# disconect inode
if [ ! -z ${FILENAME[1]} ]
then
/bin/cp -al /snapshot/backup.0 /snapshot/${FILENAME[1]} >> /root/system_tools/log/hardlinkcopy.log
touch -r /var/lock/snapshot/rsyncpath /snapshot/${FILENAME[1]}
echo "disconnect inode"
fi
echo "$COPY";
# rsync data
/bin/mkdir -p /snapshot/backup.0/var
/bin/mkdir -p /snapshot/backup.0/sysadmin
/bin/mkdir -p /snapshot/backup.0/root
/bin/mkdir -p /snapshot/backup.0/cron
$COPY -a --delete /home/mysql/var/ /snapshot/backup.0/var >> /root/system_tools/log/rsync_mysqlvar.log
$COPY -a --delete /home/sysadmin/ /snapshot/backup.0/sysadmin >> /root/system_tools/log/rsync_sysadmin.log
$COPY -a --delete /root/ /snapshot/backup.0/root >> /root/system_tools/log/rsync_root.log
$COPY -a --delete /var/spool/cron/ /snapshot/backup.0/cron >> /root/system_tools/log/rsync_cron.log
touch -r /var/lock/snapshot/rsyncpath /snapshot/backup.0
echo "rsync a data"
# remount a target directory(rw -> ro)
/bin/mount -o remount,ro /dev/sdb8 /snapshot
echo "success the remount"
############################################################################
# Check the End time
EDATE=`date +%F-%T`;
echo "End Time... "$EDATE >> /root/log/snapshot_end.log;
# SYNC the DISK
/bin/sync; /bin/sleep 5;
# Remove the touch file
/bin/rm -f /var/lock/snapshot/sn