"linux/script/example/mysql backup"의 두 판 사이의 차이

Redjini WiKi
< linux‎ | script‎ | example
 
(차이 없음)

2018년 4월 4일 (수) 12:37 기준 최신판




소스

#!/bin/sh
#==================================================================================================
MYSQL_PATH="/opt/lampp/bin"      #MYSQL    실행경로
MYSQL_USER="root"                #         계정
MYSQL_PWD="mysqlpasswd"          #         계정
BK_PATH="/data/backup/mysql"     #백업파일 저장 위치
#==================================================================================================

EXE_PATH=$(readlink -f $(dirname "$0"))
BK_DATE=$(date +"%Y%m%d_%H%M%S")
LOG_PATH="${BK_PATH}/${BK_DATE}.log"

#--------------------------------------------------------------------------------------------------
if [ ! -d "${BK_PATH}" ] ; then
    mkdir -p "${BK_PATH}"
fi

if [ ! -d "${BK_PATH}/temp" ] ; then
    mkdir -p "${BK_PATH}/temp"
fi

#--------------------------------------------------------------------------------------------------
echo "[$(date +"%Y%m%d_%H%M%S")] =================================" >> $LOG_PATH
echo "[$(date +"%Y%m%d_%H%M%S")] MOVE PATH ${BK_PATH}/temp"         >> $LOG_PATH
echo "[$(date +"%Y%m%d_%H%M%S")] =================================" >> $LOG_PATH
#--------------------------------------------------------------------------------------------------

cd "${BK_PATH}/temp"

#MySQL Database 목록 조회
DB_LIST=`echo "show databases;" | ${MYSQL_PATH}/mysql -N -u${MYSQL_USER} -p${MYSQL_PWD}`
for DB in $DB_LIST; do
    #MySQL Database / Table 목록 조회
    TABLE_LIST=`echo "show tables" | ${MYSQL_PATH}/mysql -N -u${MYSQL_USER} -p${MYSQL_PWD} $DB `
    if [ $? -eq 0 ]; then
        #------------------------------------------------------------------------------------------
        echo "[$(date +"%Y%m%d_%H%M%S")] BACKUP DB:${DB}"           >> $LOG_PATH
        #------------------------------------------------------------------------------------------
        for TABLE in $TABLE_LIST; do
            echo "[$(date +"%Y%m%d_%H%M%S")]        >> ${TABLE}"    >> $LOG_PATH
            ${MYSQL_PATH}/mysqldump --skip-lock-tables -u${MYSQL_USER} -p${MYSQL_PWD} "${DB}" "${TABLE}" > "${BK_PATH}/temp/${BK_DATE}_${DB}_${TABLE}.sql"
        done
        #------------------------------------------------------------------------------------------
    else
        echo "[$(date +"%Y%m%d_%H%M%S")] ${DB} TABLE FAIL"          >> $LOG_PATH
    fi
done

#--------------------------------------------------------------------------------------------------
echo "[$(date +"%Y%m%d_%H%M%S")] TAR : ${BK_DATE}.tgz"              >> $LOG_PATH
tar zcvf ../${BK_DATE}.tgz *.sql > /dev/null

echo "[$(date +"%Y%m%d_%H%M%S")] CLEAN "                            >> $LOG_PATH
rm -rf *.sql
#--------------------------------------------------------------------------------------------------

참고