备份与恢复
数据备份就是将数据以某种方式加以保留,以便在系统需要时重新恢复和利用。其作用主要体现在如下两个二方面:
在数据遭到意外事件破坏时,通过数据恢复还原数据
数据备份是历史数据保存归档的最佳方式
数据恢复就是把遭到破坏、删除和修改的数据还原为可使用的数据的过程
为什么要备份数据?
在生产环境中我们数据库可能会遭遇各种各样的不测从而导致数据丢失, 大概分为以下几种.
硬件故障
软件故障
自然灾害
黑客攻击
误操作
所以, 为了在数据丢失之后能够恢复数据, 我们就需要定期的备份数据, 备份数据的策略要根据不同的应用场景进行定制, 大致有几个参考数值, 我们可以根据这些数值从而定制符合特定环境中的数据备份策略
能够容忍丢失多少数据
恢复数据需要多长时间
需要恢复哪一些数据
数据备份类型
分为以下两种:
完全备份
部分备份
完全备份是指将整个数据集即整个数据库备份
而部分备份又分以下两种:
增量备份
差异备份
增量备份指的是备份自上一次备份以来(增量或完全)以来变化的数据; 特点: 节约空间、还原麻烦
差异备份指的是备份自上一次完全备份以来变化的数据 特点: 浪费空间、还原比增量备份简单
MySQL的备份方式
根据数据服务是否在线:
热备:读写操作均可进行的状态下所做的备份;
温备:可读但不可写状态下进行的备份;
冷备:读写操作均不可进行的状态下所做的备份;
不同方式的备份还要考虑所使用的存储引擎
MyISAM:只支持温备、不能热备
InnoDB:支持热备
备份工具
mysqldump
: 逻辑备份工具, 适用于所有的存储引擎, 支持温备、完全备份、部分备份、对于InnoDB存储引擎支持热备cp, tar
等归档复制工具: 物理备份工具, 适用于所有的存储引擎, 冷备、完全备份、部分备份lvm2 snapshot
: 几乎热备, 借助文件系统管理工具进行备份xtrabackup
: 一款非常强大的InnoDB/XtraDB热备工具, 支持完全备份、增量备份, 由percona提供
备份恢复演练
使用cp备份
这里我们使用MariaDB 5.5版本进行演练
查看数据库中的数据
MariaDB [(none)]> SHOW DATABASES; #查看数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> USE hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [hellodb]> SHOW TABLES; #查看数据库中的表信息
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
MariaDB [hellodb]>
向数据库施加读锁
MariaDB [hellodb]> FLUSH TABLE WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
MariaDB [hellodb]>
备份数据文件
[root@admin1 ~]# mkdir /backup #建立备份目录
[root@admin1 ~]# cp -a /var/lib/mysql/* /backup/ #备份,保留原属性信息
[root@admin1 ~]# ls /backup/ #查看备份文件
aria_log.00000001 hellodb ib_logfile0 master-bin.000001 master-bin.000003 mysql performance_schema
aria_log_control ibdata1 ib_logfile1 master-bin.000002 master-bin.index mysql.sock
[root@admin1 ~]#
模拟数据丢失并恢复
[root@admin1 ~]# rm -rf /var/lib/mysql/* #模拟数据损坏
[root@admin1 ~]# systemctl restart mariadb.service #重启服务,将初始化数据库
MariaDB [(none)]> SHOW DATABASES; #查看数据库,数据已丢失
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
#恢复数据
[root@admin1 ~]# rm -rf /var/lib/mysql/* #删除现有数据目录
[root@admin1 ~]# cp -a /backup/* /var/lib/mysql/ #还原数据
[root@admin1 ~]# systemctl restart mariadb.service #重启服务
#验证
MariaDB [(none)]> SHOW DATABASES; #数据已恢复
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> USE hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [hellodb]> SHOW TABLES;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
MariaDB [hellodb]>
mysqldummp+复制binlog
mysqldump工具介绍
是一个客户端的逻辑备份工具, 可以生成一个重现创建原始数据库和表的SQL语句, 可以支持所有的存储引擎, 对于InnoDB支持热备
#基本使用语法
mysqldump [OPTIONS] db_name [tables] #只备份指定数据库当中的表,恢复需要手动CREATE DATABASE
mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...] #恢复不需要手动创建数据库
mysqldump [OPTIONS] --all-databases [OPTIONS] #备份所有数据库
MyISAM存储引擎:支持温备,备份时要锁定表;
-x, --lock-all-tables:锁定所有库的所有表,读锁;
-l, --lock-tables:锁定指定库所有表;
InnoDB存储引擎:支持温备和热备;
--single-transaction:创建一个事务,基于此快照执行备份;
其它选项:
-A, --all-databases:备份所有库
-B, --databases DB1 DB2 ...:备份指定的库
-E, --events:备份指定数据库相关的所有event scheduler
-R, --routines:备份指定数据库相关的所有存储过程和存储函数
--triggers:备份表相关的触发器
--master-data[=#]:记录备份时二进制日志文件所处的position
1:记录为CHANGE MASTER TO语句,此语句不被注释;
2:记录为CHANGE MASTER TO语句,此语句被注释;
--flush-logs:锁定表完成后,即进行日志刷新操作;
查看数据库信息
MariaDB [hellodb]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [hellodb]>
MariaDB [hellodb]> USE hellodb;
Database changed
MariaDB [hellodb]> SHOW TABLES;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
MariaDB [hellodb]>
mysqldump备份并恢复数据
[root@admin1 ~]# mysql -uroot -pmageedu -e 'SHOW MASTER STATUS;' #查看当前二进制文件的状态, 并记录下position的数字
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000004 | 245 | | |
+-------------------+----------+--------------+------------------+
[root@admin1 ~]# mysqldump -uroot -pmageedu --all-databases --lock-all-tables > all.sql #备份数据库到all.sql文件中
MariaDB [(none)]> CREATE DATABASE test1; #创建一个数据库
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> USE test1;
Database changed
MariaDB [test1]> CREATE TABLE IF NOT EXISTS students(id SMALLINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(10)); #创建表
Query OK, 0 rows affected (0.03 sec)
MariaDB [test1]> INSERT INTO students VALUES (1,'Tom'),(2,'Jerry'); #插入数据
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [test1]> SELECT * FROM students; #查看数据
+----+-------+
| id | name |
+----+-------+
| 1 | Tom |
| 2 | Jerry |
+----+-------+
2 rows in set (0.00 sec)
MariaDB [test1]> SHOW MASTER STATUS; #记录现在的position
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000004 | 785 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
[root@admin1 ~]# cp /var/lib/mysql/master-bin.000004 /backup/binlog/ #备份二进制日志
[root@admin1 ~]# systemctl stop mariadb.service #停止服务
[root@admin1 ~]# rm -rf /var/lib/mysql/* #删除所有数据文件
[root@admin1 ~]# systemctl start mariadb.service #启动MySQL,重新初始化数据库
MariaDB [(none)]> SHOW DATABASES; #查看数据库,数据已丢失
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> SET sql_log_bin=OFF; #临时关闭二进制日志
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> source /root/all.sql #恢复数据
MariaDB [mysql]> SET sql_log_bin=ON; #开启二进制日志
Query OK, 0 rows affected (0.00 sec)
[root@admin1 ~]# mysqlbinlog --start-position=245 --stop-position=785 /backup/binlog/master-bin.000004 | mysql #通过二进制日志增量恢复
MariaDB [(none)]> SHOW DATABASES; #test1数据库已经有了
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
| test1 |
+--------------------+
6 rows in set (0.00 sec)
MariaDB [(none)]> USE test1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [test1]> SHOW TABLES; #查看表
+-----------------+
| Tables_in_test1 |
+-----------------+
| students |
+-----------------+
1 row in set (0.00 sec)
MariaDB [test1]> SELECT * FROM students; #数据已经恢复
+----+-------+
| id | name |
+----+-------+
| 1 | Tom |
| 2 | Jerry |
+----+-------+
2 rows in set (0.00 sec)
lvm2快照备份数据
LVM
快照简单来说就是将所快照源分区一个时间点所有文件的元数据进行保存,如果源文件没有改变,那么访问快照卷的相应文件则直接指向源分区的源文件,如果源文件发生改变,则快照卷中与之对应的文件不会发生改变。快照卷主要用于辅助备份文件
创建快照卷
添加硬盘/dev/sdb,使用命令直接手动上识别新添加的硬盘,无需重启
[root@admin1 ~]# echo "- - -" > /sys/class/scsi_host/host0/scan
[root@admin1 ~]# echo "- - -" > /sys/class/scsi_host/host1/scan
[root@admin1 ~]# echo "- - -" > /sys/class/scsi_host/host2/scan
[root@admin1 ~]# ls /dev/sd*
/dev/sda /dev/sda1 /dev/sda2 /dev/sdb
#分区
[root@admin1 ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x432cc07d.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G
Partition 1 of type Linux and of size 10 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@admin1 ~]# partx -a /dev/sdb
partx: /dev/sdb: error adding partition 1
#创建逻辑卷
[root@admin1 ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created
[root@admin1 ~]# vgcreate myvg /dev/sdb1
Volume group "myvg" successfully created
[root@admin1 ~]# lvcreate -n mydata -L 5G myvg
Logical volume "mydata" created.
[root@admin1 ~]# mkfs.ext4 /dev/mapper/myvg-mydata #格式化
[root@admin1 ~]# mkdir /lvm_data #创建数据目录
[root@admin1 ~]# mount /dev/mapper/myvg-mydata /lvm_data/ #挂载
[root@admin1 ~]# vim /etc/my.cnf #编辑配置文件,修改数据目录
datadir = /lvm_data
[root@admin1 ~]# chown -R mysql.mysql /lvm_data/ #修改数据目录属主属组
[root@admin1 ~]# systemctl restart mariadb #重启服务
#重新导入hellodb.sql数据库文件
[root@admin1 ~]# mysql < hellodb.sql
查看数据库信息
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> USE hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [hellodb]> SHOW TABLES;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
创建快照卷并备份
MariaDB [hellodb]> FLUSH TABLE WITH READ LOCK; #锁定所有表
Query OK, 0 rows affected (0.00 sec)
[root@admin1 ~]# lvcreate -L 2G -n mydata-snap -p r -s /dev/mapper/myvg-mydata #创建快照卷
Logical volume "mydata-snap" created.
MariaDB [(none)]> UNLOCK TABLES; #解锁
Query OK, 0 rows affected (0.00 sec)
[root@admin1 ~]# mkdir /lvm_snap #创建文件夹
[root@admin1 ~]# mount /dev/mapper/myvg-mydata--snap /lvm_snap/ #挂载快照卷
mount: /dev/mapper/myvg-mydata--snap is write-protected, mounting read-only
[root@admin1 ~]# cd /lvm_snap/
[root@admin1 lvm_snap]# ls #查看数据文件
aria_log.00000001 hellodb ib_logfile0 master-bin.000001 master-bin.000003 mysql test
aria_log_control ibdata1 ib_logfile1 master-bin.000002 master-bin.index performance_schema
[root@admin1 lvm_snap]# tar cf /backup/mysqldata.tar * #打包归档
[root@admin1 ~]# umount /lvm_snap/ #卸载快照卷
[root@admin1 ~]# lvremove myvg mydata-snap #删除快照卷
Do you really want to remove active logical volume mydata-snap? [y/n]: y
Logical volume "mydata-snap" successfully removed
Logical volume myvg/mydata contains a filesystem in use.
Volume group "mydata-snap" not found
Cannot process volume group mydata-snap
恢复数据
[root@admin1 ~]# rm -rf /lvm_data/* #模拟数据损坏
[root@admin1 ~]# systemctl start mariadb.service #重启服务
MariaDB [(none)]> SHOW DATABASES; #数据丢失
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
[root@admin1 ~]# cd /lvm_data/
[root@admin1 lvm_data]# rm -rf * #删除所有数据文件
[root@admin1 lvm_data]# tar xf /backup/mysqldata.tar #将备份的数据解压至数据目录
[root@admin1 lvm_data]# ls #查看数据文件
aria_log.00000001 hellodb ib_logfile0 master-bin.000001 master-bin.000003 mysql test
aria_log_control ibdata1 ib_logfile1 master-bin.000002 master-bin.index performance_schema
[root@admin1 lvm_data]#
MariaDB [(none)]> SHOW DATABASES; #数据已恢复
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> USE hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [hellodb]> SHOW TABLES; #数据已恢复
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
使用Xtrabackup备份
xtrabakcup
介绍
Xtrabackup是由percona提供的mysql数据库备份工具,据官方介绍,这也是世界上惟一一款开源的能够对innodb和xtradb数据库进行热备的工具。特点:
物理备份
备份速度快,可靠
备份完成后自动校验
还原速度快
能够基于压缩等功能节约磁盘空间和流量
xtrabackup
实现完全备份
我们使用
xtrabackup
的前端工具innobackupex
来实现对数据完全备份使用innobackupex备份时, 会调用xtrabackup备份所有的
InnoDB
表, 复制所有关于表结构定义的相关文件(.frm)、以及MyISAM、MERGE、CSV
和ARCHIVE
表的相关文件, 同时还会备份触发器和数据库配置文件信息相关的文件, 这些文件会被保存至一个以时间命名的目录
备份
#查看数据文件
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.01 sec)
MariaDB [(none)]> USE hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [hellodb]> SHOW TABLES;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.00 sec)
#备份数据
[root@admin1 ~]# mkdir /backups #建立备份目录
[root@admin1 ~]# innobackupex --user=root /backups/ #备份
####提示completed OK!表示成功####
[root@admin1 ~]# ls /backups/ #查看备份文件
2016-11-24_09-58-25
一般情况, 备份完成后, 数据不能用于恢复操作, 因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。因此, 此时的数据文件仍不一致, 所以我们需要”准备”一个完全备份
[root@admin1 ~]# innobackupex --apply-log /backups/2016-11-24_09-58-25/
####提示completed OK!表示成功####
[root@admin1 ~]# ls /backups/2016-11-24_09-58-25/ #查看备份数据文件
backup-my.cnf ibdata1 ib_logfile1 performance_schema xtrabackup_binlog_info xtrabackup_checkpoints xtrabackup_logfile
hellodb ib_logfile0 mysql test xtrabackup_binlog_pos_innodb xtrabackup_info
[root@admin1 ~]#
恢复数据
[root@admin1 mysql]# systemctl stop mariadb.service #停止MySQL服务
[root@admin1 mysql]# rm -rf /var/lib/mysql/* #删除数据文件
[root@admin1 mysql]# innobackupex --copy-back /backups/2016-11-24_09-58-25/ #还原
####提示completed OK!表示成功####
[root@admin1 mysql]# chown -R mysql.mysql /var/lib/mysql/* #修改数据目录文件属组属主
[root@admin1 mysql]# vim /etc/my.cnf #编辑配置文件添加此项
innodb_log_file_size = 50331648 #值设定为事务日志文件的大小
[root@admin1 mysql]# systemctl start mariadb #启动MySQL
MariaDB [(none)]> SHOW DATABASES; #数据已恢复
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> USE hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [hellodb]> SHOW TABLES;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.01 sec)
完全+增量+binglog
查看数据库信息
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> USE hellodb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [hellodb]> SHOW TABLES;
+-------------------+
| Tables_in_hellodb |
+-------------------+
| classes |
| coc |
| courses |
| scores |
| students |
| teachers |
| toc |
+-------------------+
7 rows in set (0.01 sec)
完全备份
[root@admin1 ~]# mkdir /backups #创建备份目录
[root@admin1 ~]# innobackupex --user=root --password= /backups/ #完全备份
####提示completed OK!表示备份成功####
#备份完成之后,我们再新创建一个数据库,而后做增量备份
MariaDB [(none)]> CREATE DATABASE zhai; #创建新的数据库
Query OK, 1 row affected (0.00 sec)
MariaDB [hellodb]> USE zhai;
Database changed
MariaDB [zhai]> CREATE TABLE IF NOT EXISTS students(id SMALLINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(10)); #创建表
Query OK, 0 rows affected (0.03 sec)
MariaDB [zhai]> INSERT INTO students VALUES (1,'xiaozhai'),(2,'xiaoxiao'); #插入数据
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [zhai]> SELECT * FROM students;
+----+----------+
| id | name |
+----+----------+
| 1 | xiaozhai |
| 2 | xiaoxiao |
+----+----------+
2 rows in set (0.01 sec)
MariaDB [zhai]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
| zhai |
+--------------------+
6 rows in set (0.01 sec)
增量备份
[root@admin1 ~]# cd /backups/ #切换至备份目录
[root@admin1 backups]# ls #查看之前的完全备份
2016-11-25_00-32-52
[root@admin1 backups]# innobackupex --incremental /backups/ --incremental-basedir=/backups/2016-11-25_00-32-52 #增量备份
[root@admin1 backups]# ls
2016-11-25_00-32-52 2016-11-25_00-42-48
#增量备份后,我们再次创建一个数据库,但是还没来得及备份MySQL服务已经宕机,这时就需要借助于二进制日志文件来做恢复
MariaDB [(none)]> CREATE DATABASE laozhai; #创建新的数据
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| laozhai |
| mysql |
| performance_schema |
| test |
| zhai |
+--------------------+
7 rows in set (0.00 sec)
#我们需要将上一次增量备份所记录的二进制日志position提取出来
[root@admin1 backups]# cat 2016-11-25_00-42-48/xtrabackup_binlog_info #查看二进制日志position
master-bin.000003 8304
[root@admin1 backups]# mysqlbinlog --start-position=8304 /var/lib/mysql/master-bin.000003 > /backups/binlog.sql #截取二进制日志
BASEDIR指的是完全备份所在的目录,此命令执行结束后,innobackupex命令会在/extrabackup目录中创建一个新的以时间命名的目录以存放所有的增量备份数据。另外,在执行过增量备份之后再一次进行增量备份时,其–incremental-basedir应该指向上一次的增量备份所在的目录。
整理增量备份
[root@admin1 backups]# innobackupex --apply-log --redo-only /backups/2016-11-25_00-32-52
[root@admin1 backups]# innobackupex --apply-log --redo-only /backups/2016-11-25_00-32-52 --incremental-dir=/backups/2016-11-25_00-42-48
恢复数据
[root@admin1 backups]# systemctl stop mariadb #停止MySQL服务
[root@admin1 backups]# rm -rf /var/lib/mysql/* #删除数据目录
[root@admin1 backups]# innobackupex --copy-back /backups/2016-11-25_00-32-52 #恢复数据
[root@admin1 mysql]# chown -R mysql.mysql /var/lib/mysql/* #修改属主属组
[root@admin1 mysql]# systemctl start mariadb #启动MySQL
MariaDB [(none)]> SHOW DATABASES; #此时发现hellodb已经恢复,但是我们新创建的数据库laozhai还没有恢复,我们只需对我们之前所备份的二进制日志文件做重放就可以了
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mysql |
| performance_schema |
| test |
| zhai |
+--------------------+
6 rows in set (0.01 sec)
[root@admin1 mysql]# cat /backups/binlog.sql | mysql #重放二进制日志
MariaDB [(none)]> SHOW DATABASES; #数据恢复
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| laozhai |
| mysql |
| performance_schema |
| test |
| zhai |
+--------------------+
7 rows in set (0.00 sec)
#完成
原创文章,作者:zhai796898,如若转载,请注明出处:http://www.178linux.com/60654