这篇文章主要讲解了“如何实现mysql的分库备份”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何实现mysql的分库备份”吧!
分库备份实际上就是执行一个备份语句(mysqldump)备份一个库,如果数据库里面有多个库,就执行多条相同的备份单个库的备份语句进行备份,注意每个库都可以用对应的备份的库做为库名,结尾加.sql.
我写了如下的一个脚本进行分库备份:
[root@Mysql01 mysqldumpbak]# cat /usr/local/script/mysqldump.sh
#!/bin/bash
cd /opt/db_backup/db_backup/mysql_bak/
rq=`date +%F`
mkdir $rq
cd $rq
echo '开始备份时间:' `date` >> backup.log
#由于本脚本备份是采用了gizp压缩备份,所以加压时可以用gzip xxx.sql.gz进行解压
mysql -udbbackup_user -p'xxxx' -e "show databases" |grep -Evi "database|perfor|infor" |sed -r 's/^([a-z].*$)/mysqldump -udbbackup_user -p'xxx' –routines –triggers –events –single-transaction –master-data=2 –log-error=backup.err -B /1|gzip > /1.sql.gz/g' |bash
echo '结束备份时间:' `date` >> backup.log
测试:
[root@Mysql01 mysqldumpbak]#sh /usr/local/script/mysqldump.sh
执行该脚本后,会生产如下sql语句,并自动执行如下sql进行备份
/home/mysql/mysql/bin/mysqldump -udbbackup_user -pxx –routines –triggers –events –single-transaction –master-data=2 –log-error=backup.err -B app|gzip > app.sql.gz
/home/mysql/mysql/bin/mysqldump -udbbackup_user -pxx –routines –triggers –events –single-transaction –master-data=2 –log-error=backup.err -B ad|gzip > auod.sql.gz
/home/mysql/mysql/bin/mysqldump -udbbackup_user -pxx –routines –triggers –events –single-transaction –master-data=2 –log-error=backup.err -B ad_moerben|gzip > auod_moerben.sql.gz
/home/mysql/mysql/bin/mysqldump -udbbackup_user -pxx –routines –triggers –events –single-transaction –master-data=2 –log-error=backup.err -B ccms|gzip > cz_bj_cms.sql.gz
/home/mysql/mysql/bin/mysqldump -udbbackup_user -pxx –routines –triggers –events –single-transaction –master-data=2 –log-error=backup.err -B cs|gzip > cz_bj_oms.sql.gz
感谢各位的阅读,以上就是“如何实现mysql的分库备份”的内容了,经过本文的学习后,相信大家对如何实现mysql的分库备份这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/202338.html