Method of restoring a library and a table from MySQL full library backup
- 2021-11-29 16:49:20
- OfStack
In Mysqldump official tools, how to restore only one library?
Full library backup
[root@HE1 ~]# mysqldump -uroot -p --single-transaction -A --master-data=2 >dump.sql
Restore only the contents of the erp library
[root@HE1 ~]# mysql -uroot -pMANAGER erp --one-database <dump.sql
It can be seen that the main parameters used here are-one-database abbreviation-o parameters, which greatly facilitates our recovery flexibility.
So how to extract a table from the full library backup, restore the full library, and then restore a small library of a table, but it is very troublesome for a large library. Then we can use regular expressions to extract quickly. The specific implementation method is as follows:
Extracting the table structure of t table from full library backup
[root@HE1 ~]# sed -e'/./{H;$!d;}' -e 'x;/CREATE TABLE `t`/!d;q' dump.sql
DROP TABLE IF EXISTS`t`;
/*!40101 SET@saved_cs_client =@@character_set_client */;
/*!40101 SETcharacter_set_client = utf8 */;
CREATE TABLE `t` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`age` tinyint(4) NOT NULL DEFAULT '0',
`name` varchar(30) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDBAUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
/*!40101 SETcharacter_set_client = @saved_cs_client */;
Extract the contents of t table from full library backup
[root@HE1 ~]# grep'INSERT INTO `t`' dump.sql
INSERT INTO `t`VALUES (0,0,''),(1,0,'aa'),(2,0,'bbb'),(3,25,'helei');