xtradbbackup 자동 증량 백업 데이터베이스 구현(업데이트 1)

14916 단어
#!/bin/bash 
#on xtrabackup 2.2.8  
#                     ,             
#           ,                               
 
INNOBACKUPEX_PATH=innobackupex  #INNOBACKUPEX     
INNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEX_PATH  #INNOBACKUPEX       

#mysql               

#MYSQL_CMD="--host=192.168.8.70 --user=root --password=xiaozhao1 --port=3306"    
TMPLOG="/tmp/innobackupex.$$.log"  
MY_CNF=/etc/my.cnf #mysql       
MYSQL=/usr/bin/mysql
MYSQL_ADMIN=/usr/bin/mysqladmin 
BACKUP_DIR=/mysql_backup #         
FULLBACKUP_DIR=$BACKUP_DIR/full #          
INCRBACKUP_DIR=$BACKUP_DIR/incre #          
FULLBACKUP_INTERVAL=604800 #          ,  :   
KEEP_FULLBACKUP=5 #             
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  
}  

#         

# -x file        
if [ ! -x $INNOBACKUPEXFULL ]; then  
  error "$INNOBACKUPEXFULL        /usr/bin."  
fi 

#-d file        
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  



#          

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 -a -and 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 --incremental $NEW_INCRDIR --incremental-basedir $INCRBASEDIR > $TMPLOG 2>&1 # cat $TMPLOG>$LOGFILE_DIR/$logfiledate if [ -z "`tail -1 $TMPLOG | grep '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 $FULLBACKUP_DIR > $TMPLOG 2>&1 # cat $TMPLOG>>$LOGFILE_DIR/$logfiledate if [ -z "`tail -1 $TMPLOG | grep '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 +7) do if [ -d ${efile} ]; then echo -e " :${efile}" >>$BACKUP_DIR/$logfiledate rm -rf "${efile}" elif [ -f ${efile} ]; then echo -e " :${efile}" >>$BACKUP_DIR/$logfiledate rm -rf "${efile}" fi; done if [ $? -eq "0" ];then echo echo -e " " fi # echo -e "find expire backup file...........waiting........." echo -e " ">>$LOGFILE_DIR/$logfiledate for efile in $(/usr/bin/find $INCRBACKUP_DIR/ -mtime +8) do if [ -d ${efile} ]; then echo -e " :${efile}" >>$BACKUP_DIR/$logfiledate rm -rf "${efile}" elif [ -f ${efile} ]; then echo -e " :${efile}" >>$BACKUP_DIR/$logfiledate rm -rf "${efile}" fi; done if [ $? -eq "0" ];then echo echo -e " " fi echo echo " : `date +%F' '%T' '%w`" exit 0

2 사용자 이름 및 암호 포함
#!/bin/bash 
#on xtrabackup 2.2.8  
#                     ,             
#           ,                               
 
INNOBACKUPEX_PATH=innobackupex  #INNOBACKUPEX     
INNOBACKUPEXFULL=/usr/bin/$INNOBACKUPEX_PATH  #INNOBACKUPEX       

#mysql               

MYSQL_CMD="--host=xxxxxx --user=root --password=xxxxx --port=3306"    
TMPLOG="/tmp/innobackupex.$$.log"  
MY_CNF=/etc/my.cnf #mysql       
MYSQL=/usr/bin/mysql
MYSQL_ADMIN=/usr/bin/mysqladmin 
BACKUP_DIR=/mysql_backup #         
FULLBACKUP_DIR=$BACKUP_DIR/full #          
INCRBACKUP_DIR=$BACKUP_DIR/incre #          
FULLBACKUP_INTERVAL=604800 #          ,  :   
KEEP_FULLBACKUP=5 #             
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  
}  

#         

# -x file        
if [ ! -x $INNOBACKUPEXFULL ]; then  
  error "$INNOBACKUPEXFULL        /usr/bin."  
fi 

#-d file        
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  



#          

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 -a -and 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 '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 '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 +7) do if [ -d ${efile} ]; then echo -e " :${efile}" >>$BACKUP_DIR/$logfiledate rm -rf "${efile}" elif [ -f ${efile} ]; then echo -e " :${efile}" >>$BACKUP_DIR/$logfiledate rm -rf "${efile}" fi; done if [ $? -eq "0" ];then echo echo -e " " fi # echo -e "find expire backup file...........waiting........." echo -e " ">>$LOGFILE_DIR/$logfiledate for efile in $(/usr/bin/find $INCRBACKUP_DIR/ -mtime +8) do if [ -d ${efile} ]; then echo -e " :${efile}" >>$BACKUP_DIR/$logfiledate rm -rf "${efile}" elif [ -f ${efile} ]; then echo -e " :${efile}" >>$BACKUP_DIR/$logfiledate rm -rf "${efile}" fi; done if [ $? -eq "0" ];then echo echo -e " " fi echo echo " : `date +%F' '%T' '%w`" exit 0

좋은 웹페이지 즐겨찾기