mysql数据库分库备份脚本
Posted 盛碗米饭
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql数据库分库备份脚本相关的知识,希望对你有一定的参考价值。
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
示例脚本:
#!/bin/bash
BAKPATH=/server/backup
MYUSER=root
MYPASS=oldboy123
SOCKET=/data/3306/mysql.sock
MYCMD="mysql -u$MYUSER -p$MYPASS -S $SOCKET"
MYDUMP="mysqldump -u$MYUSER -p$MYPASS -S $SOCKET -x -B -F -R"
[ ! -d $BAKPATH ] && mkdir -p $BAKPATH
DBLIST=`$MYCMD -e "show databases;" | sed 1d | egrep -v "_schema|mysql"`
for dbname in $DBLIST
do
$MYDUMP $dbname |gzip >/server/backup/${dbname}_$(date +%F).sql.gz
done
以上是关于mysql数据库分库备份脚本的主要内容,如果未能解决你的问题,请参考以下文章