mysql数据库分库备份脚本

 

版本1

for dbname in `mysql -uroot -poldboy123 -e "show databases;" |grep -Evi "database|informa|perfor"`

do

    mysqldump -uroot -poldboy123 --events -B $dbname|gzip >/opt/bak/${dbname}_bak.sql.gz

done

 

版本2

#!/bin/bash

MYUSER=root

MYPASS=oldboy123

MYCMD="mysql -u$MYUSER -p$MYPASS --default-character-set=utf8"

MYDUMP="mysqldump -u$MYUSER -p$MYPASS --default-character-set=utf8 --events -B"

DBLIST=`$MYCMD -e "show databases;" | grep -Evi "database|informa|perfor"`

 

[ ! -d /opt/bak ] && mkdir -p /opt/bak

 

for dbname in $DBLIST

do

    $MYDUMP $dbname|gzip >/opt/bak/${dbname}_bak.sql.gz

done

 

多实例分库备份

#!/bin/bash

MYUSER=root

MYPASS=oldboy123

SOCKET=/data/3306/mysql.sock

MYCMD="mysql -u$MYUSER -p$MYPASS --default-character-set=utf8 -S $SOCKET"

MYDUMP="mysqldump -u$MYUSER -p$MYPASS --default-character-set=utf8 -S $SOCKET --events -B"

DBLIST=`$MYCMD -e "show databases;" | grep -Evi "database|informa|perfor"`

 

[ ! -d /opt/bak ] && mkdir -p /opt/bak

 

for dbname in $DBLIST

do

    $MYDUMP $dbname|gzip >/opt/bak/${dbname}_bak.sql.gz

done