MySQL database backup + restore

mysql backup and restore

  • 1. Physical cold backup and recovery

1. Prepare the database and data

mysql> create database t325;

mysql> use t325

mysql> create table biao1 (id int,name char(10));

mysql> insert into biao1 values (1,'zhang1');

mysql> insert into biao1 values (2,'zhang2');

mysql> select * from biao1;

mysql> exit

2. Cold backup database

[root@localhost ~]# cd /

[root@localhost /]# mkdir backup

[root@localhost /]# /etc/init.d/mysqld stop

[root@localhost /]# tar -zcf /backup/t325_all-$(date +%F).tar.gz /usr/local/mysql/data/

[root@localhost ~]# ll /backup/
总用量 736
-rw-r--r--. 1 root root 752674 1124 14:47 t325_all-2020-11-24.tar.gz

Analysis:

Linux system time in the shell can directly call system variables such as: Get Today periods: date +%Y%m%dor date +%For $ (date +% y% m % d)

[root@localhost ~]# tar -zcvf /backup/t325-(date+%F).tar.gz /usr/local/mysql/data #It can be seen from here, (in the bash script. $ from a variable Start.)
bash: Unexpected syntax error near symbol `('

[root@localhost ~]# date +%F
bash: 2020-11-25: Command not found...

3. Restore cold backup data

[root@localhost /]# /etc/init.d/mysqld stop(关闭情况下可省略这一步)

[root@localhost /]# mkdir bak                                #创建用于恢复的目录

[root@localhost /]# mv /usr/local/mysql/data/   /bak/        #将存放数据库的“主配置文件”移动到bak目录下,模拟故障!!!    …mysql目录下不只有data目录

[root@localhost /]# /etc/init.d/mysqld start
报错,数据库出错
[root@localhost /]# mysql -u root -p123.com
无法登陆

[root@localhost ~]# tar -zxvf /backup/t325_all-2020-11-24.tar.gz -C /tmp/    #不能直接解压到“/usr/local/mysql目录”,因为它打包的是“/usr/local/mysql/data”目录
[root@localhost tmp]# mv /tmp/usr/local/mysql/data/ /usr/local/mysql/

[root@localhost /]# /etc/init.d/mysqld start
启动正常{
    
    Starting MySQL.. SUCCESS! }
  • Two, mysqldump backup and recovery

1. mysqldump backs up the tables in the database

mysqldump -u username -p password library name table name> save path/file name.sql #Export the tables in the specified library as SQL scripts (so .sql cannot be changed)

[root@localhost /]# mysqldump -u root -p123.com t325 biao1 > /bak/t325-biao1.sql  {其实不加dump也行}
但不加dump的后果:
[root@localhost ~]# mysql -u root -p123.com t325 </bak/t325db.sql
Warning: Using a password on the command line interface can be insecure.
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your 
MySQL server version for the right syntax to use near 'mysql  Ver 14.14 Distrib 5.6.36, for Linux (x86_64) using 
EditLine wrapper
Copy' at line 1

2. Backup database {must add dump}

[root@localhost /]# mysqldump -u root -p123.com --databases t325 > /bak/t325db.sql

3. Back up all databases

[root@localhost /]# mysqldump -u root -p123.com --all-databases > /bak/all-db.sql

4. View the contents of the backup file

[root@localhost /]# grep -v "^--" /bak/abcdb.sql | grep -v "^/" |grep -v "^$"         {
    
    --:解释说明;   /:查找说明;      ^$:空行}

5. Restore the backup

[1] Simulate to modify the table data:
mysql> update biao1 set name='zhang3' where id=2;
two methods to restore:

1.[root@localhost /]# mysql -u root -p123.com  t325< /bak/t325db.sql
2.[root@localhost ~]# mysql -u root -p123.com t325 </bak/t325-biao1.sql
如果你不加t325:
将会[root@localhost ~]# mysql -u root -p123.com </bak/t325-biao1.sql
Warning: Using a password on the command line interface can be insecure.
ERROR 1046 (3D000) at line 22: No database selected

[root@localhost /]# mysql -u root -p123.com -e 'show tables from t325'          //只能看到表t325,而无法看到其中数据!
[root@localhost ~]# mysql -u root -p123.com -e 'update t325.biao1 set name='zhang3' where id=2;'            //不能在命令行中修改表中数据

[2] Simulate to delete the database, and then restore:

mysql> drop database t325;
Query OK, 1 row affected (0.09 sec)
[root@localhost /]# mysql -u root -p123.com  t325< /bak/t325db.sql                // 他会报错
[root@localhost /]# mysql -u root -p123.com  < /bak/t325db.sql                    //正确写法!!!

[root@localhost /]# mysql -u root -p123.com -e 'show databases; '                //查看MySQL数据库中有那些数据库~
  • 3. Incremental backup/restore case

Incremental backup is implemented by a binary log mysqlbinlog. Once the log is opened, every user's statement except select will be recorded in the log.

1. Configure the mysql configuration file my.cnf, and turn on the binlog log function~ and set the log file path/name

[root@localhost ~]# vim /etc/my.cnf or vim /usr/local/mysql/my.cnf
add:

log-bin=/usr/local/mysql/mysql-bin                         //指定日志文件存储位置+名字!!
解析:“log-bin=”后的字符串为日志记载目录,一般建议放在不同于MySQL数据目录的磁盘上

[root@localhost ~]# /etc/init.d/mysqld stop
[root@localhost ~]# /etc/init.d/mysqld start               //必须重启服务,否则找不到日志文件存放目录!!!
以上两步可以和为一步:
[root@localhost mysql]# systemctl restart mysqld

[root@localhost ~]# ll /usr/local/mysql/ //Check whether the log file is effective

My computer can’t restart after the above steps:
my way

log-bin=qw                                                //它这个保存的名字随便,但是他会在data下显示日志文件~    {
    
    还有就是 只添加log_bin也行,但是默认名称为主机名}
[root@localhost ~]# ll /usr/local/mysql/data/             {
    
    里面一开始看不到qwer相关的文件!!!}
重启完之后发现
[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# ll /usr/local/mysql/data/             #发现多出来这两个文件
-rw-rw----. 1 mysql mysql      120 11月 24 19:50 qwer.000001                  #注意:这里的000001是准备开始记录接下来的“操作”,直到第二次“刷新”或“重启服务”才会截止
-rw-rw----. 1 mysql mysql       14 11月 24 19:50 qwer.index

2. Create a database, create a table, add data

[root@localhost ~]# mysql -u root -p123.com

mysql> create database abc;
mysql> use abc;
mysql> create table user_info (id char(20),name char(20),sex char(4),user_id char(10),xiaofei int);

mysql> insert into user_info values ('a001','zhang1','M','0001',120),('a002','zhang2','W','0002',100),
('a003','zhang3','M','0003',90),
('a004','zhang4','W','0004',160),
('a005','zhang5','M','0005',30),
('a006','zhang6','W','0006',80);

mysql> select * from user_info;

3. Make a full backup first

[root@localhost ~]# mkdir /bak
[root@localhost ~]# mysqldump -u root -p123.com abc user_info > /bak/abc_user_inf-$(date +%F).sql       
                                                                    -$(date +%F):会显示当前的时间
[root@localhost ~]# ll /bak
生成新的二进制文件
[root@localhost ~]# mysqladmin -uroot -p123.com flush-logs              
//为了让日志文件同步(即到此qwer.000001结束,但是你查看存放目录时会发现有000002文件{
    
    它是记录接下来的操作}

4. Add new data

[root@localhost ~]# mysql -u root -p123.com

mysql> use abc;

mysql>  insert into user_info values ('a007','zhang7','M','0007',120),('a008','zhang8','W','0008',100);

[root@localhost ~]# mysqladmin -uroot -p123.com flush-logs                   //为了让日志文件同步{即新添加的数据保存到>>qwer.000002日志文件中了}

or

mysql> flush logs;
Query OK, 0 rows affected (0.01 sec)

[root@localhost ~]# ll /usr/local/mysql/

5. Copy the log file
[root@localhost ~]# cp /usr/local/mysql/mysql-bin.000002 /bak # Copy the source log file to /bak, for easier recovery! ! !

Delete table

[root@localhost ~]# mysql -u root -p123.com -e "drop table abc.user_info"

View effect

[root@localhost ~]# mysql -u root -p123.com -e "show tables from abc"

6. Restore the tables and data in the database

Perform a full recovery:

[root@localhost ~]# mysql -u root -p123.com abc < /bak/abc_user_inf-2018-05-04.sql 

[root@localhost ~]# mysql -u root -p123.com -e "show tables from abc"                //查看abc库中有哪些表

[root@localhost ~]# mysql -u root -p123.com -e "select * from abc.user_info"        //查看abc库里面的user_info表中的数据

Data added after missing

[root@localhost ~]# mysqlbinlog --no-defaults /bak/mysql-bin.000002 | mysql -u root -p123.com // "Restore" the 000002 log file to the database

[root@localhost ~]# mysql -u root -p123.com -e "select * from abc.user_info"
data recovery is successful! ! !

View log file information:
[root@localhost ~]# mysqlbinlog --no-defaults /bak/mysql-bin.000002 Find the at keyword, followed by id

  • Location-based restoration {Position: location}

Recovery format:
mysqlbinlog --no-defaults --start/end-position='ID number followed by at'/backup file path/backup file name | mysql -uroot -p password
[1] from the specified location in the log Start recovery:

[root@localhost ~]# mysqlbinlog --no-defaults --start-position='1906' /bak/mysql-bin.000003 | mysql -uroot -p123.com
--start-position:起始恢复id 

[2] Recover from "an end position" in the content of the specified log file

[root@localhost ~]# mysqlbinlog --no-defaults --stop-position='1906' /bak/mysql-bin.000003 | mysql -uroot -p123.com
解析:
--stop-position:结束id
BEGIN
结束id必须是这里面的最后一个at任务所位于的那个id,才能体现出效果
下一个BEGIN

[3] Data between two positions in the recovery log:

[root@localhost ~]# mysqlbinlog --no-defaults --start-position='1906' --stop-position='2315' /bak/mysql-bin.000003 | mysql -uroot -p123.com
这里的ID则不需要最后一个!

  • Restore based on point in time {datetime: date time}

Recovery format:
mysqlbinlog --no-defaults --start/end-datetime='at the time' /backup file path/backup file name | mysql -uroot -p password

【1】Restore the data after the specified start time:

root@localhost ~]# mysqlbinlog --no-defaults --start-datetime=‘2018-5-4 17:27:00’ /bak/mysql-bin.000003 | mysql -uroot -p123.com

[2] Restore the data before the specified time:

[root@localhost ~]# mysqlbinlog --no-defaults --stop-datetime=‘2018-5-4 17:27:00’ /bak/mysql-bin.000003 | mysql -uroot -p123.com

解析:
--stop-datetime:结束时间
BEGIN
结束时间必须是这里面的最后一个at任务所位于的那个时间点,才可以生效
下一个BEGIN

【3】Restore the data in the specified time range:

[root@localhost ~]# mysqlbinlog --no-defaults --start-datetime=‘2018-5-4 17:27:00’ --stop-datetime=‘2018-5-5 17:27:00’ /bak/mysql-bin.000003 | mysql -uroot -p123.com

It can be seen from here: the
time point cannot be based on the number of the at task [otherwise the time will be the same], but based on the last date and time between begin and the next begin

[4] Restore the data from the specified time to the specified location:

[root@localhost ~]# mysqlbinlog --no-defaults --start-datetime=‘2018-5-4 17:27:00’ --stop-position=‘2315’ /bak/mysql-bin.000003 | mysql -uroot -p123.com

The end position is specified as the number id of the last at task [between begin~next BEGIN]

to sum up:

1.只要是结束位置,必须是【BEGIN~下一个BEGIN之间的】最后一个“时间”或“at任务”的编号!!!
2.数据库基于***的恢复,其实它不是恢复!而是将“你所指定的那个范围内的命令语句”再次执行一下!!!
3.当你把表删除后,自然就不能恢复“表中的数据”了!!!

Your encouragement is my motivation for continuous improvement (#^ . ^#)

Insert picture description here

Build a LAMP environment

Guess you like

Origin blog.csdn.net/qq_50573146/article/details/110149251