Shell脚本范例(七)——MySQL分库分表备份

本文分享常用的一些Shell脚本的例子,这是第七篇——MySQL分库分表备份。

要求:请用脚本实现对MySQL数据库的分库分表备份。

参考:

#!/bin/bash
#Author: Oliver King
#Blog: http://www.oliver.ren

PATH="/usr/local/mysql/bin:$PATH"
DBPATH=/tmp/backup
MYUSER=root
MYPASS=password
SOCKET=/usr/local/mysql/data/mysql.sock
MYCMD="mysql -u$MYUSER -p$MYPASS -S $SOCKET"
MYDUMP="mysqldump -u$MYUSER -p$MYPASS -S $SOCKET"

[ ! -d "$DBPATH" ]

for dbname in `$MYCMD -e "show databases;"|sed '1,2d'|egrep -v "mysql|schema"`
do
    mkdir -p $DBPATH/${dbname}_$(date +%F)
    for tbname in `$MYCMD -e "show tables from $dbname;"|sed '1d'`
    do
        $MYDUMP $dbname $tbname|gzip > $DBPATH/${dbname}_$(date +%F)/${dbname_${tbname}.sql.gz
    done
done

标签: none