간단한 백업용 종합선물 쉘스크립트

2010. 2. 2. 17:0299. 정리전 - IT/13. Unix 얇팍지식

1. 전체 스크립트관리(시작과 끝을 알리는 메일도 발송하자)

#!/bin/bash
##########################################################
#
#  backup管理(full backup)
#
##########################################################
echo "*** start backup ***"

#当日日付の取得
today=`date +"%Y%m%d"`

#10日前の日付取得
#agoday=`date -d '10 day ago' +"%Y%m%d"`

#送信先メールアドレスの設定
#email=svnw@azix.jp
email=donz@azix.jp

#開始メール送信
NOW=`date +'%Y%m%d_%H%M%S'`
echo "Backupを開始します - "$NOW | mail -s "[notice] Backup_Start" $email

#backup script 開始
echo "*** 01.ソース backup ***"
/home/donz/01.all_src_backup_rsync.sh &
echo "*** 03.イメージ(biz) backup ***"
/home/donz/03.com_img_backup_rsync.sh &
echo "*** 02.ドリブロイメージ(biz) backup ***"
/home/donz/02.biz_img_backup_rsync.sh

#終了メール送信
NOW=`date +'%Y%m%d_%H%M%S'`
echo "Backupが終了しました - "$NOW | mail -s "[notice] Backup_End" $email

echo "*** end backup ***"
exit



2. 압축 후 알씽크 rsync
#!/bin/sh

FILENAME=`date +'%Y%m%d'`STARTTIME=`date +'%Y%m%d_%H%M%S'`
LOGFILE="/home/donz/log/"$STARTTIME"_xxx_all_src_backup_rsync.log"
echo ""
echo "--------------------------------------------------------------------------------------------"
echo "  [ 1. START TIME ]" $STARTTIME >> $LOGFILE
echo "  [    MAKING A TAR - BIZ ]" >> $LOGFILE
echo "  [ 1. START TIME ]" $STARTTIME
echo "  [    MAKING A TAR - BIZ ]"
tar zcvf $FILENAME"_xxx_biz.tar.gz" /home/project/xxx.biz --exclude .svn --exclude log --exclude logs --exclude cache --exclude media --exclude media2 --exclude uploads
ENDTIME=`date +'%Y%m%d_%H%M%S'`
echo "  [ 2. END TIME ]" $ENDTIME >> $LOGFILEecho "  [ 2. END TIME ]" $ENDTIME

STARTTIME=`date +'%Y%m%d_%H%M%S'`
echo "  [ 3. START TIME ]" $STARTTIME >> $LOGFILE
echo "  [    MAKING A TAR - COM ]" >> $LOGFILE
echo "  [ 3. START TIME ]" $STARTTIME
echo "  [    MAKING A TAR - COM ]"
tar zcvf $FILENAME"_xxx_com.tar.gz" /home/project/xxx.com --exclude .svn --exclude log --exclude logs --exclude cache
 --exclude media --exclude media2 --exclude uploads
ENDTIME=`date +'%Y%m%d_%H%M%S'`
echo "  [ 4. END TIME ]" $ENDTIME >> $LOGFILEecho "  [ 4. END TIME ]" $ENDTIME
echo "--------------------------------------------------------------------------------------------"
echo ""
STARTTIME=`date +'%Y%m%d_%H%M%S'`
echo "  [ 5. START TIME ]" $STARTTIME >> $LOGFILE
echo "  [    RSYNC - BIZ ]" >> $LOGFILE
echo "  [ 5. START TIME ]" $STARTTIME
echo "  [    RSYNC - BIZ ]"
rsync -avzO --progress --bwlimit=3000 "/home/donz/"$FILENAME"_xxx_biz.tar.gz" homepage@192.168.71.4:/home/homepage/dolibl
o_src_full_backup >> $LOGFILE
ENDTIME=`date +'%Y%m%d_%H%M%S'`
echo "  [ 6. END TIME ]" $ENDTIME >> $LOGFILE
echo "  [ 6. END TIME ]" $ENDTIME

STARTTIME=`date +'%Y%m%d_%H%M%S'`
echo "  [ 7. START TIME ]" $STARTTIME >> $LOGFILE
echo "  [    RSYNC - COM ]" >> $LOGFILE
echo "  [ 7. START TIME ]" $STARTTIME
echo "  [    RSYNC - COM ]"
rsync -avzO --progress --bwlimit=3000 "/home/donz/"$FILENAME"_xxx_com.tar.gz" homepage@192.168.71.4:/home/homepage/xxx_src_full_backup >> $LOGFILE
ENDTIME=`date +'%Y%m%d_%H%M%S'`
echo "  [ 8. END TIME ]" $ENDTIME >> $LOGFILE
echo "  [ 8. END TIME ]" $ENDTIME
echo "--------------------------------------------------------------------------------------------"
echo ""
echo "  [ 9. REMOVE ] "$FILENAME"_xxx_biz.tar.gz"
rm -rf "/home/donz/"$FILENAME"_xxx_biz.tar.gz"
echo "  [ 10. REMOVE ] "$FILENAME"_xxx_com.tar.gz"
rm -rf "/home/donz/"$FILENAME"_xxx_com.tar.gz"
exit


3. 대량의 이미지만 알싱크
#!/bin/sh

STARTTIME=`date +'%Y%m%d_%H%M%S'`
LOGFILE="/home/donz/log/"$STARTTIME"_xxx_biz_img_backup_rsync.log"
echo ""
echo "--------------------------------------------------------------------------------------------"
echo "  [ START TIME ]" $STARTTIME >> $LOGFILE
echo "  [ START TIME ]" $STARTTIME
echo "  [ /home/project/xxx.biz/www.xxx.biz/sfproject/web/uploads -> 192.168.71.4:/home/homepage/xxx_img_full_backup ]"
echo "     create log file... "
echo "     you can see the logfile at realtime, typing next allow"
echo ""
echo "     --->   tail -f " $LOGFILE
echo ""
rsync -avzO --progress --bwlimit=3000 --exclude=/blog /home/project/xxx.biz/www.xxx.biz/sfproject/web/uploads homepage@192.168.71.4:/home/homepage/xxx_img_full_backup >> $LOGFILE
ENDTIME=`date +'%Y%m%d_%H%M%S'`
echo "  [ END TIME ]" $ENDTIME >> $LOGFILE
echo "  [ END TIME ]" $ENDTIME
echo "--------------------------------------------------------------------------------------------"
exit


4. 데이터베이스 백업(MySQL)
#!/bin/sh

FILENAME=`date +'%Y%m%d'`
STARTTIME=`date +'%Y%m%d_%H%M%S'`
LOGFILE="/home/donz/log/"$STARTTIME"_xxx_database_backup_rsync.log"
echo ""
echo "--------------------------------------------------------------------------------------------"
echo "  [ 1. START TIME ]" $STARTTIME >> $LOGFILE
echo "  [    XXX DATABASE DUMP ]" >> $LOGFILE
echo "  [ 1. START TIME ]" $STARTTIME
echo "  [    XXX DATABASE DUMP ]"
mysqldump -h192.168.71.5 -uroot xxx_new > "/home/donz/"$FILENAME"_xxx_new.dump"
tar zcvf "/home/donz/"$FILENAME"_xxx_new.dump.tar.gz" "/home/donz/"$FILENAME"_xxx_new.dump"
ENDTIME=`date +'%Y%m%d_%H%M%S'`
echo "  [ 2. END TIME ]" $ENDTIME >> $LOGFILE
echo "  [ 2. END TIME ]" $ENDTIME
echo "--------------------------------------------------------------------------------------------"
echo ""
STARTTIME=`date +'%Y%m%d_%H%M%S'`
echo "  [ 3. START TIME ]" $STARTTIME >> $LOGFILE
echo "  [    RSYNC - DB DUMP DATA ]" >> $LOGFILE
echo "  [ 3. START TIME ]" $STARTTIME
echo "  [    RSYNC - DB DUMP DATA ]"
rsync -avzO --progress --bwlimit=3000 "/home/donz/"$FILENAME"_xxx_new.dump.tar.gz" homepage@192.168.71.4:/home/homepage/xxx_database_backup >> $LOGFILE
ENDTIME=`date +'%Y%m%d_%H%M%S'`
echo "  [ 4. END TIME ]" $ENDTIME >> $LOGFILE
echo "  [ 4. END TIME ]" $ENDTIME
echo "--------------------------------------------------------------------------------------------"
echo ""
echo "  [ 5. REMOVE ] "$FILENAME"_xxx_new.dump, "$FILENAME"_xxx_new.dump.tar.gz"
rm -rf "/home/donz/"$FILENAME"_xxx_new.dump"
rm -rf "/home/donz/"$FILENAME"_xxx_new.dump.tar.gz"
exit


5. 유통기한이 지난 파일은 삭제
echo "start remove task"

#当日日付の取得
today=`date +"%Y%m%d"`

#15日前の日付取得
agoday=`date -d '10 day ago' +"%Y%m%d"`

#ディレクトリ(image_down)の初期化
rm -rf "/home/homepage/xxx_src_full_backup/"$agoday"_xxx_biz.tar.gz"
rm -rf "/home/homepage/xxx_src_full_backup/"$agoday"_xxx_com.tar.gz"

echo "end remove task"
exit



6. 클론설정
# 毎日夕方2000に実行
00 20 * * * /home/donz/파일명.sh > /dev/null 2>&1

7. 결과를 메일로 발송
cat 파일이름.log | mail -s 제목 leedongjoo@i.softbank.jp