溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

線上mysql數(shù)據(jù)庫(kù)備份

發(fā)布時(shí)間:2020-05-30 05:35:20 來(lái)源:網(wǎng)絡(luò) 閱讀:499 作者:chenzm0592 欄目:數(shù)據(jù)庫(kù)

#!/bin/bash
#=============================================================================
# FileName: MySQLBackup.sh
# Desc: MySQL Databases backup tool.
# Author: chenzm
# Email:969113439@qq.com
# HomePage: http://chenzm0592.blog.51cto.com/
# Version: 0.0.1
# LastChange: 2013-11-15 09:16:39
# History:
#=============================================================================

cd /opt/MySQLBackup
. /etc/profile

USERNAME='root'
PASSWORD='password'

U_ID=0
G_ID=

# 設(shè)置mysqldump參數(shù)
MYSQLDUMP="mysqldump --opt --default-character-set=utf8 --skip-lock-tables -C"

# 設(shè)置壓縮參數(shù) 從stdin讀取
COMPRESS="gzip -9"

# 設(shè)置壓縮文件的后綴名, 請(qǐng)與壓縮參數(shù)相對(duì)應(yīng)
COMPRESS_EXT="gz"

DATE=$(date +%Y-%m-%d)
mkdir -p logs/

# 定義日志輸入
function log() {
    #echo -ne ${1}
    echo -ne ${1} >>logs/backup-${DATE}.log
}

# 計(jì)算字符時(shí),分, 秒
function charDate() {
    DATE=$1
    if [ ${DATE} -lt 60  ]; then
        echo "${DATE}秒"
    elif [ ${DATE} -ge 60 ] && [ ${DATE} -lt 3600 ]; then
        echo "$((DATE/60))分$((DATE%60))秒"
    else
        M=$((DATE%3600))
        echo "$((DATE/3600))小時(shí)$((M/60))分$((M%60))秒"
    fi
}

TIME_HOST_START=$(date +%s)
retry=0
while :
do
    data=$(mysql -u${USERNAME} -p${PASSWORD} -h IP -e 'SHOW DATABASES' 2>&1)
    if [ $? == 0 ]; then
        break
    elif [ ${retry} -ge 5 ]; then
        log "連接到數(shù)據(jù)庫(kù)失敗...退出.\n"
        exit 1
    fi
    ((retry++))
done

DBS=$(echo ${data} | awk 'BEGIN{split("test|information_schema",arr,"|"); for (i in arr) b[arr[i]]} END{for(i=2;i<=NF;i++) if($i in b==0) print $i}')
DB_TOTAL=$(echo ${DBS} | awk '{print NF}')

log "總計(jì)數(shù)據(jù)庫(kù)數(shù)量: ${DB_TOTAL}\n"
log "正在備份,請(qǐng)稍后....\n"

mkdir -p /databak/IP/db/${DATE}
for DB in ${DBS}
do
    TIME_DB_START=$(date +%s)
    log "正在導(dǎo)出: ${DB}..."
    ${MYSQLDUMP} -u${USERNAME} -p${PASSWORD} -h IP ${DB} 2>>logs/error.log | ${COMPRESS} > /databak/IP/db/${DATE}/${DB}.sql.${COMPRESS_EXT}

    # 使用$PIPESTATUS獲取管道之前的exit status
    if [ ${PIPESTATUS[0]} != 0  ]; then
        log "失敗"
    else
        ((OK++))
        log "成功"
    fi
    TIME_DB_END=$(date +%s)
    ELAPSED=$((TIME_DB_END - TIME_DB_START))
    log " - 耗時(shí): $(charDate ${ELAPSED})\n"
done
#chown -R ${U_ID}:${G_ID} /db/ip/${DATE}

TIME_HOST_END=$(date +%s)
ELAPSED=$((TIME_HOST_END - TIME_HOST_START))
log "總計(jì): ${DB_TOTAL} 成功: ${OK} 失敗: $((DB_TOTAL-OK)) - 耗時(shí): $(charDate ${ELAPSED})\n"
log "$(printf '%30s\n' | tr ' ' -) $(date "+%Y-%m-%d %H:%M:%S") $(printf '%30s\n' | tr ' ' -)\n"
                                                                                           

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI