Xtrabackup 备份大数据库(全量备份和增量备份)(2)

现在全备已经完成了,我们来模拟破坏数据,然后恢复数据。
首先停掉数据库
[root@db02 3306]# mysqladmin -uroot -pli123456 -S /data/3306/mysql.sock shutdown
[root@db02 3306]# mv /data/3306/data/ /data/3306/data_bak/
[root@db02 3306]# mkdir -p /data/3306/data/ #必须创建一个新的空data文件夹用来恢复,不然恢复会报错
Original data directory /data/3306/data is not empty!

此时我们在开启数据库,发现数据库文件被破坏无法启动了
[root@db02 3306]# mysqld_safe --defaults-file=/data/3306/my.cnf &
160710 10:05:14 mysqld_safe Logging to '/data/3306/mysql_oldboy3306.err'.
160710 10:05:14 mysqld_safe Starting mysqld daemon with databases from /data/3306/data
160710 10:05:15 mysqld_safe mysqld from pid file /data/3306/mysqld.pid ended
# pid进程文件无法启动
[1]+  Done                    mysqld_safe --defaults-file=/data/3306/my.cnf
[root@db02 3306]# tail mysql_oldboy3306.err 
160710 10:05:15 InnoDB: 5.5.32 started; log sequence number 1595668
160710 10:05:15 [Note] Recovering after a crash using /data/3306/mysql-bin
160710 10:05:15 [Note] Starting crash recovery...
160710 10:05:15 [Note] Crash recovery finished.
160710 10:05:15 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
160710 10:05:15 [Note]  - '0.0.0.0' resolves to '0.0.0.0';
160710 10:05:15 [Note] Server socket created on IP: '0.0.0.0'.
160710 10:05:15 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist #提示相关数据库表未找到
160710 10:05:15 mysqld_safe mysqld from pid file /data/3306/mysqld.pid ended

准备全量恢复
[root@db02 3306]# innobackupex --defaults-file=/data/3306/my.cnf --user=root --password=li123456 --apply-log /backup/full/2016-07-10_08-24-43/
...
InnoDB: Waiting for purge to start
InnoDB: 5.6.24 started; log sequence number 1673228
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 1673238
160710 08:42:01 completed OK!

#这里的apply-log参数意思就是在正真恢复数据库数据之前,先将数据库redo日志状态恢复

接下来才是将数据文件恢复
[root@db02 3306]# innobackupex --defaults-file=/data/3306/my.cnf --copy-back /backup/full/2016-07-10_08-24-43/
...
160710 08:43:41 [01]        ...done
160710 08:43:41 [01] Copying ./mysql/user.frm to /data/3306/data/mysql/user.frm
160710 08:43:41 [01]        ...done
160710 08:43:41 [01] Copying ./mysql/tables_priv.MYI to /data/3306/data/mysql/tables_priv.MYI
160710 08:43:41 [01]        ...done
160710 08:43:41 [01] Copying ./mysql/proxies_priv.MYI to /data/3306/data/mysql/proxies_priv.MYI
160710 08:43:41 [01]        ...done
160710 08:43:41 completed OK!

再更改数据文件属组,启动数据库成功
[root@db02 3306]# chown -R mysql.mysql data
[root@db02 data]# mysqld_safe --defaults-file=/data/3306/my.cnf &
[1] 90409
[root@db02 data]# mysql -uroot -pli123456 -S /data/3306/mysql.sock
mysql> show databases;
+--------------------+
| Database          |
+--------------------+
| information_schema |
| lichengbing        | #数据表恢复成功
| lilongzi          |
| mysql              |
| performance_schema |
+--------------------+
5 rows in set (0.00 sec)

再来模拟一下增量备份
新插入几行数据
[root@db02 opt]# mysql -uroot -pli123456 -S /data/3306/mysql.sock lichengbing < /opt/test2.sql
mysql> select * from lichengbing.test;
+----+-------+
| id | name  |
+----+-------+
|  1 | one  |
|  2 | two  |
|  3 | three |
|  4 | four  | #此时4、5、6相当于数据库增量文件
|  5 | five  |
|  6 | six  |
+----+-------+
6 rows in set (0.00 sec)

增量备份
[root@db02 opt]# innobackupex --defaults-file=/data/3306/my.cnf --user=root --password=li123456 --incremental /backup/add/ --incremental-basedir=/backup/full/2016-07-10_08-24-43/
...
160710 08:55:15 [00] Writing xtrabackup_info
160710 08:55:15 [00]        ...done
xtrabackup: Transaction log of lsn (1673693) to (1673693) was copied.
160710 08:55:15 completed OK!
[root@db02 add]# cd /backup/add/2016-07-10_08-55-12/
[root@db02 2016-07-10_08-55-12]# cat xtrabackup_checkpoints 
backup_type = incremental
from_lsn = 1672995 #增量备份的起始LSN号是靠读取全备xtrabackup_checkpoints文件得到的
to_lsn = 1673693 #增量结束LSN号
last_lsn = 1673693
compact = 0
recover_binlog_info = 0

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/722e0a3e813bf86b160a23f56f19625a.html