时间:2021-07-01 10:21:17 帮助过:20人阅读
版本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数据库分库备份脚本
标签:实例 dump family sock bash events nbsp pass date