今天就跟大家聊聊有关mysql物理备份利器xtrabackup怎么用,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
-
安装
cd /opt/
tar -zxvf percona-xtrabackup-2.4.7-Linux-x86_64.tar.gz
ln -s /opt/percona-xtrabackup-2.4.7-Linux-x86_64 /usr/local/xtrabackup
-
Xtrabackup工具介绍
安装XtraBackup后,其实会有几个工具:
innobackupex:这个是其实是下面三个工具的一个perl脚本封装,可以备份MyISAM, InnoDB, XtraDB表。但在处理Myisam时需要加一个读锁。
xtrabackup:一个由C编译而来的二进制文件,只能备份InnoDB和XtraDB数据。
xbcrypt:用来加密或解密备份的数据。
xbstream:用来解压或压缩xbstream格式的压缩文件。
-
innobackupex使用方法
完整的选项使用请执行innobackupex –help,这里只介绍使用常用的选项进行完整备份及增量备份和还原。
–defaults-file 数据库的配置文件路径
–apply-log 在备份的基础上应用日志,把数据处理crash状态的不一致通过apply-log修复成一致的状态。
–copy-back 从备份目录拷贝数据,索引,日志到my.cnf文件里规定的初始位置。
–no-timestamp 创建备份时不自动生成时间目录,可以自定义备份目录名
–databases 用于指定要备份的数据库, 多个库文件使用方法: “database1 database2″
–incremental 在全备份的基础上进行增量备份,后跟增量备份存贮目录路径
–incremental-basedir=DIRECTORY 增量备份所需要的全备份路径目录或上次做增量备份的目录路径
–incremental-dir=DIRECTORY 增量备份存贮的目录路径
–redo-only 用于准备增量备份内容把数据合并到全备份目录,配合–incremental-dir 增量备份目录使用。
–force-non-empty-directories 如果是特定库备份还原,不需要删掉整个mysql目录,只是特定库的及相关文件就可以,还原时加上此参数就不会报错。
-
全量备份与恢复
全量备份
#mkdir -p /data/mysql_backup/full_backup/
#/usr/local/xtrabackup/bin/innobackupex –defaults-file=/data/mysql/mysql3306/my3306.cnf –user=root –password=root /data/mysql_backup/full_backup/
#[root@mytest7 2017-06-19_17-15-48]# pwd
/data/mysql_backup/full_backup/2017-06-19_17-15-48
[root@mytest7 2017-06-19_17-15-48]# ll
total 1048628
-rw-r—– 1 root root 433 Jun 19 17:16 backup-my.cnf
-rw-r—– 1 root root 584 Jun 19 17:16 ib_buffer_pool
-rw-r—– 1 root root 1073741824 Jun 19 17:16 ibdata1
drwxr-x— 2 root root 4096 Jun 19 17:16 mysql
drwxr-x— 2 root root 89 Jun 19 17:16 percona
drwxr-x— 2 root root 8192 Jun 19 17:16 performance_schema
drwxr-x— 2 root root 129 Jun 19 17:16 reptest
drwxr-x— 2 root root 8192 Jun 19 17:16 sys
-rw-r—– 1 root root 66 Jun 19 17:16 xtrabackup_binlog_info
-rw-r—– 1 root root 113 Jun 19 17:16 xtrabackup_checkpoints
-rw-r—– 1 root root 611 Jun 19 17:16 xtrabackup_info
-rw-r—– 1 root root 2560 Jun 19 17:16 xtrabackup_logfile
全量恢复
#kill -9 `pidof mysqld`
#mv /data/mysql/mysql3306/data /data/mysql/mysql3306/data_bak
#mkdir -p /data/mysql/mysql3306/data
#/usr/local/xtrabackup/bin/innobackupex –apply-log /data/mysql_backup/full_backup/2017-06-19_17-15-48 –user=root –password=root
#/usr/local/xtrabackup/bin/innobackupex –defaults-file=/data/mysql/mysql3306/my3306.cnf –copy-back /data/mysql_backup/full_backup/2017-06-19_17-15-48/
#chown -R mysql.mysql /data/mysql/mysql3306/data/
#/usr/local/mysql/bin/mysqld –defaults-file=/data/mysql/mysql3306/my3306.cnf &
压缩备份
#/usr/local/xtrabackup/bin/innobackupex –defaults-file=/data/mysql/mysql3306/my3306.cnf –user=root –password=root –no-timestamp –stream=tar ./ | gzip > /data/mysql_backup/full_backup/db_$(date +%F).tar.gz
–压缩备份应用时需先解压,才能应用,使用方法和一般备份同样使用
#mkdir -p /data/mysql_backup/full_backup/db_$(date +%F)
#tar -zxf /data/mysql_backup/full_backup/db_$(date +%F).tar.gz -C /data/mysql_backup/full_backup/db_$(date +%F)
单库备份没有太大意义,xtrabackup恢复需要保证datadir为空,可以备份单表或者多表,恢复的原理和innodb表空间传输很像
-
增量备份与恢复
–先进行全备
#/usr/local/xtrabackup/bin/innobackupex –defaults-file=/data/mysql/mysql3306/my3306.cnf –user=root –password=root /data/mysql_backup/full_backup/
# ll /data/mysql_backup/full_backup/2017-06-20_15-21-57
total 1048628
-rw-r—– 1 root root 433 Jun 20 15:22 backup-my.cnf
-rw-r—– 1 root root 584 Jun 20 15:22 ib_buffer_pool
-rw-r—– 1 root root 1073741824 Jun 20 15:22 ibdata1
drwxr-x— 2 root root 4096 Jun 20 15:22 mysql
drwxr-x— 2 root root 89 Jun 20 15:22 percona
drwxr-x— 2 root root 8192 Jun 20 15:22 performance_schema
drwxr-x— 2 root root 129 Jun 20 15:22 reptest
drwxr-x— 2 root root 8192 Jun 20 15:22 sys
-rw-r—– 1 root root 64 Jun 20 15:22 xtrabackup_binlog_info
-rw-r—– 1 root root 113 Jun 20 15:22 xtrabackup_checkpoints
-rw-r—– 1 root root 609 Jun 20 15:22 xtrabackup_info
-rw-r—– 1 root root 2560 Jun 20 15:22 xtrabackup_logfile
# mysql -uroot -proot -S /data/mysql/mysql3306/tmp/mysql3306.sock
mysql> select * from reptest.t1;
+—-+——+
| id | name |
+—-+——+
| 1 | yyyy |
| 2 | yyyy |
| 3 | cc |
| 4 | yyyy |
| 5 | yyyy |
+—-+——+
5 rows in set (0.05 sec)
mysql> insert into reptest.t1 values (11,'zz');
Query OK, 1 row affected (0.12 sec)
–第一次增量备份
# mkdir /data/mysql_backup/incre_backup
#/usr/local/xtrabackup/bin/innobackupex –incremental /data/mysql_backup/incre_backup –incremental-basedir=/data/mysql_backup/full_backup/2017-06-20_15-21-57 –user=root –password=root –socket=/data/mysql/mysql3306/tmp/mysql3306.sock
#ll /data/mysql_backup/incre_backup/2017-06-20_15-46-16
total 108
-rw-r—– 1 root root 427 Jun 20 15:46 backup-my.cnf
-rw-r—– 1 root root 584 Jun 20 15:46 ib_buffer_pool
-rw-r—– 1 root root 49152 Jun 20 15:46 ibdata1.delta
-rw-r—– 1 root root 44 Jun 20 15:46 ibdata1.meta
drwxr-x— 2 root root 4096 Jun 20 15:46 mysql
drwxr-x— 2 root root 146 Jun 20 15:46 percona
drwxr-x— 2 root root 8192 Jun 20 15:46 performance_schema
drwxr-x— 2 root root 4096 Jun 20 15:46 reptest
drwxr-x— 2 root root 8192 Jun 20 15:46 sys
-rw-r—– 1 root root 104 Jun 20 15:46 xtrabackup_binlog_info
-rw-r—– 1 root root 117 Jun 20 15:46 xtrabackup_checkpoints
-rw-r—– 1 root root 743 Jun 20 15:46 xtrabackup_info
-rw-r—– 1 root root 2560 Jun 20 15:46 xtrabackup_logfile
mysql> insert into reptest.t1 values (12,'ww');
Query OK, 1 row affected (0.00 sec)
mysql> select * from reptest.t1;
+—-+——+
| id | name |
+—-+——+
| 1 | yyyy |
| 2 | yyyy |
| 3 | cc |
| 4 | yyyy |
| 5 | yyyy |
| 11 | zz |
| 12 | ww |
+—-+——+
7 rows in set (0.00 sec)
–第二次增量备份(–incremental-basedir可以是上次全量备份文件,也可以是第一次增量备份文件)
#/usr/local/xtrabackup/bin/innobackupex –incremental /data/mysql_backup/incre_backup –incremental-basedir=/data/mysql_backup/full_backup/2017-06-20_15-21-57 –user=root –password=root –socket=/data/mysql/mysql3306/tmp/mysql3306.sock
#ll /data/mysql_backup/incre_backup/2017-06-20_15-51-49
total 188
-rw-r—– 1 root root 427 Jun 20 15:52 backup-my.cnf
-rw-r—– 1 root root 584 Jun 20 15:52 ib_buffer_pool
-rw-r—– 1 root root 131072 Jun 20 15:51 ibdata1.delta
-rw-r—– 1 root root 44 Jun 20 15:51 ibdata1.meta
drwxr-x— 2 root root 4096 Jun 20 15:51 mysql
drwxr-x— 2 root root 146 Jun 20 15:52 percona
drwxr-x— 2 root root 8192 Jun 20 15:52 performance_schema
drwxr-x— 2 root root 4096 Jun 20 15:52 reptest
drwxr-x— 2 root root 8192 Jun 20 15:52 sys
-rw-r—– 1 root root 106 Jun 20 15:52 xtrabackup_binlog_info
-rw-r—– 1 root root 117 Jun 20 15:52 xtrabackup_checkpoints
-rw-r—– 1 root root 745 Jun 20 15:52 xtrabackup_info
-rw-r—– 1 root root 2560 Jun 20 15:52 xtrabackup_logfile
–恢复到第一次增量备份
#kill -9 `pidof mysqld`
#mv /data/mysql/mysql3306/data /data/mysql/mysql3306/data_bak
#mkdir -p /data/mysql/mysql3306/data
–应用全备
#/usr/local/xtrabackup/bin/innobackupex –apply-log –redo-only /data/mysql_backup/full_backup/2017-06-20_15-21-57 –user=root –password=root
–合并第一次增量备份
#/usr/local/xtrabackup/bin/innobackupex –apply-log –redo-only /data/mysql_backup/full_backup/2017-06-20_15-21-57 –incremental-dir=/data/mysql_backup/incre_backup/2017-06-20_15-46-16 –user=root –password=root
–应用日志(与全量恢复一样)
#/usr/local/xtrabackup/bin/innobackupex –apply-log /data/mysql_backup/full_backup/2017-06-20_15-21-57 –user=root –password=root
–恢复数据(与全量恢复一样)
#/usr/local/xtrabackup/bin/innobackupex –defaults-file=/data/mysql/mysql3306/my3306.cnf –copy-back /data/mysql_backup/full_backup/2017-06-20_15-21-57
#chown -R mysql.mysql /data/mysql/mysql3306/data/
#/usr/local/mysql/bin/mysqld –defaults-file=/data/mysql/mysql3306/my3306.cnf &
# mysql -uroot -proot -S /data/mysql/mysql3306/tmp/mysql3306.sock
mysql> select * from reptest.t1;
+—-+——+
| id | name |
+—-+——+
| 1 | yyyy |
| 2 | yyyy |
| 3 | cc |
| 4 | yyyy |
| 5 | yyyy |
| 11 | zz |
+—-+——+
6 rows in set (0.00 sec)
此时的数据恢复到了第一次备份的时候
–恢复到第二次增量备份
#kill -9 `pidof mysqld`
#mv /data/mysql/mysql3306/data /data/mysql/mysql3306/data_bak2
#mkdir -p /data/mysql/mysql3306/data
–应用全备
#/usr/local/xtrabackup/bin/innobackupex –apply-log –redo-only /data/mysql_backup/full_backup/2017-06-20_15-21-57 –user=root –password=root
–合并第一次增量备份
#/usr/local/xtrabackup/bin/innobackupex –apply-log –redo-only /data/mysql_backup/full_backup/2017-06-20_15-21-57 –incremental-dir=/data/mysql_backup/incre_backup/2017-06-20_15-46-16 –user=root –password=root
–合并第二次增量备份
#/usr/local/xtrabackup/bin/innobackupex –apply-log –redo-only /data/mysql_backup/full_backup/2017-06-20_15-21-57 –incremental-dir=/data/mysql_backup/incre_backup/2017-06-20_15-51-49 –user=root –password=root
–应用日志(与全量恢复一样)
#/usr/local/xtrabackup/bin/innobackupex –apply-log /data/mysql_backup/full_backup/2017-06-20_15-21-57 –user=root –password=root
–恢复数据(与全量恢复一样)
#/usr/local/xtrabackup/bin/innobackupex –defaults-file=/data/mysql/mysql3306/my3306.cnf –copy-back /data/mysql_backup/full_backup/2017-06-20_15-21-57
#chown -R mysql.mysql /data/mysql/mysql3306/data/
#/usr/local/mysql/bin/mysqld –defaults-file=/data/mysql/mysql3306/my3306.cnf &
多合并一次增量备份即可
建议使用xtrabackup全量备份+binlog日志备份指定备份方案,增量备份可能需要应用多个文件,容易出错,
并且只能恢复到全量备份点和增量备份点,无法恢复到中间点,而现实生产中,多数在中间点。
看完上述内容,你们对mysql物理备份利器xtrabackup怎么用有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。
原创文章,作者:kepupublish,如若转载,请注明出处:https://blog.ytso.com/204236.html