xtrabackup全量和增量备份
中小企业MySQL Xtrabackup物理增量恢复案例实战
如果对运维课程感兴趣,可以在b站上、csdn或微信视频号 上搜索我的账号: 运维实战课程,可以关注我,学习更多免费的运维实战技术视频
1.安装mariadb-mysql5.5
(xbk备份也适用于mysql5.7.x,只是需要将mysql5.7.x的binlog日志记录格式调整成记录语句更好操作binlog的增量恢复)
# yum -y install mariadb mariadb-server
# cp /etc/my.cnf /etc/my.cnf.bak
# cat /etc/my.cnf |grep -vE "^$|#"
[mysqld]
datadir=/var/lib/mysql/data
socket=/var/lib/mysql/mysql.sock
server-id=1
log-bin=/var/lib/mysql/logs/log-bin
symbolic-links=0
[mysqld_safe]
log-error=/var/lib/mysql/logs/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
# mkdir /var/lib/mysql/{logs,data}
# chown -R mysql.mysql /var/lib/mysql
# systemctl start mariadb
# ls /var/lib/mysql/
data logs mysql.sock
# ls /var/lib/mysql/logs/
log-bin.000001 log-bin.000002 log-bin.000003 log-bin.index mariadb.log
# ls /var/lib/mysql/data/
aria_log.00000001 aria_log_control ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema test
# netstat -anput |grep 3306 |grep LISTEN
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1684/mysqld
# mysql_secure_installation #初始化优化数据库
设置root密码: 123abc@ABC,和一路yes
# mysql -uroot -p123abc@ABC
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.68-MariaDB MariaDB Server
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
MariaDB [(none)]> quit
2.安装xbackup
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost ~]# yum -y install perl perl-devel libaio libaio-devel perl-Time-HiRes perl-DBD-MySQL
wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.4/binary/redhat/6/x86_64/percona-xtrabackup-24-2.4.4-1.el6.x86_64.rpm
[root@localhost ~]# ls percona-xtrabackup-24-2.4.4-1.el6.x86_64.rpm
percona-xtrabackup-24-2.4.4-1.el6.x86_64.rpm
[root@localhost ~]# yum -y install percona-xtrabackup-24-2.4.4-1.el6.x86_64.rpm
[root@localhost ~]# which innobackupex #是xtrabackup的链接文件,用它就行
/usr/bin/innobackupex
[root@localhost ~]# ll /usr/bin/innobackupex
lrwxrwxrwx. 1 root root 10 Jan 14 15:24 /usr/bin/innobackupex -> xtrabackup
[root@localhost ~]# innobackupex -v
innobackupex version 2.4.4 Linux (x86_64) (revision id: df58cf2)
3.模拟数据
[root@localhost ~]# mkdir /server/backup -p
[root@localhost ~]# mysql -uroot -p123abc@ABC
mysql> create database ceshi;
mysql> use ceshi;
mysql> create table test (id int(5),name varchar(20));
insert into test values(1,'full01');
insert into test values(2,'full02');
insert into test values(3,'full03');
insert into test values(4,'full04');
insert into test values(5,'full05');
mysql> select * from test;
+------+--------+
| id | name |
+------+--------+
| 1 | full01 |
| 2 | full02 |
| 3 | full03 |
| 4 | full04 |
| 5 | full05 |
+------+--------+
4.2021/01/13 0点全量备份
# date -s "2021/01/13"
# innobackupex --defaults-file=/etc/my.cnf --user=root --password=123abc@ABC --socket=/var/lib/mysql/mysql.sock --no-timestamp /server/backup/new_base_full
#--no-timestamp:该选项可以表示不要创建一个时间戳目录来存储备份,指定到自己想要的备份文件夹
5.2021/01/14 0点增量备份(第一次增量备份)
# mysql -uroot -p123abc@ABC -e "use ceshi;insert into test values(6,'new_inc_one_1');"
# mysql -uroot -p123abc@ABC -e "use ceshi;insert into test values(7,'new_inc_one_2');"
# mysql -uroot -p123abc@ABC -e "select * from ceshi.test;"
+------+---------------+
| id | name |
+------+---------------+
| 1 | full01 |
| 2 | full02 |
| 3 | full03 |
| 4 | full04 |
| 5 | full05 |
| 6 | new_inc_one_1 |
| 7 | new_inc_one_2 |
+------+---------------+
# date -s "2021/01/14"
# innobackupex --defaults-file=/etc/my.cnf --user=root --password=123abc@ABC --socket=/var/lib/mysql/mysql.sock --no-timestamp --incremental-basedir=/server/backup/new_base_full --incremental /server/backup/new_one_inc
#第一次增量备份是在全备的基础上
6.2021/01/15 0点增量备份(第二次增量备份)
# mysql -uroot -p123abc@ABC -e "use ceshi;insert into test values(8,'new_inc_two1');"
# mysql -uroot -p123abc@ABC -e "use ceshi;insert into test values(9,'new_inc_two2');"
# mysql -uroot -p123abc@ABC -e "select * from ceshi.test;"
+------+---------------+
| id | name |
+------+---------------+
| 1 | full01 |
| 2 | full02 |
| 3 | full03 |
| 4 | full04 |
| 5 | full05 |
| 6 | new_inc_one_1 |
| 7 | new_inc_one_2 |
| 8 | new_inc_two1 |
| 9 | new_inc_two2 |
+------+---------------+
# date -s "2021/01/15"
# innobackupex --defaults-file=/etc/my.cnf --user=root --password=123abc@ABC --socket=/var/lib/mysql/mysql.sock --no-timestamp --incremental-basedir=/server/backup/new_one_inc --incremental /server/backup/new_two_inc
#第二次增量备份是在第一次增量备份的基础上
7.2021/01/16 0点以后-10点更新数据
# mysql -uroot -p123abc@ABC -e "use ceshi;insert into test values(10,'realbinlog_data_3');"
# mysql -uroot -p123abc@ABC -e "use ceshi;insert into test values(11,'realbinlog_data_4');"
# mysql -uroot -p123abc@ABC -e "select * from ceshi.test;"
+------+-------------------+
| id | name |
+------+-------------------+
| 1 | full01 |
| 2 | full02 |
| 3 | full03 |
| 4 | full04 |
| 5 | full05 |
| 6 | new_inc_one_1 |
| 7 | new_inc_one_2 |
| 8 | new_inc_two1 |
| 9 | new_inc_two2 |
| 10 | realbinlog_data_3 |
| 11 | realbinlog_data_4 |
+------+-------------------+
8.2018/01/16上午10点出故障了
# mysql -uroot -p123abc@ABC -e "use ceshi;update test set name='old';"
# mysql -uroot -p123abc@ABC -e " select * from ceshi.test;"
+------+------+
| id | name |
+------+------+
| 1 | old |
| 2 | old |
| 3 | old |
| 4 | old |
| 5 | old |
| 6 | old |
| 7 | old |
| 8 | old |
| 9 | old |
| 10 | old |
| 11 | old |
+------+------+
建议停库:
# systemctl stop mariadb|mysqld 或开启防火墙停止对外服务: iptables -I INPUT ! -s 127.0.0.1 -p tcp -j DROP
9.恢复数据库(注意:下面的cp -r也可是cp -a)
1).合并数据文件(全量和增量备份的数据)
注意:合并数据时候,前面几条需加—redo-only,在最后一条上不用加。
恢复增量备份到完全备份(开始恢复的增量备份和全量变化数据要添加--redo-only参数,到最后一次增量备份去掉--redo-only参数)
--redo-only是对整体的完全备份进行恢复,回滚那些未提交的数据
--redo-only参数,该参数的意思是只应用xtrabackup日志中已提交的事务数据,不回滚还未提交的数据
参考链接: innobackupex 恢复实验 - zengkefu - 博客园
# cp -r /server/backup/ /opt/ #备份备份的数据
注意:全量备份时候,也会有变化的数据,只不过变化的数据会记录在一个文件:xtrabackup_logfile中,所以全备完整是数据是:全备数据+变化的数据。下面是合成整个全备数据=将全备过程中变化的数据+全备数据文件的数据合并到一块:(指定的目录为:第一次全量备份时目录路径)
#innobackupex --apply-log --use-memory=32M --redo-only /server/backup/new_base_full/ (将全备过程中变化的数据合并过来)
下面是将第一次增量备份数据合并过来:(指定的目录为:第一次全量备份时目录路径)
#innobackupex --apply-log --use-memory=32M --redo-only --incremental-dir=/server/backup/new_one_inc /server/backup/new_base_full/
下面是将第二次增量备份数据合并过来:(指定的目录为:第一次全量备份时目录路径)
#innobackupex --apply-log --use-memory=32M --incremental-dir=/server/backup/new_two_inc /server/backup/new_base_full/
2).删除原来数据文件和开始恢复(全量和增量备份的数据)
# mv /var/lib/mysql/data/ /var/lib/mysql/data.bak
# cp -r /server/backup/new_base_full /var/lib/mysql/data
# ls /var/lib/mysql/data
backup-my.cnf ibdata1 mysql xtrabackup_binlog_info xtrabackup_checkpoints xtrabackup_logfile
ceshi ibtmp1 performance_schema xtrabackup_binlog_pos_innodb xtrabackup_info
# chown -R mysql.mysql /var/lib/mysql/data/
# systemctl start mariadb|mysqld
[root@localhost ~]# mysql -uroot -p123abc@ABC -e "select * from ceshi.test;"
+------+---------------+
| id | name |
+------+---------------+
| 1 | full01 |
| 2 | full02 |
| 3 | full03 |
| 4 | full04 |
| 5 | full05 |
| 6 | new_inc_one_1 |
| 7 | new_inc_one_2 |
| 8 | new_inc_two1 |
| 9 | new_inc_two2 |
+------+---------------+
3).处理binlog(准备恢复没有备份时间后新增加的数据)
# cat /server/backup/new_two_inc/xtrabackup_binlog_info #查看第二次增量备份后的末尾位置点,是后面逻辑备份的
log-bin.000003 3463
# ls /var/lib/mysql/logs/
log-bin.000001 log-bin.000002 log-bin.000003 log-bin.000004 log-bin.index mariadb.log
# mysqlbinlog -d ceshi --start-position=3463 /var/lib/mysql/logs/log-bin.000003 -r /server/backup/bin.sql
#注意:多个binlog时用: mysqlbinlog -d ceshi oldboy-bin.000003 oldboy-bin.000004 oldboy-bin.000005 >>bin.sql
# vim /server/backup/bin.sql
删除update一行。“update test set name='old'”
4).恢复binlog日志的新增数据(开始恢复没有备份时间后新增加的数据)
# mysql -uroot -p123abc@ABC ceshi < /server/backup/bin.sql
# mysql -uroot -p123abc@ABC -e "select * from ceshi.test;"
+------+-------------------+
| id | name |
+------+-------------------+
| 1 | full01 |
| 2 | full02 |
| 3 | full03 |
| 4 | full04 |
| 5 | full05 |
| 6 | new_inc_one_1 |
| 7 | new_inc_one_2 |
| 8 | new_inc_two1 |
| 9 | new_inc_two2 |
| 10 | realbinlog_data_3 |
| 11 | realbinlog_data_4 |
+------+-------------------+
附加:安装mysql(此处为5.7.32)—上面xbk备份,用该版本5.7.32版本也没有问题
[root@localhost ~]# wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
[root@localhost ~]# rpm -ivh mysql57-community-release-el7-8.noarch.rpm
[root@localhost ~]# yum -y install mysql-server
[root@localhost ~]# cp /etc/my.cnf /etc/my.cnf.bak
[root@localhost ~]# vim /etc/my.cnf #不修改配置文件也能启动,此处修改配置文件是为了将数据目录和日志目录分开
[root@localhost ~]# cat /etc/my.cnf |grep -vE "^$|#"
[mysqld]
datadir=/var/lib/mysql/data
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
server-id=1
log-bin=/var/lib/mysql/logs/log-bin
#binlog-format=STATEMENT #容易丢数据,binlog日志显示insert等语句
binlog-format=MIXED #居中,binlog日志显示insert等语句
#binlog-format=ROW #不容易丢数据,binlog日志不显示insert等语句
log-error=/var/lib/mysql/logs/error.log
pid-file=/var/run/mysqld/mysqld.pid
[root@localhost ~]# mkdir /var/lib/mysql/{logs,data}
[root@localhost ~]# chown -R mysql.mysql /var/lib/mysql
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2021-01-14 10:20:07 CST; 14s ago
[root@localhost ~]# netstat -anput |grep 3306 |grep LISTEN
tcp6 0 0 :::3306 :::* LISTEN 2378/mysqld
[root@localhost ~]# ls /var/lib/mysql
data logs mysql.sock mysql.sock.lock
[root@localhost ~]# ls /var/lib/mysql/data/
auto.cnf ca.pem client-key.pem ibdata1 ib_logfile1 mysql private_key.pem server-cert.pem sys
ca-key.pem client-cert.pem ib_buffer_pool ib_logfile0 ibtmp1 performance_schema public_key.pem server-key.pem
[root@localhost ~]# ls /var/lib/mysql/logs/
error.log log-bin.000001 log-bin.000002 log-bin.index
[root@localhost ~]# grep "password" /var/lib/mysql/logs/error.log
2021-01-22T07:36:26.199844Z 1 [Note] A temporary password is generated for root@localhost: (?2rnbtXWg5C
[root@localhost ~]# mysql -uroot -p
Enter password: 输入密码:(?2rnbtXWg5C
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.32
mysql> alter user 'root'@'localhost' identified by '123abc@ABC';
mysql> grant all on *.* to 'root'@'%' identified by '123abc@ABC';
mysql> flush privileges;
mysql> quit
[root@localhost ~]# mysql -uroot -p123abc@ABC
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
mysql> create database ku;
mysql> use ku;
mysql> create table biao (id int(2));
mysql> insert into biao values('1');
mysql> insert into biao values('2');
mysql> insert into biao values('3');
mysql> select * from biao;
+------+
| id |
+------+
| 1 |
| 2 |
| 3 |
+------+
mysql> quit
[root@bogon ~]# ls /var/lib/mysql/logs/
error.log log-bin.000001 log-bin.000002 log-bin.index
查看binlog日志记录情况:能记录insert、delete、update、create等语句
[root@bogon ~]# mysqlbinlog /var/lib/mysql/logs/log-bin.000002 |grep create
#210122 15:36:28 server id 1 end_log_pos 123 CRC32 0x35e68675 Start: binlog v 4, server v 5.7.33-log created 210122 15:36:28 at startup
create database ku
create table biao (id int(2))
[root@bogon ~]# mysqlbinlog /var/lib/mysql/logs/log-bin.000002 |grep insert
insert into biao values('1')
insert into biao values('2')
insert into biao values('3')
如果对运维课程感兴趣,可以在b站上、csdn或微信视频号 上搜索我的账号: 运维实战课程,可以关注我,学习更多免费的运维实战技术视频