Mysql official online backup and data recovery

We start from the critical point;

Foreign words: here we choose ext4 file format, xfs file system lv, snapshots can not be mounted at the same time;

   Snapshot backup advantages and disadvantages:

          Advantage: online backup, the backup fast speed;

          Disadvantages: lock the table is not controllable, the snapshot space must be greater than the size of the data to be backed up;

1. Lock Mysql database

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.01 sec)

2. Create a snapshot lv

[root@logan ~]# lvcreate -L 500M -s -n /dev/vg0/lv_sql /dev/vg0/lv0
  Logical volume "lv_sql" created.

3. Mount lv Snapshot

[root@logan ~]# mount /dev/vg0/lv_sql /opt/

4. here we can unlock the database

mysql> unlock tables;
Query OK, 0 rows affected (0.37 sec)

The packaged good backup data compression

[root@logan ~]# tar -cjvf /backup/mysql.tar.bz2 /opt/*
[root@logan backup]# ls
all.sql  master.000001  master.index  mysql.tar.bz2

6. After a good backup data, we can delete the snapshot

[root@logan backup]# lvremove /dev/vg0/lv_sql 
Do you really want to remove active logical volume vg0/lv_sql? [y/n]: y
  Logical volume "lv_sql" successfully removed

So far our lv snapshot backup data is complete

============================================================================================================================================================

Now we start the simulation data loss

[root@logan mysql]# cd data/
[root@logan data]# ls
auto.cnf ibdata1 ibtmp1 MySQL upup
file            ib_logfile0  logan.liunx.com.err  performance_schema
ib_buffer_pool  ib_logfile1  logan.liunx.com.pid  sys
[root@logan data]# rm -rf ./*

1. recovery

Mysql service stopped

[root@logan data]# killall mysqld

 

We need to unpack the archive

[root@logan backup]# tar -xvf mysql.tar.bz2 -C /tmp/
[root@logan opt]# cp -r ./* /usr/local/mysql/data/

2. We need to change cp over the file owner, is a group

[root@logan mysql]# chown -R mysql.mysql data/
[root@logan mysql]# ll data/
总用量 110640
-rw-r----- 1 mysql mysql       56 7月   4 22:49 auto.cnf
drwxr-x--- 2 mysql mysql     4096 7月   4 22:49 file
-rw-r----- 1 mysql mysql      433 7月   4 22:51 ib_buffer_pool
-rw-r----- 1 mysql mysql 12582912 7月   4 22:49 ibdata1
-rw-r----- 1 mysql mysql 50331648 7月   4 22:49 ib_logfile0
-rw-r----- 1 mysql mysql 50331648 7月   4 22:49 ib_logfile1
-rw-r----- 1 mysql mysql    11042 7月   4 22:49 logan.liunx.com.err
drwxr-x--- 2 mysql mysql     4096 7月   4 22:49 mysql
drwxr-x--- 2 mysql mysql     4096 7月   4 22:49 performance_schema
drwxr-x--- 2 mysql mysql    12288 7月   4 22:49 sys
Drwxr -X --- 2 Mysql Mysql      4096 7 May    4  22 : 49 Upup

3. Restart Mysql service

[root@logan mysql]# systemctl restart mysqldd

4. Log in to view database data

[root@logan mysql]# systemctl restart mysqldd
[root@logan mysql]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25-log Source distribution

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| file               |
| mysql              |
| performance_schema |
| sys                |
| upup |
+--------------------+
6 rows in set (0.00 sec)

mysql> 
mysql> use upup;
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
mysql> show tables;
+----------------+
| Tables_in_upup |
+----------------+
| home           |
| t1 |
+----------------+
2 rows in set (0.00 sec)

mysql> select * from t1;
+------+-------+------+
| id   | name  | home |
+------+-------+------+
|    1 | robin |  100 |
.    2 | sdaa |  200 |
|    3 | dfdaa |  100 |
+------+-------+------+
3 rows in set (0.00 sec)

mysql> 

Data recovery is complete

Guess you like

Origin www.cnblogs.com/loganSxb/p/11135468.html