Article Directory
database backup
In a production environment, data security is of paramount importance, and any loss of data may have serious consequences
Reasons for data loss
Program error
Human error (most of the reasons)
Computer failure,
Disk failure,
Disaster (such as fire, earthquake) and theft
Back up copywriting from a physical and logical perspective
Physical backup: backup of the physical files (such as data files, log files, etc.) of the database operating system. The
database is divided into three types of files at the physical level. The name of each file starts with the name of the table, and the extension indicates the file type
table structure file :. Frm file storage table definition
The extension of the table data file is .MYD (MYData)
The extension of the table index file is .MYI (MYIndex)
physical backup can be divided into offline backup (cold backup) and online backup (hot backup) )
Cold backup: it is performed when the database is closed.
Hot backup: the database is running. This backup method depends on the log file of the database.
Logical backup: the backup of the database logical components (such as tables and other database objects)
Database backup strategy
Full backup: make a complete backup of the data each time.
Differential backup: back up those files that have been modified since the last full backup.
Incremental backup: Only those files that have been modified after the last full backup or incremental backup will be Backup
Full backup
A full backup is a backup of the entire database, database structure and file structure. A
full backup saves the database at the time the
backup is completed. A full backup is the basis of incremental backup.
Pros and cons of full backup
Advantages:
high security,
simple and convenient backup and recovery operations.
Disadvantages:
a large amount of data repetition,
taking up a lot of backup space, low space utilization,
long backup and recovery time
bring it on! Show!
Back up files directly
[root@5centos mysql]# tar jcvf /beifen/bk-$(date +%F).tar.gz data
[root@5centos mysql]# ls /beifen/
bk-2020-08-20.tar.gz bk-.tar.gz
Use mysqldump
备份单个库(实际上是备份库里的所有表)
[root@5centos /]# mysqldump -u root -p LIU > /beifen/JUEJUE.sql
Enter password:
多库备份
[root@5centos beifen]# mysqldump -u root -p --databases LIU PAN > /beifen/databases-LIU_PAN.sql
Enter password:
[root@5centos beifen]# ls
databases-LIU_PAN.sql JUEJUE.sql
备份所有库
[root@5centos beifen]# mysqldump -u root -p --opt --all-databases > /beifen/all_databases.sql
Enter password:
备份指定表
[root@5centos beifen]# mysqldump -u root -p LIU SHUSHU > /beifen/LIU-SHUSHU.sql
Enter password:
Restore the database
There are two ways to restore the database:
one source
two mysql -u username -p <backup script
mysql -u root -p mysql < /beifen/JUEJUE.sql
Incremental backup
Only those files that have been modified
since the last full backup or incremental backup will be backed up. Incremental backups are the files or contents that have been added or changed since the last backup.
Advantages and disadvantages of incremental backup
Advantages:
no duplicate data, high efficiency, maximum space utilization
, small backup volume, short time.
Disadvantages:
troublesome restoration: it needs the last full backup and all incremental backups after the full backup to restore, and all incremental backups are required Reverse push recovery one by one
is less safe
bring it on! Show!
Add the log-bin=filepath item (filepath is the path of the binary file) in the [mysqld] item of the MySQL configuration file, such as log-bin=mysql-bin, and then restart the mysqld service.
The default path of the binary log file is /usr/local/mysql/data
The significance of MySQL binary log for backup
The binary log saves all updates or operations that may update the database. The
binary log starts to record after the MySQL server is started, and recreates a new log file after the file reaches the size set by max_binlog_size or receives the flush logs command. You
only need to execute flush logs regularly The method recreates a new log, generates a sequence of binary files, and saves these logs in a safe place in time to complete a period of incremental backup
Backup data file
[root@5centos data]# systemctl stop mysqld
tar zcvf /beifen/mysql-$(date +%F).tar.gz /usr/local/mysql/data/
[root@5centos data]# systemctl start mysqld
Open binary file
[root@5centos data]# vim /etc/my.cnf
[mysqld]
……省略部分……
log-bin=mysql-bin
[root@5centos data]# systemctl restart mysqld
[root@5centos data]# ll
总用量 122928
……省略部分……发现已生成二进制文件
-rw-r-----. 1 mysql mysql 154 8月 22 22:47 mysql-bin.000001
Create demo library and table
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| JUEJUE |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use JUEJUE;
Database changed
mysql> show tables;
+------------------+
| Tables_in_JUEJUE |
+------------------+
| LIULIU |
+------------------+
1 row in set (0.00 sec)
mysql> select * from LIULIU;
+----+------+-------+
| ID | NAME | HIGHT |
+----+------+-------+
| 1 | JIAN | 175.0 |
| 2 | HAO | 174.0 |
+----+------+-------+
2 rows in set (0.00 sec)
Take a full backup
[root@5centos data]# mysqldump -u root -p JUEJUE > /beifen/JUEJUE.sql
Enter password:
增量备份
[root@5centos data]# mysqladmin -u root -p flush-logs;
Enter password:
查看增量日志文件
[root@5centos data]# ls
mysql-bin.000001 mysql-bin.000002
Simulate misoperation
mysql> insert into LIULIU values (4,'GU',190);
Query OK, 1 row affected (0.01 sec)
mysql> insert into LIULIU values (5,'cai',180);
Query OK, 1 row affected (0.00 sec)
mysql> insert into LIULIU values (3,'SHA',180);
Query OK, 1 row affected (0.00 sec)
[root@5centos data]# mysqladmin -u root -p flush-logs;
Enter password:
View Files
[root@5centos data]# mysqlbinlog --no-defaults --base64-output=decode-rows -v mysql-bin.000003 > /beifen/bk03.txt
[root@5centos data]# vim /beifen/bk03.txt
##最后一条执行
# at 625
#200822 23:54:34 server id 1 end_log_pos 673 CRC32 0xd27d03dd Write_rows: table id 108 flags: STMT_END_F
### INSERT INTO `JUEJUE`.`LIULIU`
### SET
### @1=5
### @2='cai'
### @3=180.0
Simulated recovery
mysql> drop table LIULIU;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
Empty set (0.00 sec)
基于时间
[root@5centos data]# mysqlbinlog --no-defaults --stop-datetime='2020-8-22 23;54:34' /usr/local/mysql/data/mysql-bin.000003 | mysql -u root -p
Enter password:
+----+------+-------+
| ID | NAME | HIGHT |
+----+------+-------+
| 1 | JIAN | 175.0 |
| 2 | HAO | 174.0 |
| 4 | GU | 190.0 |
| 5 | cai | 180.0 |
+----+------+-------+
如果想恢复特定的,恢复时开始和结束时间分开写就行
基于位置
我这又删了,不掩饰了
[root@5centos data]# mysqlbinlog --no-defaults --stop-position='625' /usr/local/mysql/data/mysql-bin.000003 | mysql -u root -p123123
+----+------+-------+
| ID | NAME | HIGHT |
+----+------+-------+
| 1 | JIAN | 175.0 |
| 2 | HAO | 174.0 |
| 4 | GU | 190.0 |
| 5 | cai | 180.0 |
+----+------+-------+