具体关于xtrabackup的备份与恢复,可以参考以下文章
生产环境究竟是使用mysqldump还是xtrabackup来备份与恢复数据库?
http://dl528888.blog.51cto.com/2382721/1153204
下面是一个生产环境的全部备份恢复案例
主要是备份我之前做的php+mysql+shell的monitor数据库,这个数据库的数据文件4.2G
- 4.2G ibdata1
- 5.1M ib_logfile0
- 5.1M ib_logfile1
- 1.8M monitor
之前一直使用mysqldump备份,但恢复的时候非常的慢,3个小时都恢复不了,所以放弃了使用mysqldump来备份与恢复数据库的方案,改用xtrabackup来备份与恢复。
备份与恢复上面都描述了,下面就列出我恢复4.2G的数据库使用的时间
恢复的日志是
- start time is 20130313-13:34:35
- start to stop mysql server
- start to restore database
- InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy
- and Percona Ireland Ltd 2009-2012. All Rights Reserved.
- This software is published under
- the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.
- IMPORTANT: Please check that the apply-log run completes successfully.
- At the end of a successful apply-log run innobackupex
- prints "completed OK!".
- 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
- xtrabackup_51 version 2.0.5 for MySQL server 5.1.59 unknown-linux-gnu (x86_64) (revision id: undefined)
- xtrabackup: cd to /usr/local/monitor/data/2013-03-13_13-28-28
- xtrabackup: This target seems to be not prepared yet.
- xtrabackup: xtrabackup_logfile detected: size=2097152, start_lsn=(2 31133758)
- xtrabackup: Temporary instance for recovery is set as followings.
- xtrabackup: innodb_data_home_dir = ./
- xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
- xtrabackup: innodb_log_group_home_dir = ./
- xtrabackup: innodb_log_files_in_group = 1
- xtrabackup: innodb_log_file_size = 2097152
- xtrabackup: Temporary instance for recovery is set as followings.
- xtrabackup: innodb_data_home_dir = ./
- xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
- xtrabackup: innodb_log_group_home_dir = ./
- xtrabackup: innodb_log_files_in_group = 1
- xtrabackup: innodb_log_file_size = 2097152
- xtrabackup: Starting InnoDB instance for recovery.
- xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)
- InnoDB: The InnoDB memory heap is disabled
- 130313 13:34:35 InnoDB: Initializing buffer pool, size = 100.0M
- 130313 13:34:35 InnoDB: Completed initialization of buffer pool
- InnoDB: Log scan progressed past the checkpoint lsn 2 31133758
- 130313 13:34:35 InnoDB: Database was not shut down normally!
- InnoDB: Starting crash recovery.
- InnoDB: Reading tablespace information from the .ibd files...
- InnoDB: Doing recovery: scanned up to log sequence number 2 31272048 (7 %)
- 130313 13:34:36 InnoDB: Starting an apply batch of log records to the database...
- 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
- InnoDB: Apply batch completed
- 130313 13:34:37 InnoDB: Started; log sequence number 2 31272048
- [notice (again)]
- If you use binary log and don't use any hack of group commit,
- the binary log position seems to be:
- xtrabackup: starting shutdown with innodb_fast_shutdown = 1
- 130313 13:34:37 InnoDB: Starting shutdown...
- 130313 13:34:42 InnoDB: Shutdown completed; log sequence number 2 31272048
- 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
- for creating ib_logfile*
- xtrabackup_51 version 2.0.5 for MySQL server 5.1.59 unknown-linux-gnu (x86_64) (revision id: undefined)
- xtrabackup: cd to /usr/local/monitor/data/2013-03-13_13-28-28
- xtrabackup: This target seems to be already prepared.
- xtrabackup: notice: xtrabackup_logfile was already used to '--prepare'.
- xtrabackup: Temporary instance for recovery is set as followings.
- xtrabackup: innodb_data_home_dir = ./
- xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
- xtrabackup: innodb_log_group_home_dir = ./
- xtrabackup: innodb_log_files_in_group = 2
- xtrabackup: innodb_log_file_size = 5242880
- xtrabackup: Temporary instance for recovery is set as followings.
- xtrabackup: innodb_data_home_dir = ./
- xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
- xtrabackup: innodb_log_group_home_dir = ./
- xtrabackup: innodb_log_files_in_group = 2
- xtrabackup: innodb_log_file_size = 5242880
- xtrabackup: Starting InnoDB instance for recovery.
- xtrabackup: Using 104857600 bytes for buffer pool (set by --use-memory parameter)
- InnoDB: The InnoDB memory heap is disabled
- 130313 13:34:42 InnoDB: Initializing buffer pool, size = 100.0M
- 130313 13:34:42 InnoDB: Completed initialization of buffer pool
- 130313 13:34:42 InnoDB: Log file ./ib_logfile0 did not exist: new to be created
- InnoDB: Setting log file ./ib_logfile0 size to 5 MB
- InnoDB: Database physically writes the file full: wait...
- 130313 13:34:42 InnoDB: Log file ./ib_logfile1 did not exist: new to be created
- InnoDB: Setting log file ./ib_logfile1 size to 5 MB
- InnoDB: Database physically writes the file full: wait...
- InnoDB: The log sequence number in ibdata files does not match
- InnoDB: the log sequence number in the ib_logfiles!
- 130313 13:34:42 InnoDB: Database was not shut down normally!
- InnoDB: Starting crash recovery.
- InnoDB: Reading tablespace information from the .ibd files...
- 130313 13:34:42 InnoDB: Started; log sequence number 2 31272460
- [notice (again)]
- If you use binary log and don't use any hack of group commit,
- the binary log position seems to be:
- xtrabackup: starting shutdown with innodb_fast_shutdown = 1
- 130313 13:34:42 InnoDB: Starting shutdown...
- 130313 13:34:48 InnoDB: Shutdown completed; log sequence number 2 31272460
- 130313 13:34:48 innobackupex: completed OK!
- operation is success!
- operation is success!
- start to start mysql server
- Starting MySQL: [ OK ]
- operation is success!
- finish time is 20130313-13:34:49
可以看到仅用了14秒就恢复成功。
所以我现在都使用xtrabackup来备份与恢复数据库,下面是我的全部备份与增量备份的脚本
全部备份的脚本xtrabackup_full.sh
- [root@beiyong shell]# cat xtrabackup_full.sh
- #!/bin/bash
- local_ip="$(/sbin/ifconfig eth0|grep 'inet addr'|awk -F : '{print $2}'|cut -d ' ' -f1)"
- email='[email protected]'
- user='root'
- passwd='123456'
- database='monitor'
- my_config='/etc/my.cnf'
- log=$database-$(date +%Y%m%d%H%M).log
- str=$database-$(date +%Y%m%d%H%M).tar.gz
- backup_dir='/usr/local/monitor/data'
- echo "Start to backup at $(date +%Y%m%d%H%M)"
- if [ ! -d "$backup_dir" ];then
- mkdir -p $backup_dir
- fi
- #innobackupex --user=$user --password=$passwd --defaults-file=$my_config --database=$database --stream=tar $backup_dir 2>$backup_dir/$log | gzip 1>$backup_dir/$str
- innobackupex --user=$user --password=$passwd --defaults-file=$my_config --database=$database $backup_dir
- if [ $? -eq 0 ];then
- echo "Backup is finish! at $(date +%Y%m%d%H%M)"
- 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
- exit 0
- else
- echo "Backup is Fail! at $(date +%Y%m%d%H%M)"
- 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
- exit 1
- fi
- echo "Backup Process Done"
增量备份的脚本xtrabackup_incremental.sh
- [root@beiyong shell]# cat xtrabackup_incremental.sh
- #!/bin/bash
- local_ip="$(/sbin/ifconfig eth0|grep 'inet addr'|awk -F : '{print $2}'|cut -d ' ' -f1)"
- email='[email protected]'
- user='root'
- passwd='123456'
- database='monitor'
- my_config='/etc/my.cnf'
- log=$database-$(date +%Y%m%d%H%M).log
- str=$database-$(date +%Y%m%d%H%M).tar.gz
- backup_dir='/usr/local/monitor/data/'
- last_day=$(date -d "1 days ago" +%Y-%m-%d)
- today=$(date +%Y%m%d)
- filename=$(find $backup_dir -name "$last_day*" -print|awk -F / '{print $NF}')
- echo "Start to backup at $(date +%Y%m%d%H%M)"
- if [ ! -d "$backup_dir" ];then
- mkdir -p $backup_dir
- fi
- #innobackupex --user=$user --password=$passwd --defaults-file=$my_config --database=$database --stream=tar $backup_dir 2>$backup_dir/$log | gzip 1>$backup_dir/$str
- innobackupex --user=$user --password=$passwd --defaults-file=$my_config --database=$database --incremental --incremental-basedir=$backup_dir/$filename $backup_dir
- if [ $? -eq 0 ];then
- echo "Backup is finish! at $(date +%Y%m%d%H%M)"
- 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
- exit 0
- else
- echo "Backup is Fail! at $(date +%Y%m%d%H%M)"
- 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
- exit 1
- fi
- echo "Backup Process Done"
结合crontab来实现自动的备份
- 10 00 * * 0 /usr/local/monitor/shell/xtrabackup_full.sh>> /usr/local/monitor/logs/xtrabackup_full.log 2>&1
- 10 00 * * 1-6 /usr/local/monitor/shell/xtrabackup_incremental.sh>>/usr/local/monitor/logs/xtrabackup_incremental.log 2>&1
每周日的00:10进行全部备份,每周1-6实现增量备份。
备份完成后还能收到邮件提醒
上面是全部备份邮件提醒
上面是增量备份邮件提醒