RSS구독하기:SUBSCRIBE TO RSS FEED
즐겨찾기추가:ADD FAVORITE
글쓰기:POST
관리자:ADMINISTRATOR
요즘에 회사에서 써먹을려고 다시 짠 백업 스크립트
행여나 이 블로그에 흘러들어와서 그걸 그대로 쓴다면 무궁무진한 오류가 뜨니 주의하시요!

#!/bin/bash
#edit by uzoogom 090723
#
# backup configure
host=$(hostname)
day=$(date +%Y-%m-%d)
check=$(mount | grep -c "backup")

# Backup Storage Mounting Check
if [ $check == 1 ]
        then
                echo "Backup Storage Mounting Check .... OK!!"
        else
                echo "Backup Storage Mounting Check .... FAIL!!"
                echo "Exit backup scripts"
                exit 0
fi

# Make a backup Directory
mkdir -p /backup/${host}/{webdata,data,config} 2>/dev/null
mkdir /root/backup/reference 2>/dev/null

# 백업 할 디렉토리를 적을 파일 생성

touch /root/backup/reference/{webdir,datadir,configfile}

# Lists to Backup 백업 할 디렉토리를 적을 파일 생성
WEBDATA=$(grep -v "^#" /root/backup/reference/webdir)
DATA=$(grep -v "^#" /root/backup/reference/datadir)
CONFIG=$(grep -v "^#" /root/backup/reference/configfile)

# TMP Directory
LIST="/tmp/backuplist_$$.txt"

# Target Backup Locations
WEBDATABACKUP="/backup/${host}/webdata"
DATABACKUP="/backup/${host}/data"
CONFIGBACKUP="/backup/${host}/config"
DATABASEBACKUP="/backup/${host}/database"

#
echo "
========================================================================
Starting backup at `date +%Y-%m-%d\ %H:%M:%S`
========================================================================
"

# date to nubmer 증분 백업을 위해서 날자 지정
# 1-Mon, 2-Tue, 3-Wed, 4-Thu, 5-Fri, 6-Sat, 7-Sun
set $(date '+%u')

# backup Time stamp
rm -f /backup/${host}/backupTime*.txt
echo Starting at `date +%Y-%m-%d\ %H:%M:%S` > "/backup/${host}/backupTime($day).txt"

if test "$1" = "6" ;
then
        # weekly a full backup of all data and config. settings:
        #
        echo "Step1. Performing Full Data Backup--------------------------------------"
        tar zcf "$DATABACKUP/data_full_$day.tgz" $DATA 2>/dev/null

        echo "Removing incremental data backups..."
        rm -f $DATABACKUP/data_diff*

        echo "Removing four week old full Data backup..."
        find $DATABACKUP -depth -type f \( -ctime +20 -o -mtime +20 \) -print > $LIST
        rm -f `cat $LIST`
        echo "------------------------------------------------------------------Done +"
        echo ""

        #
        echo "Step2. Performing Full WebData Backup-----------------------------------"
        tar zcf "$WEBDATABACKUP/webdata_full_$day.tgz" --exclude-from=./reference/exclude  $WEBDATA 2>/dev/null

        echo "Removing incremental data backups..."
        rm -f $WEBDATABACKUP/webdata_diff*

        echo "Removing four week old full WebData backup..."
        find $WEBDATABACKUP -depth -type f \( -ctime +20 -o -mtime +20 \) -print > $LIST
        rm -f `cat $LIST`
        echo "------------------------------------------------------------------Done +"
        echo ""

        #
        echo ""
        echo "Step3. Performing Full Config Backup------------------------------------"
        tar zcf "$CONFIGBACKUP/config_full_$day.tgz" $CONFIG 2>/dev/null

        echo "Removing incremental config backups..."
        rm -f $CONFIGBACKUP/config_diff*

        echo "Removing four week old full config backup..."
        find $CONFIGBACKUP -depth -type f \( -ctime +20 -o -mtime +20 \) -print > $LIST
        rm -f `cat $LIST`
        echo "------------------------------------------------------------------Done +"
else
        # incremental data backup:
        #
        echo "Step1. Performing Incremental Data Backup-------------------------------"
        find $DATA -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
        tar zcfT "$DATABACKUP/data_diff_$day.tgz" "$LIST" 2>/dev/null
        rm -f "$LIST"
        echo "------------------------------------------------------------------Done +"
        echo ""

        # incremental webdata backup:
        #
    echo "Step2. Performing Incremental WebData Backup----------------------------"
        find $WEBDATA -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
        tar zcfT "$WEBDATABACKUP/webdata_diff_$day.tgz" --exclude-from=./reference/exclude "$LIST" 2>/dev/null
        rm -f "$LIST"
        echo "------------------------------------------------------------------Done +"
        echo ""

        # incremental config backup:
        #
        echo "Step3. Performing Incremental Config Backup-----------------------------"
        find $CONFIG -depth -type f  \( -ctime -1 -o -mtime -1 \) -print > $LIST
        tar zcfT "$CONFIGBACKUP/config_diff_$day.tgz" "$LIST" 2>/dev/null
        rm -f "$LIST"
        echo "------------------------------------------------------------------Done +"
fi

# MySql 용 백업 설정이나, MySql을 마땅히 백업할 서버가 없어서 주석처리
# Database Backup
#
#mkdir -p /backup/${host}/database 2>/dev/null
#DB_DIR="/usr/local/mysql/data"
#
# Create SQL dump of databases and compress:
#echo "
#Archiving Databases...
#"
# Find current databases
# DB=( $(find ${DB_DIR}/ -maxdepth 1 -type d ! -name ".*" | sort | sed -e 's/\/usr\/local\/mysql\/data\///g') )
# Set for loop max counter based on number of databases
# j=${#DB[@]}
# for (( i=0; i<j; i++ )); do
#         mysqldump -u user --password=passwd --opt ${DB[i]} | gzip >"$DATABASEBACKUP/${DB[i]}_$day.sql.gz"
# done
#
#echo "
#Removing two week old database backups...
#"
#find $DATABASEBACKUP -depth -type f \( -ctime +13 -o -mtime +13 \) -print > $LIST
#rm -f `cat $LIST`
#rm -f "$LIST"
#

# backup Time stamp
echo Finished at `date +%Y-%m-%d\ %H:%M:%S` >> "/backup/${host}/backupTime($day).txt"
echo "
========================================================================
Finished: `date +%Y-%m-%d\ %H:%M:%S`
========================================================================
"
exit 0
우주곰:지구곰이 아닙니다.
지구곰이 아닙니다.
Categories (190)
Information (5)
About uzoogom (5)
My Advanced Linux (73)
Learning Linux (96)
OperatingSystem (5)
Databases (4)
Tips! (1)
OpenSource (1)
«   2018/06   »
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
  1. 2012/12 (2)
  2. 2012/04 (3)
  3. 2012/03 (6)
  4. 2012/02 (6)
  5. 2012/01 (2)