##########MYSQL分库备份#######################################
#!/bin/bash MYCMD="mysql -uroot -pjinyan123" #定义了mysql命令 MYDUMP="mysqldump -uroot -pjinyan123" #定义了mysqldump命令 DATABASE=`$MYCMD -e "show databases;"|sed ‘1d‘|egrep -v "mysql|performance_schema|information_schema"` #获取到数据库名 for database in $DATABASE do $MYDUMP -B $database|gzip >/server/backup/${database}_$(date +%F).sql.gz done
############MYSQL分表备份##########################################
#!/bin/bash MYCMD="mysql -uroot-pjinyan123" MYDUMP="mysqldump -uroot -pjinyan123" DATABASE=`$MYCMD -e "show databases"|sed ‘1d‘|egrep -v "mysql|performance_schema|information_schema"` for database in $DATABASE do [ ! -d /server/backup/$database ] && mkdir -p /server/backup/$database #创建以数据库名命名的目录 TABLE=`$MYCMD -e "show tables from $database;"|sed ‘1d‘` #获取表名 for table in $TABLE do $MYDUMP $database $table |gzip >/server/backup/$database/${database}_${table}_$(date +%F).sql.gz #for循环分表备份 done done
本文出自 “运维人生” 博客,谢绝转载!
原文地址:http://jinyan2049.blog.51cto.com/881440/1612330