首页 > 代码库 > shell编程基础-shell分库备份

shell编程基础-shell分库备份

分库备份
企业实战题7:如何实现对MySQL数据库进行分库备份,请用脚本实现

#!/bin/bash
MysqlUser=root
PassWord=root
Port=3306
Socket="/usr/include/mysql/mysql.sock"    
MysqlCmd="mysql -u$MysqlUser -p$PassWord -S $Socket"
Database=`$MysqlCmd -e "show databases;"|egrep -v "Database|_schema|mysql"`
MysqlDump="mysqldump-u$MysqlUser -p$PassWord -S $Socket"
IP=`ifconfig enp0s3|grep "broadcast"|awk {print $NF}`   //centos 7
BackupDir=/backup/$IP
[ -d $BackupDir ] || mkdir -p$BackupDir
for dbname in $Database
  do
      $MysqlDump --events -B $dbname|gzip>/$BackupDir/${dbname}_$(date +%F)_bak.sql.gz
done 

shell编程基础-shell分库备份