Full and incremental backups of MySQL using mysqldump
The purpose of backup: When data is an important asset, we need to back it up frequently to prevent data damage and restore it to the state when it was damaged in time.
Backup content: data, configuration files, binary logs, transaction logs
Backup classification:
Backup type:
Hot, Warm, and Cold Backups
Hot backup: read and write are not affected;
Warm backup: only read operations can be performed;
Cold backup: offline backup; read and write operations are suspended;
Physical Backup and Logical Backup
Physical backup: copy data files;
Logical backup: export data to text files;
full, incremental and differential backups;
Full backup: back up all data;
Incremental backup: Only back up data that has changed since the last full backup or incremental backup;
Differential backup: Back up only the data that has changed since the last full backup
Backup tools: xtrabackup, mysqldump
MyISAM engine can only support warm backup, InnoDB can support hot backup and warm backup.
Backup strategy: full + incremental; full + differential
mysqldump backup tool introduction
mysqldump syntax
mysqldump DB_NAME [tb1] [tb2] only backs up a certain database or a table in the library. Note: it does not contain the name of the database. When restoring in the future, the database must be created manually
--master-data={0|1|2}
0: : Do not log binary log file and location:
1 : Record the location in the form of CHANGE MASTER TO , which can be used to directly start the slave server after recovery:
2 : Record the location as CHANGE MASTER TO , but annotated by default:
--lock-all-tables: lock all tables Before starting the backup of the MyISAM engine tables, lock all tables first.
--flush-logs : Before backup, lock the table and perform log rolling
--single-transaction start hot standby
If the tables in the specified library are all InnoDB , you can use --single-transaction to start hot standby:
--events backup events, the event scheduler defined by the backup database
--routines backup stored procedures and stored functions
--triggers backup triggers
Backup multiple libraries
--all-databases: backup all databases
--databases DB_NAME,DB_NAME,... Backup the specified database
These two commands do not need to manually create a library before restoring because more than one library is backed up.
Test 1: mysqldump full backup + binary log for point-in-time restore simulated full backup + incremental backup
Test environment: MySQL version: mysql-5.1.73-8
Linux distribution: CentOS6.8
Preparation before experiment:
1. Create the database jiaowu and tutors tables, as follows
Create a database for academic affairs: mysql> CREATE DATABASE jiaowu;
Create the tutors table:
CREATE TABLE `tutors` (
`TID` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`Tname` varchar(50) NOT NULL,
`Gender` enum('F','M') DEFAULT 'M',
`Age` tinyint(3) unsigned DEFAULT NULL,
UNIQUE KEY `TIME`
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1; // Create table structure
INSERT INTO `tutors` VALUES // Insert table data
(1,'HongQigong','M',93),(2,'HuangYaoshi','M',63),(3,'Miejueshitai','F',72),(4,'OuYangfeng',' M',76),(5,'YiDeng','M',90),(6,'YuCanghai','M',56),(7,'Jinlunfawang','M',67),(8 ,'HuYidao','M',42),(9,'NingZhongze','F',49);
2. Enable binary logging
Add log-bin=mysql-bin ( mysql-bin represents the binary log name) in [mysqld] of the configuration file /etc/my.cnf ; then restart the service: service mysqld restart
Check whether bin_log is enabled: mysql> SELECT @@sql_log_bin; 1 : indicates that it is enabled
Experimental steps:
1. Lock table, refresh
mysql> FLUSH TABLES WITH READ LOCK;
2. View the current binary log
mysql> SHOW MASTER LOGS;
3. Do log scrolling
mysql> FLUSH TABLES WITH READ LOCK;
4. View the starting record position of the binary record after rolling: the next transaction starts from 106 of mysql-bin.000004 :
mysql> SHOW BINARY LOGS;
5. Create a backup directory
[root@Paul ~]# mkdir /root/backup
6. Do a full backup
[root@Paul ~]# mysqldump -uroot -p jiaowu > /root/backup/jiaowu-`date +%F_%H-%M-%S`
7. View backup files
[root@Paul backup]# ls -l /root/backup/
8. Change the owner and group after backup
[root@Paul backup]# chown -R mysql.mysql /root/backup/
9. View the result after the change:
[root@Paul backup]# ls -l /root/backup/
10. After the backup is over, unlock it
mysql> UNLOCK TABLE;
11. Insert new data in the tutors standard of the jiaowu database
mysql> INSERT INTO tutors (Tname) VALUES ('stu1'),('stu2');
12. Look at the updated content of the watch
mysql> SELECT * FROM tutors;
13. Enter the binary directory
[root@Paul backup]# cd /var/lib/mysql
14. Backup the updated binary log
[root@Paul mysql]# cp mysql-bin.000004 /root/backup
15. Delete all data files
[root@Paul mysql]# rm -rf ./*
16. Restart the mysql service
[root@Paul ~]# service mysqld restart
17. Create the original database
mysql> CREATE DATABASE jiaowu;
18. Restore the database
mysql jiaowu < jiaowu.2018-04-21-07-13-59
19. View the result after restoration:
mysql> select * from tutors;
20. Use binary logs for point-in-time restore
[root@Paul backup]# mysqlbinlog mysql-bin.000004 | mysql -uroot –p
21. View the restoration results:
mysql> select * from tutors;