MySQL分表备份

Posted outsrkem

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL分表备份相关的知识,希望对你有一定的参考价值。

#!/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分表备份的主要内容,如果未能解决你的问题,请参考以下文章

mysql分表备份

Shell脚步之MySql分库分表备份

MySQL分库分表备份

Mysql数据库分库备份,分表备份

MySQL分库分表备份脚本

16MySQL数据库分库分表备份脚本