SysDump.sh 스크립트

 

1. 용도

OS Disk의 시스템 파티션 영역 Backup

 

2. 설치방법

만약 sda(OS Disk), sdb(Data Disk) 가 있다고 가정하면 아래와 같다.

  • 백업 대상 디스크 : sda
  • 백업 데이터 저장 디스크 : sdb

/data1 - sdb Disk에 속한 디렉토리라고 한다면 /data1 에 SysDump.sh 를 위치시키고 실행 권한 설정을 한다.

# chmod +x /data1/SysDump.sh

 

3. 사용법

1) 직접 실행

# /data1/SysDump.sh sda1,sda3,sda5 /data1

-> OS Disk의 swap을 제외한 파티션을 , 로 구분지어 지정.
-> 백업대상 파티션은 OS 디스크에 속한 파티션에 한정됨.

# /data1/SysDump.sh sda1 /data1

-> 만약 한개의 파티션만 백업한다면 하나만 설정.

2) cron 스케쥴 실행

# crontab -e

00 13 * * 1 /bin/bash /data1/SysDump.sh sda1,sda3,sda5 /data1

-> 월요일 오후 1시에 sda1,sda3,sda5 파티션정보&데이터를 /data1/Backup-System 디렉토리에 로테이션 설정에 맞게 보관 되도록 dd 백업.

3) 실행 결과

스크립트가 실행되어 백업이 완료되면 /data1/Backup-System 디렉토리 안의 First 디렉토리에 압축된 백업파일이 확인된다.
스크립트 실행 여부 로그가 /var/log/messages에 기록됨.

 

아래는 SysDump.sh - bash shell 스크립트 코드임.

#!/bin/bash


################################ 환경 변수 ###################################
RotateCycle=0
Compression=on
# RotateCycle 값은 0 ~ 3 까지 지정 가능함.
##############################################################################

# 스크립트 실행 시 인자 두개를 받는다. 만약 인자 2개가 만족되지 못하면 사용법 메시지를 출력
# 받은 인자는 각각 $TargetDisk, $Dest 변수값이 된다.
ARGS=2

if [ $# -ne $ARGS ]; then
  echo "사용법: `basename $0` 타겟디스크 백업볼륨"
  exit $E_BADARGS
fi

# 쉘로부터 받은 인자를 각각 TargetDisk와 Dest 변수로 변경. 이유는 MvImg, DDImg, FirstDDImg 함수에서 $1, $2 변수가 먹지 않는다.
if [ `echo $1 | grep ','` ]; then
    TargetDisk=$(echo $1 | awk -F "," '{print $1}' | sed -s 's/[1-99]//g')
    echo -e "\033[1;32mTarget Disk     : $TargetDisk \033[0m"
    IN=$1
    List=$(echo $IN | tr "," "\n")
    cnt=0
    for Part in $List
    do
        cnt=$((cnt+1))
        echo -e "\033[1;32mPartitions $cnt    : $Part \033[0m"
    done
    echo -e "\033[1;32m---------------------------- \033[0m"
    echo -e "\033[1;32mStore Directory : $2 \033[0m"
else
    TargetDisk=$(echo $1 | awk -F "," '{print $1}' | sed -s 's/[1-99]//g')
    echo -e "\033[1;32mTarget Disk     : $TargetDisk \033[0m"
    Part=$1
    echo -e "\033[1;32mPartitions 1    : $Part \033[0m"
    echo -e "\033[1;32m---------------------------- \033[0m"
    echo -e "\033[1;32mStore Directory : $2 \033[0m"
fi

TargetPart=$1
Dest=$2
LastPart=$(echo $Part | tail -n 1)

Dest=$2

PATH=$PATH:/bin:/sbin:/usr/bin:/usr/sbin
Host=`hostname`
Mtime=$(date --rfc-2822 | awk -F " " '{print $3 " " $2 " " $5}')
CurrDate=$(date --rfc-2822 | awk -F " " '{print $3 " " $2}')
CheckDay=$(echo "$CurrDate" | awk -F "" '{print $5}')
BackupDir=$Dest/Backup-System

cd $Dest ;

SwapPart=$(fdisk -l /dev/$TargetDisk | grep swap | awk -F " " '{ print $1 }' | awk -F "/" '{ print $3 }')

MvImg () {
    if [ -f $BackupDir/3-week/3-week-Comp.tar.gz ]; then
        mv -f $BackupDir/3-week/3-week-Comp.tar.gz $BackupDir/4-week/4-week-Comp.tar.gz >& /dev/null
	mv -f $BackupDir/3-week/{*.img,*.sf,*.mbr,*.part,*.gz} $BackupDir/4-week >& /dev/null ; mv -f $BackupDir/3-week/SysInfo/* $BackupDir/4-week/SysInfo >& /dev/null
    fi
    if [ -f $BackupDir/2-week/2-week-Comp.tar.gz ]; then
        mv -f $BackupDir/2-week/2-week-Comp.tar.gz $BackupDir/3-week/3-week-Comp.tar.gz >& /dev/null
    	mv -f $BackupDir/2-week/{*.img,*.sf,*.mbr,*.part,*.gz} $BackupDir/3-week >& /dev/null ; mv -f $BackupDir/2-week/SysInfo/* $BackupDir/3-week/SysInfo >& /dev/null
    fi
    if [ -f $BackupDir/1-week/1-week-Comp.tar.gz ]; then
        mv -f $BackupDir/1-week/1-week-Comp.tar.gz $BackupDir/2-week/2-week-Comp.tar.gz >& /dev/null
    	mv -f $BackupDir/1-week/{*.img,*.sf,*.mbr,*.part,*.gz} $BackupDir/2-week >& /dev/null ; mv -f $BackupDir/1-week/SysInfo/* $BackupDir/2-week/SysInfo >& /dev/null
    fi
}

DDImg () {
if [  `echo $TargetPart | grep ','` ]; then
    TargetDisk=$(echo $TargetPart | awk -F "," '{print $1}' | sed -s 's/[1-99]//g')
    IN=$TargetPart
    List=$(echo $IN | tr "," "\n")
    for Part in $List
    do
        sync
        dd if=/dev/$Part of=$BackupDir/$WEEK/$Part".img" bs=10M ;
    done
else
    TargetDisk=$(echo $TargetPart | awk -F "," '{print $1}' | sed -s 's/[1-99]//g')
    Part=$TargetPart
    sync
    dd if=/dev/$Part of=$BackupDir/$WEEK/$Part".img" bs=10M ;
fi
    sfdisk -d /dev/$TargetDisk > $BackupDir/$WEEK/$TargetDisk.sf ;
    sync
    dd if=/dev/$TargetDisk of=$BackupDir/$WEEK/$TargetDisk.mbr count=1 bs=446 ;
    dd if=/dev/$TargetDisk of=$BackupDir/$WEEK/$TargetDisk.part count=1 bs=512 ;
}

SysInfoRecord () {
    find $BackupDir/$WEEK/SysInfo >& /dev/null
    if [ $? == "1" ]; then
        mkdir -p $BackupDir/$WEEK/SysInfo
    fi
    sync;
    cat /proc/cpuinfo > $BackupDir/$WEEK/SysInfo/cpu.info
    crontab -l > $BackupDir/$WEEK/SysInfo/crontab.info
    df > $BackupDir/$WEEK/SysInfo/df.info
    echo "" >> $BackupDir/$WEEK/SysInfo/df.info
    echo "------------ human-readable -----------" >> $BackupDir/$WEEK/SysInfo/df.info
    df -h >> $BackupDir/$WEEK/SysInfo/df.info
    dmidecode > $BackupDir/$WEEK/SysInfo/dmidecode.info
    fdisk -l > $BackupDir/$WEEK/SysInfo/fdisk.info
    cat /etc/fstab > $BackupDir/$WEEK/SysInfo/fstab.info
    echo $Host > $BackupDir/$WEEK/SysInfo/hostname.info
    ifconfig -a > $BackupDir/$WEEK/SysInfo/ifconfig.info
    netstat -rn > $BackupDir/$WEEK/SysInfo/route.info
    lspci > $BackupDir/$WEEK/SysInfo/lspci.info
    cat /proc/meminfo > $BackupDir/$WEEK/SysInfo/mem.info
    cat /etc/rc.d/rc.local > $BackupDir/$WEEK/SysInfo/rclocal.info
    cat /etc/redhat-release > $BackupDir/$WEEK/SysInfo/release.info
    uname -r -m > $BackupDir/$WEEK/SysInfo/uname.info
    cat /proc/scsi/scsi > $BackupDir/$WEEK/SysInfo/disk.info
    cat /proc/ide/hd*/model >& /dev/null
    if [ $? == "0" ]; then
        echo "" >> $BackupDir/$WEEK/SysInfo/disk.info
        echo "IDE Disk Info:" >> $BackupDir/$WEEK/SysInfo/disk.info
        ls /dev/hd* | grep hd* | egrep -v 'hd[a-z][0-9]' >> $BackupDir/$WEEK/SysInfo/disk.info
        echo "-----------" >> $BackupDir/$WEEK/SysInfo/disk.info
        cat /proc/ide/hd*/model >> $BackupDir/$WEEK/SysInfo/disk.info
    fi
    if [ -d /home/webpage ]; then
        cd /home ;
        tar zcf webpage.tar.gz webpage/
        mv webpage.tar.gz $BackupDir/$WEEK
        cd $Dest ;
    fi
}	

Compress () {
find $BackupDir/$WEEK/"$LastPart".img >& /dev/null
CheckIMG2=$?
find $BackupDir/$WEEK/"$LastPart".img -size 0 | grep img >& /dev/null
CheckIMG22=$?
if [ $Compression == "on" ]; then
    cd $BackupDir
    echo "In file compression... Takes a few minutes."
    tar zcf $WEEK-Comp.tar.gz $WEEK/ && find $BackupDir/$WEEK -type f | xargs rm -f >& /dev/null ; rm -f $BackupDir/$WEEK/SysInfo/* >& /dev/null
    mv $WEEK-Comp.tar.gz $BackupDir/$WEEK
fi
if [ $CheckIMG2 == "0" ] && [ $CheckIMG22 == "1" ]; then
    echo -e "\033[1;32mSysDump: System $WEEK Dump Success. \033[0m"
    if [ $CheckDay == "0" ]; then
        Mtime2=$(echo "$Mtime" | awk -F "" '{print $1 $2 $3 " " $4 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15}')
        echo "$Mtime2 $Host SysDump: System $WEEK Dump Success." >> /var/log/messages
    else
        echo "$Mtime $Host SysDump: System $WEEK Dump Success." >> /var/log/messages
    fi
else
    echo -e "\033[1;31mSysDump: System $WEEK Dump Error. Check nFile System. \033[0m"
    if [ $CheckDay == "0" ]; then
        Mtime2=$(echo "$Mtime" | awk -F "" '{print $1 $2 $3 " " $4 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15}')
        echo "$Mtime2 $Host SysDump: System $WEEK Dump Error. Check nFile System." >> /var/log/messages
    else
        echo "$Mtime $Host SysDump: System $WEEK Dump Error. Check nFile System." >> /var/log/messages
    fi
fi
exit
}

Rotate () {
    find $BackupDir/$RWEEK/*.img >& /dev/null
    CheckFile1=$?
    find $BackupDir/$RWEEK/*-Comp.tar.gz >& /dev/null
    CheckFile2=$?
    if [ $CheckFile1 == "0" ] || [ $CheckFile2 == "0" ]; then
        rm -f $BackupDir/$RWEEK/*.{img,sf,mbr,part,gz} ; rm -f $BackupDir/$RWEEK/SysInfo/*
        find $BackupDir/4-week/*.img >& /dev/null
        CheckFile11=$?
        find $BackupDir/4-week/*-Comp.tar.gz >& /dev/null
        CheckFile22=$?
        if [ $CheckFile11 == "0" ] || [ $CheckFile22 == "0" ]; then
            rm -f $BackupDir/4-week/*.{img,sf,mbr,part,gz} ; rm -f $BackupDir/4-week/SysInfo/*
        fi
        MvImg ;
    else
        MvImg ;
    fi
}

find $BackupDir >& /dev/null
if [ $? == "1" ]; then
	mkdir -p $BackupDir/{1,2,3,4}-week/SysInfo
	chmod 700 $BackupDir ;
fi

find $BackupDir/First/*.img >& /dev/null
CheckFirstFile1=$?
find $BackupDir/First/*-Comp.tar.gz >& /dev/null
CheckFirstFile2=$?
if [ $CheckFirstFile1 == "1" ] && [ $CheckFirstFile2 == "1" ]; then
    WEEK=First
    find $BackupDir/$WEEK/SysInfo >& /dev/null
    if [ $? == "1" ]; then
	    mkdir -p $BackupDir/$WEEK/SysInfo
    fi
    DDImg ;
    SysInfoRecord ;
    Compress ;
fi

if [ $RotateCycle == "3" ]; then
find $BackupDir/First/*-Comp.tar.gz >& /dev/null
CheckFirstFile2=$?
if [ $CheckFirstFile1 == "1" ] && [ $CheckFirstFile2 == "1" ]; then
    WEEK=First
    find $BackupDir/$WEEK/SysInfo >& /dev/null
    if [ $? == "1" ]; then
	    mkdir -p $BackupDir/$WEEK/SysInfo
    fi
    DDImg ;
    SysInfoRecord ;
    Compress ;
fi

if [ $RotateCycle == "3" ]; then
    find $BackupDir/4-week/*.img >& /dev/null
    CheckFile1=$?
    find $BackupDir/4-week/*-Comp.tar.gz >& /dev/null
    CheckFile2=$?
    if [ $CheckFile1 == "0" ] || [ $CheckFile2 == "0" ]; then
		rm -f $BackupDir/4-week/*.{img,sf,mbr,part,gz} ; rm -f $BackupDir/4-week/SysInfo/*
		MvImg ;
	else
		MvImg ;
	fi
elif [ $RotateCycle == "2" ]; then
    RWEEK=3-week
    Rotate ;
elif [ $RotateCycle == "1" ]; then
    RWEEK=2-week
    Rotate ;
elif [ $RotateCycle == "0" ]; then
    RWEEK=1-week
    Rotate ;
else
    echo -e "\033[1;31mSysDump: System Dump Error. Checking - Sysdump.sh RotateCycle variable. \033[0m"
	if [ $CheckDay == "0" ]; then
        Mtime2=$(echo "$Mtime" | awk -F "" '{print $1 $2 $3 " " $4 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15}')
        echo "$Mtime2 $Host SysDump: System Dump Error. Checking - Sysdump.sh RotateCycle variable." >> /var/log/messages
    else
        echo "$Mtime $Host SysDump: System Dump Error. Checking - Sysdump.sh RotateCycle variable." >> /var/log/messages
    fi	
	exit
fi

WEEK=1-week
DDImg ;
SysInfoRecord ;
Compress ;

 

SysDump 스크립트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다