xtradbbackup 자동화 증량 백업 데이터베이스 구현

7601 단어
#backup.sh  
#!/bin/sh  
#on xtrabackup 2.2.8  
#                     ,             
#           ,                                
   
INNOBACKUPEX_PATH=innobackupex  #INNOBACKUPEX     
INNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEX_PATH  #INNOBACKUPEX       
   
#mysql               
MYSQL_CMD="--host=localhost --user=root --password=xxxxxx --port=3306"    
   
MYSQL_UP=" --user=root --password='xxxxxx' --port=3306 "  #mysqladmin         
   
TMPLOG="/tmp/innobackupex.$$.log"  
   
MY_CNF=/etc/my.cnf #mysql       
   
MYSQL=/usr/local/mysql/bin/mysql  
   
MYSQL_ADMIN=/usr/local/mysql/bin/mysqladmin  
   
BACKUP_DIR=/opt/backup2 #         
   
FULLBACKUP_DIR=$BACKUP_DIR/full #          
   
INCRBACKUP_DIR=$BACKUP_DIR/incre #          
   
FULLBACKUP_INTERVAL=86400 #          ,  :   
   
KEEP_FULLBACKUP=1 #             
   
logfiledate=backup.`date +%Y%m%d%H%M`.txt  
LOGFILE_DIR=$BACKUP_DIR/logs
   
#      
STARTED_TIME=`date +%s`  
   
   
   
#############################################################################  
   
#          
   
#############################################################################  
   
error()  
{  
    echo "$1" 1>&2  
    exit 1  
}  
   
    
   
#         
   
if [ ! -x $INNOBACKUPEXFULL ]; then  
  error "$INNOBACKUPEXFULL        /usr/bin."  
fi  
   
    
   
if [ ! -d $BACKUP_DIR ]; then  
  error "       :$BACKUP_DIR   ."  
fi  
   
    
   
mysql_status=`netstat -nl | awk 'NR>2{if ($4 ~ /.*:3306/) {print "Yes";exit 0}}'`  
   
if [ "$mysql_status" != "Yes" ];then  
    error "MySQL       ."  
fi  
   
   
   
    
   
if ! `echo 'exit' | $MYSQL -s $MYSQL_CMD` ; then  
 error "               !"  
fi  
   
    
   
#          
   
echo "----------------------------"  
echo  
echo "$0: MySQL    "  
echo "   : `date +%F' '%T' '%w`"  
echo  
   
    
   
#              
   
mkdir -p $FULLBACKUP_DIR  
mkdir -p $INCRBACKUP_DIR  
mkdir -p $LOGFILE_DIR
   
   
   
#           
LATEST_FULL_BACKUP=`find $FULLBACKUP_DIR -mindepth 1 -maxdepth 1 -type d -printf "%P
" | sort -nr | head -1` # LATEST_FULL_BACKUP_CREATED_TIME=`stat -c %Y $FULLBACKUP_DIR/$LATEST_FULL_BACKUP` # if [ "$LATEST_FULL_BACKUP" -a `expr $LATEST_FULL_BACKUP_CREATED_TIME + $FULLBACKUP_INTERVAL + 5` -ge $STARTED_TIME ] ; then # echo -e " $LATEST_FULL_BACKUP , $LATEST_FULL_BACKUP " echo " " NEW_INCRDIR=$INCRBACKUP_DIR/$LATEST_FULL_BACKUP mkdir -p $NEW_INCRDIR # . LATEST_INCR_BACKUP=`find $NEW_INCRDIR -mindepth 1 -maxdepth 1 -type d -printf "%P
" | sort -nr | head -1` if [ ! $LATEST_INCR_BACKUP ] ; then INCRBASEDIR=$FULLBACKUP_DIR/$LATEST_FULL_BACKUP echo -e " $INCRBASEDIR " echo " " else INCRBASEDIR=$INCRBACKUP_DIR/${LATEST_FULL_BACKUP}/${LATEST_INCR_BACKUP} echo -e " $INCRBASEDIR " echo " " fi echo " $INCRBASEDIR ." $INNOBACKUPEXFULL --defaults-file=$MY_CNF --use-memory=4G $MYSQL_CMD --incremental $NEW_INCRDIR --incremental-basedir $INCRBASEDIR > $TMPLOG 2>&1 # cat $TMPLOG>$LOGFILE_DIR/$logfiledate if [ -z "`tail -1 $TMPLOG | grep 'innobackupex: completed OK!'`" ] ; then echo "$INNOBACKUPEX :"; echo echo -e "---------- $INNOBACKUPEX_PATH ----------" cat $TMPLOG rm -f $TMPLOG exit 1 fi THISBACKUP=`awk -- "/Backup created in directory/ { split( \\\$0, p, \"'\" ) ; print p[2] }" $TMPLOG` rm -f $TMPLOG echo -n " :$THISBACKUP" echo # LATEST_FULL_BACKUP=`find $FULLBACKUP_DIR -mindepth 1 -maxdepth 1 -type d -printf "%P
" | sort -nr | head -1` NEW_INCRDIR=$INCRBACKUP_DIR/$LATEST_FULL_BACKUP LATEST_INCR_BACKUP=`find $NEW_INCRDIR -mindepth 1 -maxdepth 1 -type d -printf "%P
" | sort -nr | head -1` RES_FULL_BACKUP=${FULLBACKUP_DIR}/${LATEST_FULL_BACKUP} RES_INCRE_BACKUP=`dirname ${INCRBACKUP_DIR}/${LATEST_FULL_BACKUP}/${LATEST_INCR_BACKUP}` echo echo -e '\e[31m NOTE:---------------------------------------------------------------------------------.\e[m' # echo -e " $KEEP_FULLBACKUP ${RES_FULL_BACKUP} ${RES_INCRE_BACKUP} ." echo -e '\e[31m NOTE:---------------------------------------------------------------------------------.\e[m' # echo else echo "*********************************" echo -e " ... ..." echo "*********************************" $INNOBACKUPEXFULL --defaults-file=$MY_CNF --use-memory=4G $MYSQL_CMD $FULLBACKUP_DIR > $TMPLOG 2>&1 # cat $TMPLOG>$LOGFILE_DIR/$logfiledate if [ -z "`tail -1 $TMPLOG | grep 'innobackupex: completed OK!'`" ] ; then echo "$INNOBACKUPEX :"; echo echo -e "---------- $INNOBACKUPEX_PATH ----------" cat $TMPLOG rm -f $TMPLOG exit 1 fi THISBACKUP=`awk -- "/Backup created in directory/ { split( \\\$0, p, \"'\" ) ; print p[2] }" $TMPLOG` rm -f $TMPLOG $INNOBACKUPEXFULL --apply-log $THISBACKUP > $TMPLOG 2>&1 cat $TMPLOG>>$LOGFILE_DIR/$logfiledate echo -n " :$THISBACKUP" echo # LATEST_FULL_BACKUP=`find $FULLBACKUP_DIR -mindepth 1 -maxdepth 1 -type d -printf "%P
" | sort -nr | head -1` RES_FULL_BACKUP=${FULLBACKUP_DIR}/${LATEST_FULL_BACKUP} echo echo -e '\e[31m NOTE:---------------------------------------------------------------------------------.\e[m' # echo -e " , $KEEP_FULLBACKUP ${RES_FULL_BACKUP}." echo -e '\e[31m NOTE:---------------------------------------------------------------------------------.\e[m' # echo fi # echo -e "find expire backup file...........waiting........." echo -e " ">>$LOGFILE_DIR/$logfiledate for efile in $(/usr/bin/find $FULLBACKUP_DIR/ -mtime +6) do if [ -d ${efile} ]; then rm -rf "${efile}" echo -e " :${efile}" >>$BACKUP_DIR/$logfiledate elif [ -f ${efile} ]; then rm -rf "${efile}" echo -e " :${efile}" >>$BACKUP_DIR/$logfiledate fi; done if [ $? -eq "0" ];then echo echo -e " " fi echo echo " : `date +%F' '%T' '%w`" exit 0

이 스크립트는 전체 백업 및 증가분 백업 데이터베이스를 자동화하여 시간당 한 번, 매일 한 번 전체 백업, 한 시간마다 증가분 백업을 수행하도록 설정할 수 있습니다.(PS: 이 스크립트는 내가 CSDN의 대신에게서 찾은 것이고, 내 환경에 맞추어 어느 정도 수정을 한 것이다. ------이 스크립트를 두 시간 동안 보고 나서야 겨우 알아냈다. 쉽지 않은데, 스크립트는 역시 나의 단판이야. 앞으로 자주 쓸 거야!)

좋은 웹페이지 즐겨찾기