码迷,mamicode.com
首页 > 数据库 > 详细

MySQL分表备份

时间:2019-07-27 22:25:23      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:UNC   creation   ESS   base   The   logfile   tar   failed   bin   

#!/bin/bash
DUMP=/usr/bin/mysqldump
MYSQL=/usr/bin/mysql
IPADDR=127.0.0.1
PORT=3306
USER=abc
PASSWD=123456
DATABASE=test
ROOT_DIR=/data
LogFile=/var/log/mysqldunp.log
DATE=`date +%Y%m%d`
function info() {
    echo -e "[`date +%Y-%m-%d\ %H:%M:%S`] - INFO "  $@" " >> $LogFile
}
function error() {
    echo -e "[`date +%Y-%m-%d\ %H:%M:%S`] - ERROR " $@" " >> $LogFile
}

function backup(){
    table=$1
    $MYSQLDUMP $DATABASE $table > ${OUTDIR}/${DATABASE}-${table}-${DATE}.sql
    if [ $? == 0 ];then
        info  "${DATABASE} database ${table} table Backup successfully!"
    else
        error "${DATABASE}-${table} Backup failure"
    fi
}

MYSQLCMD="$MYSQL -h$IPADDR -P$PORT -u$USER -p$PASSWD"
MYSQLDUMP="$DUMP -h$IPADDR -P$PORT -u$USER -p$PASSWD"
info "Database backup starts `date +%Y-%m-%d\ %H:%M:%S`"

OUTDIR=${ROOT_DIR}/mysql/${DATE}/${DATABASE}
[ -d $OUTDIR ] || mkdir -p $OUTDIR
if [ -d $OUTDIR ];then
    info "Directory created successfully! -- ${ROOT_DIR}/mysql/${DATE}/${DATABASE}"
else
    error "${ROOT_DIR}/mysql/${DATE}/${DATABASE}  Directory creation failed."
    error "Backup to terminate."
fi

table=(`$MYSQLCMD -e "show tables from $DATABASE;" | sed '1d'`)
info "tables : ${table[*]}"
for table in ${table[*]}
do
   backup $table
done

MySQL分表备份

标签:UNC   creation   ESS   base   The   logfile   tar   failed   bin   

原文地址:https://www.cnblogs.com/outsrkem/p/11256950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!