Welcome 微信登录

首页 / 数据库 / MySQL / 远程备份MySQL数据库脚本

最近有几台线上数据库,硬盘比较吃紧,但是又不方便扩容和转移到其它机器。但是数据又比较重要,需要经常备份出来。最早的做法是,在本地备份出完整的库,把备份库和binlog一并推到远程的某台备份主机上。但是因为最近空间吃紧,而备份库需要占用不小的空间。有没有其它的解决办法呢?查看了mysqldump具备远程备份的功能,可以直接将远程数据备份到本地来,这样就可以大大的节省了远程数据库的磁盘空间。所以我尝试写了一个备份脚本,再配合crontab定时执行,目前看来效果还不错。线上环境稍加修改可直接使用,如有问题可以联系我O(∩_∩)O~注意:此法多适用于内网,外网的话需要考虑数据量大小和带宽等网络因素。脚本内容如下:
  1. #by cuiyuanrong   
  2. #远程备份mysql数据和binlog日志到本地!  
  3.  
  4. mysql_user="admin" 
  5. mysql_password="admin_password" 
  6. remote_ips=`cat /data/shell_files/ips.txt`  
  7. mysql_port="3306" 
  8. TIME=`date "+%Y%m%d"`   
  9.  
  10. source /etc/profile  
  11. rm -f /data/shell_files/db.list   
  12. #循环每个IP 
  13. for remote_ip in $remote_ips  
  14. do
  15. #读取每个远程mysql服务器中的库名  
  16. echo "=========== $remote_ip `date "+%Y_%m_%d-%H:%M:%S"` ===========" >> /data/shell_files/history_mysql_backup.log  
  17. /app/mysql/bin/mysql --host="$remote_ip" --port="$mysql_port" --user="$mysql_user" --password="$mysql_password" << EOF > /data/shell_files/db.list  
  18. show databases;  
  19. exit  
  20. EOF  
  21. #用sed过滤掉无需备份的库 
  22.    sed -i "/Database|information_schema|performance_schema|test/d" /data/shell_files/db.list  
  23. #创建所需的备份目录
  24.    mkdir -p /data/mysql_backup/$TIME/$remote_ip/data/  
  25.    mkdir -p /data/mysql_backup/$TIME/$remote_ip/binlog/  
  26. #循环备份每个库
  27.    for DB in `cat /data/shell_files/db.list`  
  28.    do  
  29.    echo "begin dump $DB `date "+%Y_%m_%d-%H:%M:%S"`" >> /data/shell_files/history_mysql_backup.log  
  30.    /app/mysql/bin/mysqldump --host="$remote_ip" --port="$mysql_port" --user="$mysql_user" --password="$mysql_password" --opt --flush-logs --master-data=2 --single-transaction --default-character-set=utf8 $DB | gzip -f > /data/mysql_backup/$TIME/$remote_ip/data/$DB.sql.gz  
  31.    echo "end dump $DB " >> /data/shell_files/history_mysql_backup.log  
  32.    done  
  33. #备份binlog日志文件          
  34.    echo "begin rsync binlogs $remote_ip" >> /data/shell_files/history_mysql_backup.log  
  35.    rsync -e "ssh -p 22 -o StrictHostKeyChecking=no" -avz --progress --delete root@$remote_ip:/data/mysql/binlog/ /data/mysql_backup/$TIME/$remote_ip/binlog/  
  36.    echo "end rsync binlogs $remote_ip" >> /data/shell_files/history_mysql_backup.log  
  37.           
  38.    sleep 60  
  39. done
MySQL删除binlog日志及日志恢复数据MySQL 5.5.19/20同步一个BUG相关资讯      MySQL基础 
  • MySQL因为区分大小写而引起找不到  (09/08/2012 20:12:21)
  • MySQL 执行计划解读  (09/07/2012 06:18:14)
  • MySQL审计插件安装使用说明文档--  (08/09/2012 08:57:10)
  • MySQL 版本选择  (09/07/2012 06:18:58)
  • MySQL Internal 笔记  (09/01/2012 16:34:19)
  • MySQL的“[Warning] Invalid (old?  (08/09/2012 08:55:01)
本文评论 查看全部评论 (0)
表情: 姓名: 字数