mysqldump恢复指定表

如果mysql服务器上不能随意安装软件,当需要从全备份中恢复单个表,怎么办?


1.mysqldump备份指定数据库,如ivr
[mysql@bjmysql01]$ mysqldump -utelivr -p123456 -F -R --events --databases ivr |gzip >ivr_$(date +%F).sql.gz

2. 确认备份文件已经生成
[mysql@bjmysql01]$ ll ivr*
ivr_2014-01-25.sql.gz


3.模拟删除atuo表
mysql> show tables;
+---------------+
| Tables_in_ivr |
+---------------+
| atuo          |
| ivr           |
+---------------+
2 rows in set (0.00 sec)



mysql> select * from atuo;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  4 | c    |
+----+------+
3 rows in set (0.00 sec)


mysql> drop table atuo;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+---------------+
| Tables_in_ivr |
+---------------+
| ivr           |
+---------------+
1 row in set (0.00 sec)



4.从备份文件中找出需要恢复表的建表语句:
[mysql@bjmysql01]$ gunzip -c ivr_2014-01-25.sql.gz |sed -e '/./{H;$!d;}' -e 'x;/CREATE TABLE `atuo`/!d;q' 
DROP TABLE IF EXISTS `atuo`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `atuo` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(5) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;



5.从备份文件中找出需要恢复表的数据: 
[mysql@bjmysql01]$ gunzip -c ivr_2014-01-25.sql.gz | grep --ignore-case  'insert into `atuo`'
INSERT INTO `atuo` VALUES (1,'a'),(2,'b'),(4,'c');


6.恢复被删除表的表结构
[mysql@bjmysql01]$ gunzip -c ivr_2014-01-25.sql.gz |sed -e '/./{H;$!d;}' -e 'x;/CREATE TABLE `atuo`/!d;q' |mysql -utelivr -p123456 ivr
 




7.从备份文件中恢复被删除表的数据:
[mysql@bjmysql01]$ gunzip -c ivr_2014-01-25.sql.gz | grep --ignore-case  'insert into `atuo`'| mysql -utelivr -p123456 ivr



8.查看atuo表,数据已经恢复。
mysql> select * from atuo;
+----+------+
| id | name |
+----+------+
|  1 | a    |
|  2 | b    |
|  4 | c    |
+----+------+
3 rows in set (0.00 sec)

如果按表来备份,或把表结构和数据单独分开备份,单个表恢复起来或许更容易。

你可能感兴趣的:(MySQL)