mysqldump恢復指定表
來自: http://blog.csdn.net/lwei_998/article/details/18860889
如果mysql服務器上不能隨意安裝軟件,當需要從全備份中恢復單個表,怎么辦?
1.mysqldump備份指定數據庫,如ivr
[sql] view plain copy 在CODE上查看代碼片派生到我的代碼片
[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表
[sql] view plain copy 在CODE上查看代碼片派生到我的代碼片
mysql> show tables;
+---------------+
| Tables_in_ivr |
+---------------+
| atuo |
| ivr |
+---------------+
2 rows in set (0.00 sec)
[sql] view plain copy 在CODE上查看代碼片派生到我的代碼片
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)
[sql] view plain copy 在CODE上查看代碼片派生到我的代碼片
mysql> show tables;
+---------------+
| Tables_in_ivr |
+---------------+
| ivr |
+---------------+
1 row in set (0.00 sec)
4.從備份文件中找出需要恢復表的建表語句:
[sql] view plain copy 在CODE上查看代碼片派生到我的代碼片
[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.從備份文件中找出需要恢復表的數據:
[sql] view plain copy 在CODE上查看代碼片派生到我的代碼片
[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.恢復被刪除表的表結構
[sql] view plain copy 在CODE上查看代碼片派生到我的代碼片
[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.從備份文件中恢復被刪除表的數據:
[sql] view plain copy 在CODE上查看代碼片派生到我的代碼片
[mysql@bjmysql01]$ gunzip -c ivr_2014-01-25.sql.gz | grep --ignore-case 'insert into `atuo`'| mysql -utelivr -p123456 ivr
8.查看atuo表,數據已經恢復。
[sql] view plain copy 在CODE上查看代碼片派生到我的代碼片
mysql> select * from atuo;
+----+------+
| id | name |
+----+------+
| 1 | a |
| 2 | b |
| 4 | c |
+----+------+
3 rows in set (0.00 sec)
如果按表來備份,或把表結構和數據單獨分開備份,單個表恢復起來或許更容易。