MySQL 自动备份还原工具 MySQL-AutoXtraBackup

MySQL-AutoXtraBackup 是 Percona 实验室中的开源工具,是基于 xtrabackup 和 autoxtrabackup 备份的备份工具。如果不是很熟悉 AutoXtraBackup,个人觉得还不如使用 autoxtrabackup 方便,因为 AutoXtraBackup 同样需要配置和了解相关参数。

 

MySQL-AutoXtraBackup 1.5 安装要求:

Percona Xtrabackup (>= 2.3.5)
Python 3 (tested version 3.5.3 on CentOS 7)
mysql-utilities (>=1.5.4)
支持的数据库:  MySQL, Percona Server, MariaDB
支持的操作系统:Debian, Ubuntu, CentOS, RedHat
 

AutoXtraBackup 下载:https://github.com/Percona-Lab/MySQL-AutoXtraBackup
AutoXtraBackup 文档:https://mysql-autoxtrabackup.readthedocs.io/en/latest/index.html
Percona 的官方示例:https://www.percona.com/blog/2017/11/27/perconalab-autoxtrabackup-v1-5-0-release/

 

相关工具安装:

# Python 3.5 安装
wget https://www.python.org/ftp/python/3.5.6/Python-3.5.6.tar.xz 
tar xvf Python-3.5.6.tar.xz -C /tmp/
cd /tmp/Python-3.5.6
./configure --prefix=/usr/local/python3
make && make install
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
/usr/local/python3/bin/pip3 install --upgrade pip
/usr/local/python3/bin/pip3 list


# 相关包安装
yum install -y openssl openssl-devel zlib zlib-devel pigz


# mysql-connector-python 安装
wget https://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.1.5-1.el7.x86_64.rpm
yum install -y mysql-connector-python-2.1.5-1.el7.x86_64.rpm


# mysql-utilities 安装
wget https://dev.mysql.com/get/Downloads/MySQLGUITools/mysql-utilities-1.6.5-1.el7.noarch.rpm
yum install -y mysql-utilities-1.6.5-1.el7.noarch.rpm


# percona-xtrabackup 2.4 安装
yum install http://www.percona.com/downloads/percona-release/redhat/0.1-6/percona-release-0.1-6.noarch.rpm
yum list | grep percona
yum install -y percona-xtrabackup-24
xtrabackup --version


# MySQL-AutoXtraBackup 安装
pip3 install mysql-autoxtrabackup
ln -s /usr/local/python3/bin/autoxtrabackup /usr/bin/autoxtrabackup
autoxtrabackup --help

MySQL 配置文件须设置的变量:

# vim /etc/my.cnf
[mysqld]
innodb_file_per_table=1
binlog_format = row
log_bin=/usr/local/mysql/binlog/mysql-bin

autoxtrabackup 配置文件创建:

# vim /etc/bck.cnf
[MySQL]
#mysql访问信息及部分配置信息(备份还原用到)
mysql_host=localhost
mysql_port=3306
mysql_user=root
mysql_password=mysql
mysql_socket=/tmp/mysql.sock
mycnf=/etc/my.cnf
datadir=/usr/local/mysql/data
mysql=/usr/local/mysql/bin/mysql
mysqladmin=/usr/local/mysql/bin/mysqladmin

[Backup]
#备份目录
backupdir=/home/mysql/backup_dir
#原data目录的临时备份目录
tmpdir=/home/mysql/mysql_datadirs
#进行备份时使用的Percona xtrabackup可执行文件的完整路径
backup_tool=/usr/bin/xtrabackup
#准备(恢复)时使用的Percona xtrabackup可执行文件的完整路径
#prepare_tool=/usr/bin/xtrabackup
#pid运行目录
pid_dir=/tmp
#pid运行时长警告
pid_runtime_warning=2 Hours
#xtrabackup的选项,用于增量备份还原
xtra_prepare=--apply-log-only
#backup附加选项
#xtra_backup=--compact
#prepare附加选项
#xtra_prepare_options=--rebuild-indexes
#backup和prepare通用的附加选项(--binlog-info=ON --galera-info)
xtra_options=--no-version-check
#???
#prepare_archive=1
#将备份归档而不是使用tar.gz进行压缩
#move_archive=0
#完整备份间隔时间
#full_backup_interval=1 day
#大于最大大小则删除归档备份
#max_archive_size=100GiB
#大于最大保留时间则删除归档备份
#max_archive_duration=4 Days
#启用部分备份,需定义数据库或表名称
#partial_list=test.t1 test.t2 dbtest
#归档目录
#archive_dir=/home/mysql/backup_archives

[Compress]
#启用压缩(则取消注释)
#compress=quicklz
#compress_chunk_size=65536
#compress_threads=4
#decompress=TRUE
#解压缩后是否移除.qp文件.(Not available yet, will be released with XB 2.3.7 and 2.4.6)
#remove_original=FALSE

[Encrypt]
#启用加密备份(则取消注释)
#xbcrypt=/usr/bin/xbcrypt
#encrypt=AES256
#注意 --encrypt-key 和 --encrypt-key-file 是互斥的
#encrypt_key='VVTBwgM4UhwkTTV98fhuj+D1zyWoA89K'
#encrypt_key_file=/path/to/file/with_encrypt_key
#encrypt_threads=4
#encrypt_chunk_size=65536
#decrypt=AES256
#解压缩后是否移除.qp文件.(Not available yet, will be released with XB 2.3.7 and 2.4.6)
#remove_original=FALSE

[Xbstream]
#启用stream备份(则取消注释)
#xbstream=/usr/bin/xbstream
#stream=xbstream
#stream=tar
#xbstream_options=-x --parallel=100
#xbs_decrypt=1
#remote_stream=ssh xxx.xxx.xxx.xxx

#[Remote]
#远程同步(则取消注释)
#[email protected]
#remote_dir=/home/sh/Documents

[Commands]
#设置启动与停止MySQL服务的命令及默认MySQL用户&组
start_mysql_command=service mysqld start
stop_mysql_command=service mysqld stop
chown_command=chown -R mysql:mysql

先修复一个 bug :

# 先修复一个 bug ,否则连续第三次备份(即第二次增量备份)时报错如下:
Traceback (most recent call last):
  File "/usr/bin/autoxtrabackup", line 11, in 
    load_entry_point('mysql-autoxtrabackup==1.5.3', 'console_scripts', 'autoxtrabackup')()
  File "/usr/local/python3/lib/python3.5/site-packages/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/python3/lib/python3.5/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/usr/local/python3/lib/python3.5/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/python3/lib/python3.5/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/python3/lib/python3.5/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/usr/local/python3/lib/python3.5/site-packages/mysql_autoxtrabackup-1.5.3-py3.6.egg/autoxtrabackup.py", line 263, in all_procedure
    b.all_backup()
  File "/usr/local/python3/lib/python3.5/site-packages/master_backup_script/backuper.py", line 917, in all_backup
    self.inc_backup()
  File "/usr/local/python3/lib/python3.5/site-packages/master_backup_script/backuper.py", line 815, in inc_backup
    logger.debug("The following backup command will be executed {}".format(filteredargs))
UnboundLocalError: local variable 'filteredargs' referenced before assignment
2018-12-14 13:45:40 DEBUG     closing pidfile: /opt/MySQL-AutoXtraBackup-1.5.3/autoxtrabackup.pid


解决:
# vim /usr/local/python3/lib/python3.5/site-packages/master_backup_script/backuper.py

在 813 行,与上下行一样左对齐
812             # filter out password from argument list
813                 filteredargs = re.sub("--password='?\w+'?", "--password='*'", args)
814 
815             logger.debug("The following backup command will be executed {}".format(filteredargs))

改为:

812             # filter out password from argument list
813             filteredargs = re.sub("--password='?\w+'?", "--password='*'", args)
814 
815             logger.debug("The following backup command will be executed {}".format(filteredargs))

备份:

# 第一次运行则进行完整备份(以后参考完整备份间隔 full_backup_interval)
autoxtrabackup --tag="Full backup" --defaults_file=/etc/bck.cnf -v -l DEBUG -lf /home/mysql/autoxtrabackup.log --backup

autoxtrabackup --tag="First incremental backup" --defaults_file=/etc/bck.cnf -v -l DEBUG -lf /home/mysql/autoxtrabackup.log --backup

autoxtrabackup --tag="Second incremental backup" --defaults_file=/etc/bck.cnf -v -l DEBUG -lf /home/mysql/autoxtrabackup.log --backup

autoxtrabackup --tag="Third incremental backup" --defaults_file=/etc/bck.cnf -v -l DEBUG -lf /home/mysql/autoxtrabackup.log --backup

查看备份信息(2种方法):

# cat /home/mysql/backup_dir/backup_tags.txt 
2018-12-27_21-46-13	FullOK	2018-12-27_21-46-25	237M	'Full backup'
2018-12-27_22-08-42	Inc	OK	2018-12-27_22-08-55	12M		'First incremental backup'
2018-12-28_10-42-32	Inc	OK	2018-12-28_10-42-44	14M		'Second incremental backup'
2018-12-28_10-43-59	Inc	OK	2018-12-28_10-44-10	8.6M	'Third incremental backup'

# autoxtrabackup --show_tags --defaults_file=/etc/bck.cnf
Backup             	Type	Status	Completion_time    	Size	TAG
----------------------------------------------------------------------------------
2018-12-27_21-46-13	FullOK	2018-12-27_21-46-25	237M	'Full backup'
2018-12-27_22-08-42	Inc	OK	2018-12-27_22-08-55	12M		'First incremental backup'
2018-12-28_10-42-32	Inc	OK	2018-12-28_10-42-44	14M		'Second incremental backup'
2018-12-28_10-43-59	Inc	OK	2018-12-28_10-44-10	8.6M	'Third incremental backup'

还原:

# 执行还原有以下三个选项:
# autoxtrabackup -v -lf /home/mysql/autoxtrabackup.log -l DEBUG --defaults_file=/etc/bck.cnf --prepare

Preparing full/inc backups!
What do you want to do?
1. Prepare Backups and keep for future usage. NOTE('Once Prepared Backups Can not be prepared Again')
2. Prepare Backups and restore/recover/copy-back immediately
3. Just copy-back previously prepared backups
Please Choose one of options and type 1 or 2 or 3:(输入数字)

(选择2, 恢复到最近备份)

 

数据库进行了多次还原 ,导致日志序列号对不上,还原出错:

InnoDB: Page [page id: space=0, page number=3] 
log sequence number 3034169474 is in the future! Current system log sequence number 3031234808.
InnoDB: Your database may be corrupt or you may have copied the InnoDB tablespace but not the InnoDB log files. 
Please refer to http://dev.mysql.com/doc/refman/5.7/en/forcing-innodb-recovery.html for information about forcing recovery.

解决:(还没试过)
https://www.percona.com/blog/2013/09/11/how-to-move-the-innodb-log-sequence-number-lsn-forward/

你可能感兴趣的:(MYSQL,MYSQL,备份还原,MYSQL,常用工具)