42-2 mysql备份与恢复

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了42-2 mysql备份与恢复相关的知识,希望对你有一定的参考价值。

02 mysql备份与恢复


实战:percona-xtrabackup备份还原数据库

原服务器:source 192.168.1.133 CentOS7.2 

备份服务器:restore 192.168.1.132 CentOS7.2


1、使用indobackupex进行完全备份

[[email protected] ~]# yum install percona-xtrabackup-2.3.2-1.el7.x86_64.rpm

[[email protected] ~]# rpm -ql percona-xtrabackup


MariaDB [(none)]> use hellodb;

MariaDB [hellodb]> show table status \G

*************************** 1. row ***************************

  Name: classes

Engine: InnoDB

Version: 10

Row_format: Compact

  Rows: 8

Avg_row_length: 2048

Data_length: 16384

Max_data_length: 0

  Index_length: 0

 Data_free: 9437184

Auto_increment: 9

Create_time: 2016-11-19 22:01:43

Update_time: NULL

Check_time: NULL

 Collation: utf8_general_ci

  Checksum: NULL

Create_options: 

Comment: 

确保Engine为: InnoDB

[[email protected] ~]# systemctl stop mariadb

[[email protected] ~]# rm -rf /data/mysql/*

[[email protected] ~]# rm -rf /data/binlogs/*

[[email protected] ~]# vim /etc/my.cnf

添加

innodb_file_per_table=ON

[[email protected] ~]# systemctl start mariadb.service

[[email protected] ~]# mysql < all.sql 

[[email protected] ~]# ls /data/mysql/

aria_log.00000001  hellodb  ib_logfile0  mydb   performance_schema

aria_log_control   ibdata1  ib_logfile1  mysql  test

[[email protected] ~]# ls /data/mysql/hellodb/

classes.frm  coc.ibd      db.opt      students.frm  teachers.ibd

classes.ibd  courses.frm  scores.frm  students.ibd  toc.frm

coc.frm      courses.ibd  scores.ibd  teachers.frm  toc.ibd

[[email protected] ~]# mkdir /backups #创建备份目录

MariaDB [(none)]> SHOW BINARY LOGS;

+------------------+-----------+

| Log_name         | File_size |

+------------------+-----------+

| mysql-bin.000001 |     30334 |

| mysql-bin.000002 |   1038814 |

| mysql-bin.000003 |    523472 |

+------------------+-----------+


[[email protected] ~]# innobackupex --user=root /backups/

[[email protected] ~]# ls /backups/2016-11-20_12-04-51/

backup-my.cnf  mydb                test                    xtrabackup_info

hellodb        mysql               xtrabackup_binlog_info  xtrabackup_logfile

ibdata1        performance_schema  xtrabackup_checkpoints


[[email protected] ~]# systemctl stop mariadb.service

[[email protected] ~]# vim /etc/my.cnf

修改

datadir=/var/lib/mysql

datadir=/data/mysql

添加

innodb_file_per_table=ON

[[email protected] ~]# mkdir -p /data/mysql

[[email protected] ~]# chown mysql.mysql /data/mysql/

[[email protected] ~]# systemctl start mariadb.service

[[email protected] ~]# yum -y install percona-xtrabackup-2.3.2-1.el7.x86_64.rpm

[[email protected] ~]# scp -r /backups/2016-11-20_12-04-51/ 192.168.1.132:/root

[[email protected] ~]# mkdir /backups

[[email protected] ~]# mv 2016-11-20_12-04-51/ /backups/

#整理

[[email protected] ~]# innobackupex --apply-log /backups/2016-11-20_12-04-51/

[[email protected] ~]# systemctl stop mariadb.service 

[[email protected] ~]# rm -rf /data/mysql/*

[[email protected] ~]# innobackupex --copy-back /backups/2016-11-20_12-04-51/

[[email protected] ~]# chown -R mysql.mysql /data/mysql/*

[[email protected] ~]# rm -f /data/mysql/ib_logfile*

[[email protected] ~]# systemctl start mariadb.service

MariaDB [(none)]> SHOW DATABASES;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| hellodb            |

| mydb               |

| mysql              |

| performance_schema |

| test               |

+--------------------+

MariaDB [(none)]> USE hellodb;

MariaDB [hellodb]> SHOW TABLES;

+-------------------+

| Tables_in_hellodb |

+-------------------+

| classes           |

| coc               |

| courses           |

| scores            |

| students          |

| teachers          |

| toc               |

+-------------------+

MariaDB [hellodb]> SELECT * FROM students;

+-------+---------------+-----+--------+---------+-----------+

| StuID | Name          | Age | Gender | ClassID | TeacherID |

+-------+---------------+-----+--------+---------+-----------+

|     1 | Shi Zhongyu   |  22 | M      |       2 |         3 |

|     2 | Shi Potian    |  22 | M      |       1 |         7 |

|     3 | Xie Yanke     |  53 | M      |       2 |        16 |

|     4 | Ding Dian     |  32 | M      |       4 |         4 |

|     5 | Yu Yutong     |  26 | M      |       3 |         1 |

|     6 | Shi Qing      |  46 | M      |       5 |      NULL |

|     7 | Xi Ren        |  19 | F      |       3 |      NULL |

|     8 | Lin Daiyu     |  17 | F      |       7 |      NULL |

|     9 | Ren Yingying  |  20 | F      |       6 |      NULL |

|    10 | Yue Lingshan  |  19 | F      |       3 |      NULL |

|    11 | Yuan Chengzhi |  23 | M      |       6 |      NULL |

|    12 | Wen Qingqing  |  19 | F      |       1 |      NULL |

|    13 | Tian Boguang  |  33 | M      |       2 |      NULL |

|    14 | Lu Wushuang   |  17 | F      |       3 |      NULL |

|    15 | Duan Yu       |  19 | M      |       4 |      NULL |

|    16 | Xu Zhu        |  21 | M      |       1 |      NULL |

|    17 | Lin Chong     |  25 | M      |       4 |      NULL |

|    18 | Hua Rong      |  23 | M      |       7 |      NULL |

|    19 | Xue Baochai   |  18 | F      |       6 |      NULL |

|    20 | Diao Chan     |  19 | F      |       7 |      NULL |

|    21 | Huang Yueying |  22 | F      |       6 |      NULL |

|    22 | Xiao Qiao     |  20 | F      |       1 |      NULL |

|    23 | Ma Chao       |  23 | M      |       4 |      NULL |

|    24 | Xu Xian       |  27 | M      |    NULL |      NULL |

|    25 | Sun Dasheng   | 100 | M      |    NULL |      NULL |

+-------+---------------+-----+--------+---------+-----------+

2、使用innodbbackupex进行增量备份

[[email protected] ~]# mysq

MariaDB [(none)]> use hellodb;

MariaDB [hellodb]> CREATE TABLE testtb (id int);

MariaDB [hellodb]> INSERT INTO testtb VALUES (1),(10),(99);

MariaDB [hellodb]> SELECT * FROM testtb;

+------+

| id   |

+------+

|    1 |

|   10 |

|   99 |

+------+

MariaDB [hellodb]> \q

#进行完全备份

[[email protected] ~]# innobackupex /backups/

[[email protected] ~]# ls /backups/

2016-11-20_12-55-15

[[email protected] ~]# mysql

MariaDB [(none)]> use hellodb;

MariaDB [hellodb]> SHOW TABLES;

+-------------------+

| Tables_in_hellodb |

+-------------------+

| classes           |

| coc               |

| courses           |

| scores            |

| students          |

| teachers          |

| testtb            |

| toc               |

+-------------------+

MariaDB [hellodb]> DROP TABLE coc;

MariaDB [hellodb]> INSERT INTO testtb VALUES (44),(32);

MariaDB [hellodb]> \q

#增量备份

[[email protected] ~]# innobackupex --incremental /backups/ --incremental-base=/backups/2016-11-20_12-55-15

#查看备份结果

[[email protected] ~]# less /backups/2016-11-20_13-08-46/xtrabackup_checkpoints 

backup_type = incremental

from_lsn = 1655411

to_lsn = 1658073

last_lsn = 1658073

compact = 0

recover_binlog_info = 0

[[email protected] ~]# systemctl stop mariadb.service 


#整理完全备份

[[email protected] ~]# innobackupex --apply-log --redo-only /backups/2016-11-20_12-55-15/

#整理增量备份并合并入原来的完全备份

[[email protected] ~]# innobackupex --apply-log --redo-only /backups/2016-11-20_12-55-15/ --incremental-dir=/backups/2016-11-20_13-08-46

#删除原来数据库

[[email protected] ~]# rm -rf /data/mysql/*

[[email protected] ~]# rm -rf /data/binlogs/*

#用刚才的备份还原数据库

[[email protected] ~]# innobackupex --copy-back /backups/2016-11-20_12-55-15/

[[email protected] ~]# cd /data/mysql/

[[email protected] mysql]# ll -h

total 115M

drwx------ 2 root root 4.0K Nov 20 16:38 hellodb

-rw-r----- 1 root root  18M Nov 20 16:38 ibdata1

-rw-r----- 1 root root  48M Nov 20 16:38 ib_logfile0

-rw-r----- 1 root root  48M Nov 20 16:38 ib_logfile1

drwx------ 2 root root 4.0K Nov 20 16:38 mydb

drwx------ 2 root root 4.0K Nov 20 16:38 mysql

drwx------ 2 root root 4.0K Nov 20 16:38 performance_schema

drwx------ 2 root root 4.0K Nov 20 16:38 test

-rw-r----- 1 root root   38 Nov 20 16:38 xtrabackup_binlog_pos_innodb

-rw-r----- 1 root root  445 Nov 20 16:38 xtrabackup_info

[[email protected] mysql]# chown -R mysql.mysql ./*

[[email protected] mysql]# systemctl start mariadb.service 

[[email protected] mysql]# mysql

MariaDB [(none)]> USE hellodb;

MariaDB [hellodb]> SELECT * FROM testtb;

+------+

| id   |

+------+

|    1 |

|   10 |

|   99 |

|   44 |

|   32 |

+------+

3、导出单张表

#完全备份

[[email protected] mysql]# innobackupex /backups/

[[email protected] mysql]# innobackupex --apply-log --export /backups/2016-11-20_16-47-57/

[[email protected] mysql]# cd /backups/2016-11-20_16-47-57/

[[email protected] 2016-11-20_16-47-57]# cd hellodb/

[[email protected] hellodb]# ls

classes.cfg  courses.exp  scores.exp    students.frm  teachers.ibd  toc.cfg

classes.exp  courses.frm  scores.frm    students.ibd  testtb.cfg    toc.exp

classes.frm  courses.ibd  scores.ibd    teachers.cfg  testtb.exp    toc.frm

classes.ibd  db.opt       students.cfg  teachers.exp  testtb.frm    toc.ibd

courses.cfg  scores.cfg   students.exp  teachers.frm  testtb.ibd

[[email protected] ~]# mysql

MariaDB [(none)]> use mydb;

MariaDB [mydb]> SHOW TABLES;

+----------------+

| Tables_in_mydb |

+----------------+

| t1             |

| t2             |

| tb1            |

+----------------+

MariaDB [mydb]> CREATE TABLE `students` (   `StuID` int(10) unsigned NOT NULL AUTO_INCREMENT,   `Name` varchar(50) NOT NULL,   `Age` tinyint(3) unsigned NOT NULL,   `Gender` enum(‘F‘,‘M‘) NOT NULL,   `ClassID` tinyint(3) unsigned DEFAULT NULL,   `TeacherID` int(10) unsigned DEFAULT NULL,   PRIMARY KEY (`StuID`) ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8;

MariaDB [mydb]> desc students;

+-----------+---------------------+------+-----+---------+----------------+

| Field     | Type                | Null | Key | Default | Extra          |

+-----------+---------------------+------+-----+---------+----------------+

| StuID     | int(10) unsigned    | NO   | PRI | NULL    | auto_increment |

| Name      | varchar(50)         | NO   |     | NULL    |                |

| Age       | tinyint(3) unsigned | NO   |     | NULL    |                |

| Gender    | enum(‘F‘,‘M‘)       | NO   |     | NULL    |                |

| ClassID   | tinyint(3) unsigned | YES  |     | NULL    |                |

| TeacherID | int(10) unsigned    | YES  |     | NULL    |                |

+-----------+---------------------+------+-----+---------+----------------+

MariaDB [mydb]> ALTER TABLE students DISCARD TABLESPACE;

MariaDB [mydb]> \q

Bye

[[email protected] ~]# cd /data/mysql/

[[email protected] mysql]# ls

aria_log.00000001  ib_logfile0  performance_schema

aria_log_control   ib_logfile1  test

hellodb            mydb         xtrabackup_binlog_pos_innodb

ibdata1            mysql        xtrabackup_info

[[email protected] mysql]# cd mydb/

[[email protected] mydb]# ls

db.opt  students.frm  t1.frm  t1.ibd  t2.frm  t2.ibd  tb1.frm  tb1.ibd

[[email protected] ~]# cd /backups/2016-11-20_16-47-57/hellodb/

[[email protected] hellodb]# ls

classes.cfg  courses.exp  scores.exp    students.frm  teachers.ibd  toc.cfg

classes.exp  courses.frm  scores.frm    students.ibd  testtb.cfg    toc.exp

classes.frm  courses.ibd  scores.ibd    teachers.cfg  testtb.exp    toc.frm

classes.ibd  db.opt       students.cfg  teachers.exp  testtb.frm    toc.ibd

courses.cfg  scores.cfg   students.exp  teachers.frm  testtb.ibd

[[email protected] hellodb]# scp students.exp students.ibd 192.168.1.132:/data/mysql/mydb/

[[email protected] mydb]# chown mysql.mysql students.*

测试结果:数据导出成功,导入不成功!


本文出自 “追梦” 博客,请务必保留此出处http://sihua.blog.51cto.com/377227/1875524

以上是关于42-2 mysql备份与恢复的主要内容,如果未能解决你的问题,请参考以下文章

Mysql 备份恢复与 xtrabackup备份

MySQL备份与恢复

Mysql备份与恢复

MySQL备份与恢复

MySQL备份与恢复

MySQL增量备份与恢复