MySql 物理备份和恢复


xtrabackup是percona公司针对MySQL开发的一款开源的物理备份工具,直接拷贝物理文件,速度快,效率高,支持不锁表备份,支持全量、增量(基于LSN序号)、压缩及流备份

# 下载rpm 地址 https://downloads.mysql.com/archives/community/ 
安装:
rpm -ivh mysql-community-common-8.0.33-1.el8.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.33-1.el8.x86_64.rpm
rpm -ivh mysql-community-client-plugins-8.0.33-1.el8.x86_64.rpm
rpm -ivh mysql-community-client-8.0.33-1.el8.x86_64.rpm
rpm -ivh mysql-community-icu-data-files-8.0.33-1.el8.x86_64.rpm
rpm -ivh mysql-community-server-8.0.33-1.el8.x86_64.rpm

对应数据库对应版本 (源数据库机器和目标机器都安装)
wget https://downloads.percona.com/downloads/Percona-XtraBackup-8.0/Percona-XtraBackup-8.0.33-28/binary/redhat/8/x86_64/percona-xtrabackup-80-8.0.33-28.1.el8.x86_64.rpm
yum install percona-xtrabackup-80-8.0.33-28.1.el8.x86_64.rpm

安装压缩工具
#yum -y install qpress


1.源数据机器,备份数据库
xtrabackup --backup --target-dir=/opt/mysql_bkdata  --compress --compress-threads=4  --datadir=/opt/mysql/data --host=127.0.0.1  --user=root --password=xxxx -S /tmp/mysql.sock

备份:/opt/mysql_bkdata
数据库源目录:/opt/mysql/data 

tar -zcvf mysql_bkdata.tar.gz mysql_bkdata

2.目标机器,恢复数据库

从源数据机器下载 mysql_bkdata.tar.gz

目录:/var/ 解压文件mysql_bkdata.tar.gz
cd /var/
tar -zxvf mysql_bkdata.tar.gz

# 解压
xtrabackup --decompress --remove-original --target-dir=/var/mysql_bkdata/
...
[Xtrabackup] completed OK!
# 恢复前准备
xtrabackup --defaults-file=/var/mysql_bkdata/backup-my.cnf  --prepare --target-dir=/var/mysql_bkdata/
 
...[MY-011825] [Xtrabackup] completed OK!

# 编辑数据库配置文件 /etc/my.conf
datadir = /var/mysql_newdata

# 恢复
xtrabackup --defaults-file=/etc/my.cnf --copy-back --target-dir=/var/mysql_bkdata/

chown -R mysql:mysql /var/mysql_newdata

# 启动
systemctl start mysqld


## 不停机情况下,备份完成后,源数据库还在写情况,需要开启主从同步至数据一致
1. 目标数据库作为从库,同步起始位置查看备份目录下 mysql_bkdata/xtrabackup_info
如:binlog_pos = filename 'mysql-bin.019209', position 156975931, GTID of the last change '07def8c4-c389-11ec-ade8-7cd30ae40448:1-24


文章作者: weilongshi
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 weilongshi !
  目录