备份实战案例1
(1)获得软件包
- https://www.percona.com/ — 在官网安装percona XtraBackup
(2)安装
- 安装YUM仓库
安装percona需要的mysql包
mysql官方源 — yum install -y https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
yum工具 — yum install -y yum-utils
查看mysql可用版本 yum repolist all | grep mysql
禁用80 — yum-config-manager --disable mysql80-community
启用57 — yum-config-manager --enable mysql57-community
yum install mysql-community-libs-compat -y // 安装mysql插件
yum install https://repo.percona.com/yum/percona-release-latest.noarch.rpm
- YUM安装percona-xtrabackup
- yum -y install percona-xtrabackup-24.x86_64
- 查询安装结果
- rpm -ql percona-xtrabackup-24 rpm -qa percona-xtrabackup-24
(3)完全备份流程
- innobackupex --user=root --password=‘QianFeng@123’ /xtrabackup/full // 连接数据库,开始备份。
- ls /xtrabackup/full/ // 查看备份目录。数据库,配置文件,日志文件
- ls /xtrabackup/full/2017-08-01_00-00-02/ // 观看二进制日志位置(cat /xtrabackup/full/2017-08-01_00-00-18/xtrabackup_binlog_info ?)
(4)完全恢复流程
- systemctl stop mysqld
- rm -rf /var/lib/mysql/* rm -rf /var/log/mysqld.log // 清理环境(模拟损坏)
- innobackupex --apply-log /xtrabackup/full/2017-08-01_00-00-18/ // 生成回滚日志
- innobackupex --copy-back /xtrabackup/full/2017-08-01_00-00-18/ // 恢复文件
- 登陆验证
- ls /var/lib/mysql
- chown -R mysql.mysql /var/lib/mysql
- systemctl start mysqld
- mysql -uroot -p’QianFeng@123’
(5)增量备份流程
- 准备工作
- create database testdb;
- use testdb;
- create table test(id int);
- insert into test values (1);
- select * from test;
- 完整备份:周一
- rm -rf /xtrabackup/*
- date 09010000
- innobackupex --user=root --password=‘QianFeng@123’ /xtrabackup
- ll /xtrabackup/
- 数据库记录:1
- 增量备份:周二
- date 09020000 // 更新时间
- mysql -uroot -p’QianFeng@123’ -e ‘insert into testdb.test values (2)’
- mysql -uroot -p’QianFeng@123’ -e ‘select * from testdb.test’
- innobackupex --user=root --password=‘QianFeng@123’ --incremental xtrabackup/
–incremental-basedir=/xtrabackup/2017-09-01_00-00-04 - ls /xtrabackup/
- 增量备份:周三
date 09030000
mysql -uroot -p’QianFeng@123’ -e ‘insert into testdb.test values (3)’ innobackupex --user=root --password=‘QianFeng@123’ --incremental /xtrabackup/ --incremental-basedir=/xtrabackup/2017-09-02_00-00-26 // basedir基于周2的备份
会生成一个今天的。ls /xtrabackup/
(6)增量恢复流程
- 停止数据库
- systemctl stop mysqld
- 清理环境
- rm -rf /var/lib/mysql/*
- 周一
- innobackupex --apply-log --redo-only /xtrabackup/2017-09-01_00-00-04、
- 周二
- innobackupex --apply-log --redo-only /xtrabackup/2017-09-01_00-00-04 --incremental-dir=/xtrabackup/2017-09-02_00-00-26
- 恢复
innobackupex --copy-back /xtrabackup/2017-09-01_00-00-06
chown -R mysql.mysql /var/lib/mysql
systemctl start mysqld
若想恢复第三天
systemctl stop mysqld
innobackupex --apply-log --redo-only /xtrabackup/2017-09-01_00-00-04 --incremental-dir=/xtrabackup/2017-09-03_00-00-27
rm -rf /var/lib/mysql/*
innobackupex --copy-back /xtrabackup/2017-09-01_00-00-04
chown -R mysql.mysql /var/lib/mysql
systemctl start mysqld
mysql -uroot -p’QianFeng@123’ -e ‘select * from testdb.test’
(7)差异备份流程
- rm -rf /xtrabackup/*
- 完整备份周一
- date 10010000
- create database testdb;
use testdb;
create table test2(id int);
insert into test2 values(1);
select * from test2; - innobackupex --user=root --password=‘QianFeng@123’ /xtrabackup
- 差异备份
周二
date 10020000
mysql -uroot -p’QianFeng@123’ -e ‘insert into testdb.test2 values(2)’
innobackupex --user=root --password=‘QianFeng@123’
–incremental /xtrabackup
–incremental-basedir=/xtrabackup/2017-10-01_00-01-12 // 完全备份目录(周一)ll /xtrabackup/ du -sh /xtrabackup/
周三
date 10030000
mysql -uroot -p’QianFeng@123’ -e ‘insert into testdb.test2 values(3)’
innobackupex --user=root --password=‘QianFeng@123’
–incremental /xtrabackup
–incremental-basedir=/xtrabackup/2017-10-01_00-01-12 // 完全备份目录(周一)ll /xtrabackup/ du -sh /xtrabackup/
周四
date 10040000
mysql -uroot -p’QianFeng@123’ -e ‘insert into testdb.test2 values(4)’
innobackupex --user=root --password=‘QianFeng@123’
–incremental /xtrabackup
–incremental-basedir=/xtrabackup/2017-10-01_00-01-12 // 完全备份目录(周一)ll /xtrabackup/ du -sh /xtrabackup/
(8)差异恢复流程
- systemctl stop mysqld
- rm -rf /var/lib/mysql/*
- 重演回滚redo log(周一,某次差异)--> 恢复数据
- innobackupex --apply-log --redo-only /xtrabackup/2017-10-01_00-01-12
- innobackupex --apply-log --redo-only
/xtrabackup/2017-10-01_00-01-12
–incremental-dir=/xtrabackup/2017-10-04_00-00-29 - innobackupex --copy-back /xtrabackup/2017-10-01_00-01-12
- chown -R mysql.mysql /var/lib/mysql/ // 修改权限
- systemctl start mysqld