美文网首页
xtrabackup备份恢复

xtrabackup备份恢复

作者: 左轮Lee | 来源:发表于2019-02-21 11:38 被阅读0次
    Percona XtraBackup 支持的mysql版本信息:

    Percona XtraBackup下包含两个工具,Innobackup和xtrabackup。
    下面介绍下各个版本这两个工具的差异。

    2.2 版本支持 InnoDB, XtraDB, and MyISAM tables on MySQL 5.1, 5.5 and 5.6 servers, as well as Percona Serverwith XtraDB.
    innobackupex a wrapper script that provides functionality to backup a wholeMySQL database instance with MyISAM, InnoDB, and XtraDBtables. ###2.2版本时期都用此工具备份,因为可备份包含Innodb和Myisam引擎的表;
    xtrabackup a compiled C binary, which copies only InnoDB and XtraDB data ###不支持备份Myisam表。

    2.3 版本支持 InnoDB, XtraDB, and MyISAM tables on MySQL 5.1, 5.5 and 5.6 servers, as well as Percona Serverwith XtraDB. ###该版本中xtrabackup支持了Innodb和Myisam引擎的表备份,innobackupex工具只是xtrabackup的一个软链接,而且在后续版本中会被去掉。
    innobackupex innobackupex is the symlink for xtrabackup. innobackupexstill supports all features and syntax as 2.2 version did, but is now deprecated and will be removed in next major release.
    xtrabackup a compiled C binary that provides functionality to backup a whole MySQL database instance with MyISAM, InnoDB, and XtraDB tables.

    2.4 版本支持 InnoDB, XtraDB, and MyISAM tables on MySQL 5.1, 5.5, 5.6 and 5.7 servers, as well as Percona Server with XtraDB. ###该版本中xtrabackup支持了mysql5.7,其他特性和2.3版本一致。
    innobackupex innobackupex is the symlink for xtrabackup. innobackupexstill supports all features and syntax as 2.2 version did, but is now deprecated and will be removed in next major release.
    xtrabackup a compiled C binary that provides functionality to backup a whole MySQL database instance with MyISAM, InnoDB, and XtraDBtables.

    8.0 版本支持 InnoDB, XtraDB, and MyISAM tables on MySQL 8.0 servers as well as Percona Server with XtraDB, Percona Server 8.0, and Percona XtraDB Cluster 8.0。 ###此版本单独为mysql8.0开发,不支持8.0之前的db版本。
    xtrabackup a compiled C binary that provides functionality to backup a whole MySQL database instance with MyISAM, InnoDB, and XtraDB tables.
    innobackupex ###此工具已经被移除

    目前使用版本大多以2.4为主,故不区分 xtrabackup 和 innobackupex,Myisam和Innodb 都支持。
    所以以 xtrabackup 为例测试备份恢复:

    全备和恢复
    全量备份:
    xtrabackup -uroot -proot123 --backup --target-dir=/data/backup/20190103/
    
    全量恢复:
    xtrabackup --prepare --target-dir=/data/backup/20190103/
    xtrabackup --move-back|--copy-back --target-dir=/data/backup/20190103/ --datadir=/data/mysql/mysql3306/data
    --move-back 不保留备份,相当于 mv
    --copy-back 保留备份,相当于cp
    chown -R mysql:mysql /data/mysql/mysql3306/data
    
    启动:
    service mysql start
    
    增量备份和恢复
    先做全量备份:
    xtrabackup -uroot -proot123 --parallel=4 --backup --target-dir=/data/backup/20190103/
    [root@localhost 20190103]# cat xtrabackup_checkpoints
    backup_type = full-backuped
    from_lsn = 0
    to_lsn = 2558962
    last_lsn = 2558971
    compact = 0
    recover_binlog_info = 0
    
    增量备份1:
    xtrabackup -uroot -proot123 --backup --parallel=4 --target-dir=/data/backup/inc1 --incremental-basedir=/data/backup/20190103/
    [root@localhost inc1]# cat xtrabackup_checkpoints 
    backup_type = incremental
    from_lsn = 2558962
    to_lsn = 2559915
    last_lsn = 2559924
    compact = 0
    recover_binlog_info = 0
    
    增量备份2:
    xtrabackup -uroot -proot123 --backup --target-dir=/data/backup/inc2 --incremental-basedir=/data/backup/inc1
    [root@localhost inc2]# cat xtrabackup_checkpoints 
    backup_type = incremental
    from_lsn = 2559915
    to_lsn = 2560568
    last_lsn = 2560577
    compact = 0
    recover_binlog_info = 0
    
    In this case you can see that there is a difference between the to_lsn (last checkpoint LSN) and last_lsn (last copied LSN), this means that there was some traffic on the server during the backup process. 
    to_lsn和last_lsn之间存在差异,表示备份期间服务器有some traffic
    
    增量备份时有两种增量备份策略:
    1.读取所有的页的LSN,找到LSN大于之前的全备或者增量备的页,然后copy pages;
    2.适用于percona server,开启 [changed page tracking]参数,可以将变化的页记录到一个位图文件中,增量备份时只需要读取该文件即可,大大减少读取数据页的请求 。即使位图文件可用,你也可以强制读取所有页, [xtrabackup --incremental-force-scan] 。
    实际上,你也可以指定LSN来执行增量备份 [xtrabackup --incremental-lsn],而不需要之前的全备文件, 不过恢复时仍然需要全备,否则这个增量备份将毫无用处。
    
    备份完成后再执行几条sql语句,模拟实际场景:
    insert into tt values(400); 
    insert into tt values(401); 
    insert into tt values(402);
    然后模拟数据库宕机,恢复数据: kill -9 xxxx
    删除数据文件目录: mv /data/mysql/mysql3306/data /data/mysql/mysql3306/data.bak
    删除日志文件目录: mv /data/mysql/mysql3306/logs /data/mysql/mysql3306/logs.bak
    创建需要目录: mkdir /data/mysql/mysql3306/{data,logs}
    
    完整及增量恢复
    prepare:
    xtrabackup --prepare --apply-log-only --target-dir=/data/backup/20190103
    xtrabackup --prepare --apply-log-only --target-dir=/data/backup/20190103 --incremental-dir=/data/backup/inc1
    xtrabackup --prepare --target-dir=/data/backup/20190103 --incremental-dir=/data/backup/inc2   ##最后一个去掉 --apply-log-only参数
    
    restore:
    xtrabackup --parallel=4 --copy-back --target-dir=/data/backup/20190103 --datadir=/data/mysql/mysql3306/data
    
    启动:
    service mysqld start
    
    由于备份完成后仍有数据变动,查看恢复完成的最后binlog位置:
    下面两种方式均可以:
    一是查看增量备份文件的记录
    二是查看全部restore完成后的文件记录,二者一致
    [root@localhost data]# cat /data/backup/inc2/xtrabackup_binlog_info
    mysql-bin.000001        2576
    
    [root@localhost data]# cat /data/mysql/mysql3306/data/xtrabackup_binlog_pos_innodb 
    mysql-bin.000001        2576
    
    如果主机上还保存有最近的binlog文件,则恢复即可:
    mysqlbinlog --start-position=2576 mysql-bin.000001 |mysql -uroot -proot123
    
    压缩备份与恢复:
    多线程压缩全备:
    xtrabackup -uroot -proot123 --backup --compress --compress-threads=4 --target-dir=/data/backup/compressed/ ## 1.4G可以压缩到16M
    
    全备解压缩:
    yum install -y qpress
    xtrabackup --parallel=4 --decompress --target-dir=/data/backup/compressed/ --remove-original
    
    全备恢复:
    xtrabackup --prepare --target-dir=/data/backup/compressed/
    xtrabackup --move-back|--copy-back --target-dir=/data/backup/20190103/ --datadir=/data/mysql/mysql3306/data
    chown -R mysql:mysql /var/lib/mysql
    
    启动:
    service mysql start
    
    加密备份与恢复:
    加密备份:
    生成密钥
    [root@localhost backup]# openssl rand -base64 24
    5QsS+fykEURHFmuLcFS81aIkCLaVJIyv
    
    xtrabackup -uroot -proot123 --backup --target-dir=/data/backup/encrypt --encrypt=AES256 --encrypt-threads=4 --encrypt-chunk-size=64K --encrypt-key="5QsS+fykEURHFmuLcFS81aIkCLaVJIyv"
    或者
    echo -n "5QsS+fykEURHFmuLcFS81aIkCLaVJIyv" > /data/backup/keyfile
    xtrabackup -uroot -proot123 --backup --target-dir=/data/backup/encrypt --encrypt=AES256 --encrypt-threads=4 --encrypt-chunk-size=64K --encrypt-key-file=/data/backup/keyfile
    
    解密:
    xtrabackup --decrypt=AES256 --parallel=4 --encrypt-key="5QsS+fykEURHFmuLcFS81aIkCLaVJIyv" --target-dir=/data/backup/encrypt --remove-original
    
    恢复:
    xtrabackup --prepare --target-dir=/data/backup/encrypt/
    xtrabackup --move-back|--copy-back --target-dir=/data/backup/encrypt/ --datadir=/data/mysql/mysql3306/data
    

    相关文章

      网友评论

          本文标题:xtrabackup备份恢复

          本文链接:https://www.haomeiwen.com/subject/qstryqtx.html