美文网首页
Xtrabackup对Mysql备份及恢复数据

Xtrabackup对Mysql备份及恢复数据

作者: ALuckyLive | 来源:发表于2017-09-12 22:47 被阅读0次
    xtrabackup对mysql进行备份,恢复
    Mysql启用状态;
    1、全量备份,恢复
    2、全量备份,增量备份,恢复
    
    1、全量备份及模拟故障恢复;
    
    下载xtrabackup;
    [root@note2 ~]# lftp 172.16.0.1/pub
    cd 成功, 当前目录=/pub               
    lftp 172.16.0.1:/pub> cd Sources/7.x86_64/percona/
    lftp 172.16.0.1:/pub/Sources/7.x86_64/percona> ls
    -rw-r--r--    1 0        0         7732612 Aug 21 09:15 percona-xtrabackup-24-2.4.7-2.el7.x86_64.rpm
    -rw-r--r--    1 0        0          932280 Aug 21 09:15 percona-xtrabackup-test-2.3.2-1.el7.x86_64.rpm
    -rw-r--r--    1 0        0        13568540 Aug 21 09:15 percona-xtrabackup-test-24-2.4.7-2.el7.x86_64.rpm
    -rw-r--r--    1 0        0           28236 Aug 21 09:15 percona-zabbix-templates-1.1.7-2.noarch.rpm
    lftp 172.16.0.1:/pub/Sources/7.x86_64/percona> mget percona-xtrabackup-24-2.4.7-2.el7.x86_64.rpm
    安装xtrabackup;
    [root@note2 ~]# yum install ./percona-xtrabackup-24-2.4.7-2.el7.x86_64.rpm       
    
    查看配置文件;
    [root@note2 ~]# rpm -ql percona-xtrabackup-24
    /usr/bin/innobackupex---------二次封装脚本
    /usr/bin/xbcloud--------------备份到云端的工具
    /usr/bin/xbcloud_osenv
    /usr/bin/xbcrypt--------------加密工具
    /usr/bin/xbstream-------------流式划工具
    /usr/bin/xtrabackup-----------主程序
    /usr/share/doc/percona-xtrabackup-24-2.4.7
    /usr/share/doc/percona-xtrabackup-24-2.4.7/COPYING
    /usr/share/man/man1/innobackupex.1.gz
    /usr/share/man/man1/xbcrypt.1.gz
    /usr/share/man/man1/xbstream.1.gz
    /usr/share/man/man1/xtrabackup.1.gz
    
    查看man帮助
    [root@note2 ~]# man innobackupex
    
    
    备份 全备;
    创建目录
    [root@note2 mariadb]# cd /var/lib/mysql
    [root@note2 mysql]# ls
    aria_log.00000001  ib_logfile1        mydb        note2-slow.log
    aria_log_control   master_log.000001  mysql       performance_schema
    ibdata1            master_log.000002  mysql.sock  test
    ib_logfile0        master_log.index   note2.log
    [root@note2 mysql]# mkdir /mydata/xbdata -pv
    备份;
    [root@note2 mysql]# innobackupex --user=root --host=localhost /mydata/xbdata/
    170912 20:14:28 innobackupex: Starting the backup operation
    
    IMPORTANT: Please check that the backup run completes successfully.
               At the end of a successful backup run innobackupex
               prints "completed OK!".
    备份成功后会出现completed OK!否则没有成功;
    
    生成的备份文件;
    [root@note2 mysql]# ls /mydata/xbdata/
    2017-09-12_20-14-28---此目录下有相关配置文件;
    [root@note2 mysql]# ls /mydata/xbdata/2017-09-12_20-14-28/
    backup-my.cnf  performance_schema      xtrabackup_info
    ibdata1        test                    xtrabackup_logfile
    mydb           xtrabackup_binlog_info
    mysql          xtrabackup_checkpoints
    
    
    模拟恢复数据,将mysql停掉;
    [root@note2 2017-09-12_20-14-28]# less backup-my.cnf ^C
    [root@note2 2017-09-12_20-14-28]# less xtrabackup_binlog_info ^C
    [root@note2 2017-09-12_20-14-28]# less xtrabackup_checkpoints ^C
    [root@note2 2017-09-12_20-14-28]# less xtrabackup_info ^C
    [root@note2 2017-09-12_20-14-28]# systemctl stop mariadb.service
    
    将日志文件删除;
    [root@note2 2017-09-12_20-14-28]# cd /var/lib/mysql
    [root@note2 mysql]# ls
    aria_log.00000001  ib_logfile1        mydb            performance_schema
    aria_log_control   master_log.000001  mysql           test
    ibdata1            master_log.000002  note2.log
    ib_logfile0        master_log.index   note2-slow.log
    [root@note2 mysql]# rm -rf ./*
    
    [root@note2 mysql]# cd /mydata/xbdata/
    [root@note2 xbdata]# cd /mydata/xbdata/2017-09-12_20-14-28/
    [root@note2 2017-09-12_20-14-28]# ls
    backup-my.cnf  performance_schema      xtrabackup_info
    ibdata1        test                    xtrabackup_logfile
    mydb           xtrabackup_binlog_info
    mysql          xtrabackup_checkpoints
    
    执行准备操作;
    [root@note2 2017-09-12_20-14-28]# innobackupex --apply-log ./ 
    
    InnoDB: 32 non-redo rollback segment(s) are active.
    InnoDB: Waiting for purge to start
    InnoDB: 5.7.13 started; log sequence number 2259989
    xtrabackup: starting shutdown with innodb_fast_shutdown = 1
    InnoDB: FTS optimize thread exiting.
    InnoDB: Starting shutdown...
    InnoDB: Shutdown completed; log sequence number 2260017
    170912 20:30:31 completed OK!
    
    还原;
    [root@note2 2017-09-12_20-14-28]# innobackupex --copy-back ./
    
    170912 20:33:49 [01]        ...done
    170912 20:33:49 [01] Copying ./xtrabackup_binlog_pos_innodb to /var/lib/mysql/xtrabackup_binlog_pos_innodb
    170912 20:33:49 [01]        ...done
    170912 20:33:49 [01] Copying ./ibtmp1 to /var/lib/mysql/ibtmp1
    170912 20:33:49 [01]        ...done
    170912 20:33:49 completed OK!
    
    查看恢复回来的文件;
    [root@note2 2017-09-12_20-14-28]# cd /var/lib/mysql
    [root@note2 mysql]# ls
    ibdata1      ibtmp1  performance_schema            xtrabackup_info
    ib_logfile0  mydb    test
    ib_logfile1  mysql   xtrabackup_binlog_pos_innodb
    
    [root@note2 ~]# cd /var/lib/mysql/
    [root@note2 mysql]# ll
    总用量 40976
    -rw-r-----. 1 root  root  18874368 9月  12 20:33 ibdata1
    -rw-r-----. 1 root  root   5242880 9月  12 20:33 ib_logfile0
    -rw-r-----. 1 root  root   5242880 9月  12 20:33 ib_logfile1
    -rw-r-----. 1 root  root  12582912 9月  12 20:33 ibtmp1
    -rw-rw----. 1 mysql mysql        0 9月  12 20:39 master_log.index
    drwxr-x---. 2 root  root        59 9月  12 20:33 mydb
    drwxr-x---. 2 root  root      4096 9月  12 20:33 mysql
    drwxr-x---. 2 root  root      4096 9月  12 20:33 performance_schema
    drwxr-x---. 2 root  root        20 9月  12 20:33 test
    -rw-r-----. 1 root  root        24 9月  12 20:33 xtrabackup_binlog_pos_innodb
    -rw-r-----. 1 root  root       478 9月  12 20:33 xtrabackup_info
    属者,属组需要要改为mysql;
    [root@note2 mysql]# chown -R mysql.mysql ./*
    
    启动服务查看状态;
    [root@note2 mysql]# systemctl start mariadb
    [root@note2 mysql]# systemctl status mariadb
    ● mariadb.service - MariaDB database server
       Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
       Active: active (running) since 二 2017-09-12 20:51:58 CST; 39s ago
      Process: 20637 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
      Process: 20609 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
     Main PID: 20636 (mysqld_safe)
       CGroup: /system.slice/mariadb.service
               ├─20636 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
               └─20806 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/m...
    
    9月 12 20:51:56 note2 systemd[1]: Starting MariaDB database server...
    9月 12 20:51:56 note2 mysqld_safe[20636]: 170912 20:51:56 mysqld_safe Lo....
    9月 12 20:51:56 note2 mysqld_safe[20636]: 170912 20:51:56 mysqld_safe St...l
    9月 12 20:51:58 note2 systemd[1]: Started MariaDB database server.
    Hint: Some lines were ellipsized, use -l to show in full.
    
    
    2、全量备份,增量备份,模拟故障恢复;
    在启动状态下备份;
    
    [root@note2 mysql]# innobackupex /mydata/xbdata/
    全量备份;
    
    xtrabackup: Transaction log of lsn (2260017) to (2260017) was copied.
    170912 20:57:00 completed OK!
    
    
    查看日志序列号;
    [root@note2 xbdata]# ls
    2017-09-12_20-14-28  2017-09-12_20-56-58
    
    [root@note2 xbdata]# cat /mydata/xbdata/2017-09-12_20-56-58/xtrabackup_checkpoints 
    backup_type = full-backuped
    from_lsn = 0
    to_lsn = 2260017
    last_lsn = 2260017
    compact = 0
    recover_binlog_info = 0
    
    删除学员信息,做增量备份;
    [root@note2 ~]# mysql
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 5
    Server version: 5.5.52-MariaDB MariaDB Server
    
    Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MariaDB [(none)]> use mydb
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    MariaDB [mydb]> DELETE FROM students WHERE stuid=1300;
    Query OK, 1 row affected (0.00 sec)
    
    MariaDB [mydb]> DELETE FROM students WHERE stuid=1400;
    Query OK, 1 row affected (0.00 sec)
    
    MariaDB [mydb]> DELETE FROM students WHERE stuid=1500;
    Query OK, 1 row affected (0.01 sec)
    
    MariaDB [mydb]> 
    
    增量基于谁做增量;incremental-basedir=/mydata/xbdata/2017-09-12_20-56-58/
     
    
    [root@note2 ~]# ls /mydata/xbdata/
    2017-09-12_20-14-28  2017-09-12_20-56-58
    [root@note2 ~]# innobackupex --incremental /mydata/xtrdata --incremental-basedir=/mydata/xbdata/2017-09-12_20-
    2017-09-12_20-14-28/ 2017-09-12_20-56-58/ 
    [root@note2 ~]# innobackupex --incremental /data/backups --incremental-basedir=/mydata/xbdata/2017-09-12_20-56-58/
    
    查看增量;
    [root@note2 ~]# ls /mydata/xbdata/
    2017-09-12_20-14-28  2017-09-12_20-56-58  2017-09-12_21-11-23
    
    [root@note2 2017-09-12_21-11-23]# ls
    backup-my.cnf  mysql                   xtrabackup_checkpoints
    ibdata1.delta  performance_schema      xtrabackup_info
    ibdata1.meta   test                    xtrabackup_logfile
    mydb           xtrabackup_binlog_info
    
    [root@note2 2017-09-12_21-11-23]# cat xtrabackup_checkpoints 
    backup_type = incremental
    from_lsn = 2260017
    to_lsn = 2262366
    last_lsn = 2262366
    compact = 0
    recover_binlog_info = 0
    
    
    上一个全量备份日志序列号与增量对序列号对比
    ;
    [root@note2 xbdata]# cat 2017-09-12_20-56-58/xtrabackup_checkpoints 
    backup_type = full-backuped
    from_lsn = 0
    to_lsn = 2260017
    last_lsn = 2260017
    compact = 0
    recover_binlog_info = 0
    
    在创建一个学生列表;并删除;
    MariaDB [mydb]> INSERT INTO students (name) VALUES ('JIAO DA');
    MariaDB [mydb]> DELETE FROM students WHERE stuid=3001;
    Query OK, 1 row affected (0.00 sec)
    
    
    查看最后增量;
    [root@note2 xbdata]# ls
    2017-09-12_20-14-28  2017-09-12_20-56-58  2017-09-12_21-11-23
    [root@note2 xbdata]# cat 2017-09-12_21-11-23/xtrabackup_info 
    uuid = e172b661-97bb-11e7-b798-000c2980ce95
    name = 
    tool_name = innobackupex
    tool_command = --incremental /mydata/xbdata/ --incremental-basedir=/mydata/xbdata/2017-09-12_20-56-58/
    tool_version = 2.4.7
    ibbackup_version = 2.4.7
    server_version = 5.5.52-MariaDB
    start_time = 2017-09-12 21:11:23
    end_time = 2017-09-12 21:11:25
    lock_time = 0
    binlog_pos = filename 'master_log.000001', position '830'
    innodb_from_lsn = 2260017
    innodb_to_lsn = 2262366
    partial = N
    incremental = Y
    format = file
    compact = N
    compressed = N
    encrypted = N
    [root@note2 xbdata]# mysqlbinlog -j 830 /var/lib/mysql/master_log.000001 
    /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
    /*!40019 SET @@session.max_insert_delayed_threads=0*/;
    /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
    DELIMITER /*!*/;
    # at 4
    #170912 20:51:57 server id 1  end_log_pos 245   Start: binlog v 4, server v 5.5.52-MariaDB created 170912 20:51:57 at startup
    # Warning: this binlog is either in use or was not closed properly.
    ROLLBACK/*!*/;
    BINLOG '
    bdi3WQ8BAAAA8QAAAPUAAAABAAQANS41LjUyLU1hcmlhREIAbG9nAAAAAAAAAAAAAAAAAAAAAAAA
    AAAAAAAAAAAAAAAAAABt2LdZEzgNAAgAEgAEBAQEEgAA2QAEGggAAAAICAgCAAAAAAAAAAAAAAAA
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    AAAAAAAAAAAAi0NoCA==
    '/*!*/;
    
    
    
    创建临时文件;
    [root@note2 xbdata]# mkdir /mydata/binlogs
    
    将增量内容保存;
    [root@note2 xbdata]# mysqlbinlog -j 830 /var/lib/mysql/master_log.000001 >/mydata/binlogs/2017-09-12_21-11-23-binlog
    
    
    模拟故障,停止服务;
    [root@note2 xbdata]# systemctl stop mariadb
    [root@note2 xbdata]# cd
    [root@note2 ~]# rm -rf /var/lib/mysql/*
    
    恢复;
    1、同步、重做功能;
    
    
    [root@note2 xbdata]# ls
    2017-09-12_20-14-28  2017-09-12_20-56-58  2017-09-12_21-11-23
    [root@note2 xbdata]# cd 2017-09-12_20-56-58/
    [root@note2 2017-09-12_20-56-58]# innobackupex --apply-log --redo-only  ./
    
    和并增量;
    [root@note2 2017-09-12_20-56-58]# innobackupex --apply-log --redo-only ./ --incremental-dir=/mydata/xbdata/2017-09-12_21-11-23/
    
    
    检查全量备份有无改动;
    [root@note2 2017-09-12_20-56-58]# cat xtrabackup_checkpoints 
    backup_type = log-applied
    from_lsn = 0
    to_lsn = 2262366
    last_lsn = 2262366
    compact = 0
    recover_binlog_info = 0
    
    最后回滚;
    [root@note2 2017-09-12_20-56-58]# innobackupex --apply-log ./
    
    InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
    InnoDB: File './ibtmp1' size is now 12 MB.
    InnoDB: 96 redo rollback segment(s) found. 1 redo rollback segment(s) are active.
    InnoDB: 32 non-redo rollback segment(s) are active.
    InnoDB: Waiting for purge to start
    InnoDB: 5.7.13 started; log sequence number 2262577
    xtrabackup: starting shutdown with innodb_fast_shutdown = 1
    InnoDB: FTS optimize thread exiting.
    InnoDB: Starting shutdown...
    InnoDB: Shutdown completed; log sequence number 2262596
    170912 22:18:20 completed OK!
    
    恢复
    [root@note2 2017-09-12_20-56-58]# innobackupex --copy-back ./
    170912 22:21:24 [01]        ...done
    170912 22:21:24 [01] Copying ./xtrabackup_binlog_pos_innodb to /var/lib/mysql/xtrabackup_binlog_pos_innodb
    170912 22:21:24 [01]        ...done
    170912 22:21:24 [01] Copying ./xtrabackup_info to /var/lib/mysql/xtrabackup_info
    170912 22:21:24 [01]        ...done
    170912 22:21:24 [01] Copying ./ibtmp1 to /var/lib/mysql/ibtmp1
    170912 22:21:24 [01]        ...done
    170912 22:21:24 completed OK!
    
    查看文件所属;
    [root@note2 2017-09-12_20-56-58]# cd /var/lib/mysql
    [root@note2 mysql]# ll
    总用量 40976
    -rw-r-----. 1 root root 18874368 9月  12 22:21 ibdata1
    -rw-r-----. 1 root root  5242880 9月  12 22:21 ib_logfile0
    -rw-r-----. 1 root root  5242880 9月  12 22:21 ib_logfile1
    -rw-r-----. 1 root root 12582912 9月  12 22:21 ibtmp1
    drwxr-x---. 2 root root       59 9月  12 22:21 mydb
    drwxr-x---. 2 root root     4096 9月  12 22:21 mysql
    drwxr-x---. 2 root root     4096 9月  12 22:21 performance_schema
    drwxr-x---. 2 root root       20 9月  12 22:21 test
    -rw-r-----. 1 root root       24 9月  12 22:21 xtrabackup_binlog_pos_innodb
    -rw-r-----. 1 root root      527 9月  12 22:21 xtrabackup_info
    
    
    更改所属者,所属组;
    [root@note2 mysql]# chown -R mysql.mysql ./*
    [root@note2 mysql]# ll
    总用量 40976
    -rw-r-----. 1 mysql mysql 18874368 9月  12 22:21 ibdata1
    -rw-r-----. 1 mysql mysql  5242880 9月  12 22:21 ib_logfile0
    -rw-r-----. 1 mysql mysql  5242880 9月  12 22:21 ib_logfile1
    -rw-r-----. 1 mysql mysql 12582912 9月  12 22:21 ibtmp1
    drwxr-x---. 2 mysql mysql       59 9月  12 22:21 mydb
    drwxr-x---. 2 mysql mysql     4096 9月  12 22:21 mysql
    drwxr-x---. 2 mysql mysql     4096 9月  12 22:21 performance_schema
    drwxr-x---. 2 mysql mysql       20 9月  12 22:21 test
    -rw-r-----. 1 mysql mysql       24 9月  12 22:21 xtrabackup_binlog_pos_innodb
    -rw-r-----. 1 mysql mysql      527 9月  12 22:21 xtrabackup_info
    
    启动服务;
    [root@note2 mysql]# systemctl start mariadb
    [root@note2 mysql]# mysql
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 2
    Server version: 5.5.52-MariaDB MariaDB Server
    
    Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MariaDB [(none)]> use mydb
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    MariaDB [mydb]> SELECT * FROM students WHERE stuid=1200;
    +-------+---------+------+--------+-----------+
    | stuid | name    | age  | gender | major     |
    +-------+---------+------+--------+-----------+
    |  1200 | stu1200 |   74 | M      | major1200 |
    +-------+---------+------+--------+-----------+
    1 row in set (0.00 sec)
    
    MariaDB [mydb]> SELECT * FROM students WHERE stuid=1300;
    Empty set (0.00 sec)
    
    MariaDB [mydb]> SELECT * FROM students WHERE stuid=1400;
    Empty set (0.00 sec)
    
    
    关掉二进制日志;
    MariaDB [mydb]> SET @@session.sql_log_bin=OFF;
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [mydb]> \. /mydata/binlogs/2017-09-12_21-11-23-binlog
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Charset changed
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Database changed
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 1 row affected (0.00 sec)
    
    Query OK, 0 rows affected (0.01 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 1 row affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    Query OK, 0 rows affected (0.00 sec)
    
    MariaDB [mydb]> SET @@session.sql_log_bin=ON;
    Query OK, 0 rows affected (0.00 sec)
    
    在做一次全量备份;
    
    innobackupex --user=DBUSER --password=DBUSERPASS /PATH/TO/BACKUP-DIR/

    相关文章

      网友评论

          本文标题:Xtrabackup对Mysql备份及恢复数据

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