使用xtrabackup全部备份与恢复数据库的真实案例

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:
具体关于xtrabackup的备份与恢复,可以参考以下文章
生产环境究竟是使用mysqldump还是xtrabackup来备份与恢复数据库?
下面是一个生产环境的全部备份恢复案例
主要是备份我之前做的php+mysql+shell的monitor数据库,这个数据库的数据文件4.2G
 
 
  1. 4.2G    ibdata1  
  2. 5.1M    ib_logfile0  
  3. 5.1M    ib_logfile1  
  4. 1.8M    monitor  
之前一直使用mysqldump备份,但恢复的时候非常的慢,3个小时都恢复不了,所以放弃了使用mysqldump来备份与恢复数据库的方案,改用xtrabackup来备份与恢复。
备份与恢复上面都描述了,下面就列出我恢复4.2G的数据库使用的时间
恢复的日志是
 
  1. start time is 20130313-13:34:35  
  2. start to stop mysql server  
  3. start to restore database  
  4.  
  5. InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy  
  6. and Percona Ireland Ltd 2009-2012.  All Rights Reserved.  
  7.  
  8. This software is published under  
  9. the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.  
  10.  
  11. IMPORTANT: Please check that the apply-log run completes successfully.  
  12.            At the end of a successful apply-log run innobackupex  
  13.            prints "completed OK!".  
  14.  
  15.  
  16.  
  17. 130313 13:34:35  innobackupex: Starting ibbackup with command: xtrabackup_51  --defaults-file="/usr/local/monitor/data/2013-03-13_13-28-28/backup-my.cnf"  --defaults-group="mysqld" --prepare --target-dir=/usr/local/monitor/data/2013-03-13_13-28-28  
  18.  
  19. xtrabackup_51 version 2.0.5 for MySQL server 5.1.59 unknown-linux-gnu (x86_64) (revision id: undefined)  
  20. xtrabackup: cd to /usr/local/monitor/data/2013-03-13_13-28-28  
  21. xtrabackup: This target seems to be not prepared yet.  
  22. xtrabackup: xtrabackup_logfile detected: size=2097152start_lsn=(2 31133758)  
  23. xtrabackup: Temporary instance for recovery is set as followings.  
  24. xtrabackup:   innodb_data_home_dir = ./  
  25. xtrabackup:   innodb_data_file_path = ibdata1:10M:autoextend  
  26. xtrabackup:   innodb_log_group_home_dir = ./  
  27. xtrabackup:   innodb_log_files_in_group = 1 
  28. xtrabackup:   innodb_log_file_size = 2097152 
  29. xtrabackup: Temporary instance for recovery is set as followings.  
  30. xtrabackup:   innodb_data_home_dir = ./  
  31. xtrabackup:   innodb_data_file_path = ibdata1:10M:autoextend  
  32. xtrabackup:   innodb_log_group_home_dir = ./  
  33. xtrabackup:   innodb_log_files_in_group = 1 
  34. xtrabackup:   innodb_log_file_size = 2097152 
  35. xtrabackup: Starting InnoDB instance for recovery.  
  36. xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)  
  37. InnoDB: The InnoDB memory heap is disabled  
  38. 130313 13:34:35  InnoDB: Initializing buffer pool, size = 100.0M  
  39. 130313 13:34:35  InnoDB: Completed initialization of buffer pool  
  40. InnoDB: Log scan progressed past the checkpoint lsn 2 31133758  
  41. 130313 13:34:35  InnoDB: Database was not shut down normally!  
  42. InnoDB: Starting crash recovery.  
  43. InnoDB: Reading tablespace information from the .ibd files...  
  44. InnoDB: Doing recovery: scanned up to log sequence number 2 31272048 (7 %)  
  45. 130313 13:34:36  InnoDB: Starting an apply batch of log records to the database...  
  46. InnoDB: Progress in percents: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99   
  47. InnoDB: Apply batch completed  
  48. 130313 13:34:37  InnoDB: Started; log sequence number 2 31272048  
  49.  
  50. [notice (again)]  
  51.   If you use binary log and don't use any hack of group commit,  
  52.   the binary log position seems to be:  
  53.  
  54. xtrabackup: starting shutdown with innodb_fast_shutdown = 1 
  55. 130313 13:34:37  InnoDB: Starting shutdown...  
  56. 130313 13:34:42  InnoDB: Shutdown completed; log sequence number 2 31272048  
  57.  
  58. 130313 13:34:42  innobackupex: Restarting xtrabackup with command: xtrabackup_51  --defaults-file="/usr/local/monitor/data/2013-03-13_13-28-28/backup-my.cnf"  --defaults-group="mysqld" --prepare --target-dir=/usr/local/monitor/data/2013-03-13_13-28-28  
  59. for creating ib_logfile*  
  60.  
  61. xtrabackup_51 version 2.0.5 for MySQL server 5.1.59 unknown-linux-gnu (x86_64) (revision id: undefined)  
  62. xtrabackup: cd to /usr/local/monitor/data/2013-03-13_13-28-28  
  63. xtrabackup: This target seems to be already prepared.  
  64. xtrabackup: notice: xtrabackup_logfile was already used to '--prepare'.  
  65. xtrabackup: Temporary instance for recovery is set as followings.  
  66. xtrabackup:   innodb_data_home_dir = ./  
  67. xtrabackup:   innodb_data_file_path = ibdata1:10M:autoextend  
  68. xtrabackup:   innodb_log_group_home_dir = ./  
  69. xtrabackup:   innodb_log_files_in_group = 2 
  70. xtrabackup:   innodb_log_file_size = 5242880 
  71. xtrabackup: Temporary instance for recovery is set as followings.  
  72. xtrabackup:   innodb_data_home_dir = ./  
  73. xtrabackup:   innodb_data_file_path = ibdata1:10M:autoextend  
  74. xtrabackup:   innodb_log_group_home_dir = ./  
  75. xtrabackup:   innodb_log_files_in_group = 2 
  76. xtrabackup:   innodb_log_file_size = 5242880 
  77. xtrabackup: Starting InnoDB instance for recovery.  
  78. xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)  
  79. InnoDB: The InnoDB memory heap is disabled  
  80. 130313 13:34:42  InnoDB: Initializing buffer pool, size = 100.0M  
  81. 130313 13:34:42  InnoDB: Completed initialization of buffer pool  
  82. 130313 13:34:42  InnoDB: Log file ./ib_logfile0 did not exist: new to be created  
  83. InnoDB: Setting log file ./ib_logfile0 size to 5 MB  
  84. InnoDB: Database physically writes the file full: wait...  
  85. 130313 13:34:42  InnoDB: Log file ./ib_logfile1 did not exist: new to be created  
  86. InnoDB: Setting log file ./ib_logfile1 size to 5 MB  
  87. InnoDB: Database physically writes the file full: wait...  
  88. InnoDB: The log sequence number in ibdata files does not match  
  89. InnoDB: the log sequence number in the ib_logfiles!  
  90. 130313 13:34:42  InnoDB: Database was not shut down normally!  
  91. InnoDB: Starting crash recovery.  
  92. InnoDB: Reading tablespace information from the .ibd files...  
  93. 130313 13:34:42  InnoDB: Started; log sequence number 2 31272460  
  94.  
  95. [notice (again)]  
  96.   If you use binary log and don't use any hack of group commit,  
  97.   the binary log position seems to be:  
  98.  
  99. xtrabackup: starting shutdown with innodb_fast_shutdown = 1 
  100. 130313 13:34:42  InnoDB: Starting shutdown...  
  101. 130313 13:34:48  InnoDB: Shutdown completed; log sequence number 2 31272460  
  102. 130313 13:34:48  innobackupex: completed OK!  
  103. operation is success!  
  104. operation is success!  
  105. start to start mysql server  
  106. Starting MySQL:  [  OK  ]  
  107. operation is success!  
  108. finish time is 20130313-13:34:49  
可以看到仅用了14秒就恢复成功。
所以我现在都使用xtrabackup来备份与恢复数据库,下面是我的全部备份与增量备份的脚本
全部备份的脚本xtrabackup_full.sh
 
  1. [root@beiyong shell]# cat xtrabackup_full.sh   
  2. #!/bin/bash  
  3. local_ip="$(/sbin/ifconfig eth0|grep 'inet addr'|awk -F : '{print $2}'|cut -d ' ' -f1)" 
  4. email='denglei@ctfo.com' 
  5. user='root' 
  6. passwd='123456' 
  7. database='monitor' 
  8. my_config='/etc/my.cnf' 
  9. log=$database-$(date +%Y%m%d%H%M).log  
  10. str=$database-$(date +%Y%m%d%H%M).tar.gz  
  11. backup_dir='/usr/local/monitor/data' 
  12. echo "Start to backup at $(date +%Y%m%d%H%M)"  
  13. if [ ! -d "$backup_dir" ];then  
  14.     mkdir -p $backup_dir  
  15. fi  
  16. #innobackupex --user=$user --password=$passwd --defaults-file=$my_config --database=$database --stream=tar $backup_dir 2>$backup_dir/$log | gzip 1>$backup_dir/$str  
  17. innobackupex --user=$user --password=$passwd --defaults-file=$my_config --database=$database $backup_dir  
  18. if [ $? -eq 0 ];then  
  19.     echo "Backup is finish! at $(date +%Y%m%d%H%M)"  
  20.     echo "Server_name:$(hostname) Server_ip:$local_ip $(date +"%y-%m-%d %H:%M:%S") mysql full backup Success!"|/bin/mail -s "Database: [$database} Daily Full Backup Success!" $email  
  21.     exit 0  
  22. else  
  23.     echo "Backup is Fail! at $(date +%Y%m%d%H%M)"  
  24.     echo "Server_name:$(hostname) Server_ip:$local_ip $(date +"%y-%m-%d %H:%M:%S") mysql full backup Fail!"|/bin/mail -s "Database: [$database} Daily Full Backup Fail!" $email  
  25.     exit 1  
  26. fi  
  27. echo "Backup Process Done"  
增量备份的脚本xtrabackup_incremental.sh
 
  1. [root@beiyong shell]# cat xtrabackup_incremental.sh   
  2. #!/bin/bash  
  3. local_ip="$(/sbin/ifconfig eth0|grep 'inet addr'|awk -F : '{print $2}'|cut -d ' ' -f1)" 
  4. email='denglei@ctfo.com' 
  5. user='root' 
  6. passwd='123456' 
  7. database='monitor' 
  8. my_config='/etc/my.cnf' 
  9. log=$database-$(date +%Y%m%d%H%M).log  
  10. str=$database-$(date +%Y%m%d%H%M).tar.gz  
  11. backup_dir='/usr/local/monitor/data/' 
  12. last_day=$(date -d "1 days ago" +%Y-%m-%d)  
  13. today=$(date +%Y%m%d)  
  14. filename=$(find $backup_dir -name "$last_day*" -print|awk -F / '{print $NF}')  
  15. echo "Start to backup at $(date +%Y%m%d%H%M)"  
  16. if [ ! -d "$backup_dir" ];then  
  17.     mkdir -p $backup_dir  
  18. fi  
  19. #innobackupex --user=$user --password=$passwd --defaults-file=$my_config --database=$database --stream=tar $backup_dir 2>$backup_dir/$log | gzip 1>$backup_dir/$str  
  20. innobackupex --user=$user --password=$passwd --defaults-file=$my_config --database=$database --incremental --incremental-basedir=$backup_dir/$filename $backup_dir  
  21. if [ $? -eq 0 ];then  
  22.     echo "Backup is finish! at $(date +%Y%m%d%H%M)"  
  23.     echo "Server_name:$(hostname) Server_ip:$local_ip $(date +"%y-%m-%d %H:%M:%S") mysql incremental backup Success!"|/bin/mail -s "Database: [$database} Daily incremental Backup Success!" $email  
  24.     exit 0  
  25. else  
  26.     echo "Backup is Fail! at $(date +%Y%m%d%H%M)"  
  27.     echo "Server_name:$(hostname) Server_ip:$local_ip $(date +"%y-%m-%d %H:%M:%S") mysql incremental backup Fail!"|/bin/mail -s "Database: [$database} Daily incremental Backup Fail!" $email  
  28.     exit 1  
  29. fi  
  30. echo "Backup Process Done"  
结合crontab来实现自动的备份
 
  1. 10 00 * * 0 /usr/local/monitor/shell/xtrabackup_full.sh>> /usr/local/monitor/logs/xtrabackup_full.log 2>&1  
  2. 10 00 * * 1-6 /usr/local/monitor/shell/xtrabackup_incremental.sh>>/usr/local/monitor/logs/xtrabackup_incremental.log 2>&1  
每周日的00:10进行全部备份,每周1-6实现增量备份。
备份完成后还能收到邮件提醒

 

 

上面是全部备份邮件提醒

上面是增量备份邮件提醒




 本文转自 reinxu 51CTO博客,原文链接:http://blog.51cto.com/dl528888/1153207,如需转载请自行联系原作者

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
6月前
|
存储 SQL 关系型数据库
数据库的备份与恢复
数据库的备份与恢复
158 0
|
8月前
|
SQL 数据挖掘 数据库
数据库数据恢复-SQL SERVER数据库文件误还原备份的数据恢复方案
SQL SERVER数据库故障类型: 1、SQL SERVER数据库文件被删除。 2、SQL SERVER数据库所在分区格式化。 3、SQL SERVER数据库文件大小变为“0”。 4、使用备份还原数据库时覆盖原数据库。
|
存储 容灾 关系型数据库
7 PostgreSQL 物理备份和还原,逻辑备份和还原|学习笔记
快速学习7 PostgreSQL 物理备份和还原,逻辑备份和还原
987 0
7 PostgreSQL 物理备份和还原,逻辑备份和还原|学习笔记
|
SQL 关系型数据库 MySQL
mysql备份与恢复数据库|学习笔记
快速学习mysql备份与恢复数据库
|
关系型数据库 数据库 MySQL
|
Oracle 关系型数据库 数据库
|
Oracle 关系型数据库 数据库