MySQL之mysqldump全备份脚本

#!/bin/bash


function  fun_mysql_operations(){
        exp_week=`date +'%w'`
        MYSQLBACKUP_TIME=$(date +"%Y-%m-%d_%H:%M:%S")        
        MYSQL_BASEDIR=$1
        MYSQL_DUMPBASEDIR=$2
        MYSQL_HOSTNAME=$3
        MYSQL_USERNAME=$4
        MYSQL_PASSWORD=$5
        MYSQL_PORT=$6
        MYSQL_SOCKET=$7
        MYSQL_BACKUPFILE_DIR=$8
        MYSQLBACKUPCMD_STATUS=1
      
        MYSQLBACKUP_LOGFILE="${MYSQL_BACKUPFILE_DIR}/${MYSQL_HOSTNAME}_${MYSQL_PORT}_${exp_week}.txt"
        echo '' > $MYSQLBACKUP_LOGFILE      
        echo ${MYSQLBACKUP_LOGFILE}
        MYSQLBACKUP_SIZE=''
        MYSQL_CONNECTION_COMMAND="${MYSQL_BASEDIR} -h${MYSQL_HOSTNAME} -u${MYSQL_USERNAME} -p${MYSQL_PASSWORD}  -P${MYSQL_PORT} -S${MYSQL_SOCKET}"
        MYSQL_FULL_BACKUPFILE="${MYSQL_HOSTNAME}_${MYSQL_PORT}_$exp_week.sql"
 
        if [ ! -x "${MYSQL_BACKUPFILE_DIR}" ]; then
           mkdir -p ${MYSQL_BACKUPFILE_DIR}
           chown -R mysql:mysql ${MYSQL_BACKUPFILE_DIR}
           chmod 750 ${MYSQL_BACKUPFILE_DIR}
        fi
       echo ${MYSQL_BACKUPFILE_DIR}
       if [ !  -f "${MYSQL_FULL_BACKUPFILE}" ]; then
           touch ${MYSQLBACKUP_LOGFILE}
       fi
       # if [  -f "${MYSQL_FULL_BACKUPFILE}" ]; then
       #   echo ${MYSQL_BACKUPFILE_DIR}/${MYSQL_FULL_BACKUPFILE}
       #   #rm -f ${MYSQL_BACKUPFILE_DIR}/${MYSQL_FULL_BACKUPFILE}
       # fi
        ${MYSQL_DUMPBASEDIR} -h${MYSQL_HOSTNAME} -u${MYSQL_USERNAME} -p${MYSQL_PASSWORD}  -P${MYSQL_PORT} -S${MYSQL_SOCKET}   --triggers --routines --master-data=2  --single-transaction  --flush-logs  --all-databases --default-character-set=gbk |gzip  > ${MYSQL_BACKUPFILE_DIR}/${MYSQL_FULL_BACKUPFILE} 2>${MYSQLBACKUP_LOGFILE}
       # echo ${MYSQL_DUMP_COMMAND}
       if [ $? -eq 0 ]; then
          MYSQLBACKUPCMD_STATUS=0
          MYSQLBACKUP_SIZE=`du -sh ${MYSQL_BACKUPFILE_DIR}/${MYSQL_FULL_BACKUPFILE} | awk  '{print $1}'`
          echo "mysqldump success,ok.">>${MYSQLBACKUP_LOGFILE}
          MYSQLBACKUP_ERROR_LOG=`cat ${MYSQLBACKUP_LOGFILE}`
          SQL1="create  table if not exists  mysql.mysql_fullbackup_info_new(MYSQLBACKUP_TIME datetime,MYSQL_HOSTNAME varchar(15),MYSQL_USERNAME varchar(15),MYSQL_PORT int,MYSQLBACKUPCMD_STATUS tinyint,MYSQLBACKUP_ERROR_LOG text,MYSQLBACKUP_SIZE varchar(10));"
        SQL2="insert into  mysql.mysql_fullbackup_info_new(MYSQLBACKUP_TIME,MYSQL_HOSTNAME,MYSQL_USERNAME,MYSQL_PORT,MYSQLBACKUPCMD_STATUS,MYSQLBACKUP_ERROR_LOG,MYSQLBACKUP_SIZE) values(now(),'${MYSQL_HOSTNAME}','${MYSQL_USERNAME}',${MYSQL_PORT},${MYSQLBACKUPCMD_STATUS},'${MYSQLBACKUP_ERROR_LOG}','${MYSQLBACKUP_SIZE}');"
          echo ${SQL1}
          echo ${SQL2}
          ${MYSQL_CONNECTION_COMMAND} -e "${SQL1}"
          $MYSQL_CONNECTION_COMMAND} -e "${SQL2}"
          exit 0
       else
          MYSQLBACKUPCMD_STATUS=1
          MYSQLBACKUP_SIZE=0
          echo "mysqldump fail.">>${MYSQLBACKUP_LOGFILE}
#          ${MYSQL_CONNECTION_COMMAND} -e "${SQL1}"
          ${MYSQL_CONNECTION_COMMAND} -e "${SQL2}"
          exit 1
       fi

}


mysql_connections_info=/root/mysql_connections_info.txt
while read line
do
MYSQL_BASEDIR=`echo $line| awk '{print $1}'`
MYSQL_DUMPBASEDIR=`echo $line| awk '{print $2}'`
MYSQL_HOSTNAME=`echo $line | awk '{print $3}'`
MYSQL_USERNAME=`echo $line | awk '{print $4}'`
MYSQL_PASSWORD=`echo $line | awk '{print $5}'`
MYSQL_PORT=`echo $line | awk '{print $6}'`
MYSQL_SOCKET=`echo $line | awk '{print $7}'`
MYSQL_BACKUPFILE_DIR=`echo $line | awk '{print $8}'`
fun_mysql_operations ${MYSQL_BASEDIR}  ${MYSQL_DUMPBASEDIR} ${MYSQL_HOSTNAME} ${MYSQL_USERNAME} ${MYSQL_PASSWORD}  ${MYSQL_PORT} ${MYSQL_SOCKET} ${MYSQL_BACKUPFILE_DIR}
done < ${mysql_connections_info}
exit 0

你可能感兴趣的:(SHELL脚本及基础知识)