Loading

MySQL 多库备份脚本

创建MySQL备份用户: https://www.cnblogs.com/outsrkem/p/18528941

#!/bin/bash
DUMP="docker exec -it some-mysql /bin/mysqldump"
IPADDR=127.0.0.1
PORT=3306
USER=backupuser
PASSWD=backupuser
DATABASE=(db1 db2)
ROOT_DIR=/data
LogFile=/data/mysqldunp.log
DATE="`date +%Y%m%d`"
MYSQLDUMP="$DUMP -h$IPADDR -P$PORT -u$USER -p$PASSWD"



function log_info(){
    echo -e "[`date +%Y-%m-%d\ %H:%M:%S`] - INFO "  $@" " >> $LogFile
}
function log_error(){
    echo -e "[`date +%Y-%m-%d\ %H:%M:%S`] - ERROR " $@" " >> $LogFile
}
function backup(){
    local database=$1
    local outdir="${ROOT_DIR}/mysql/${DATE}"
    [ -d $outdir ] || mkdir -p $outdir
    $MYSQLDUMP --databases --skip-extended-insert $database |gzip > ${outdir}/${database}-${DATE}.sql.gz
    if [ $? == 0 ];then
        log_info  "${database} database ${table} table Backup successfully!"
    else
        log_error "${database}-${table} Backup failure 100"
        exit 100
    fi
}


log_info "Database backup starts `date +%Y-%m-%d\ %H:%M:%S`"


for db in ${DATABASE[*]}
do
    backup $db
done

 

posted @ 2024-11-05 21:49  Outsrkem  阅读(5)  评论(0编辑  收藏  举报