如何從MySQL全庫備份的dump檔案中恢復某張表和表結構

chenfeng發表於2017-01-19
例如全庫dump檔案為test.sql 

從全庫備份test.sql中抽取出t_user_sign表的內容:
[root@linuxidc ~]# grep 'INSERT INTO `t_user_sign`' test.sql > /tmp/t_user_sign.sql
INSERT INTO `t_user_sign` VALUES ('913400007568317795','0','661401092936','20150101000000','60.166.38.226','2016-11-24 17:24:19','
0001221E7FF447FB96111C2510F5B80F','74-27-EA-4B-F5-A1',,...............................................................................................................
.........................................................................................................


從全庫備份中test.sql抽取出t_user_sign表的建表結構:
[root@linuxidc ~]# sed -e'/./{H;$!d;}' -e 'x;/CREATE TABLE `t_user_sign`/!d;q' test.sql > /tmp/create.sql
[root@linuxidc ~]# cat /tmp/create.sql
DROP TABLE IF EXISTS `t_user_sign`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `t_user_sign` (
  `SH` varchar(25) DEFAULT NULL COMMENT '稅號',
  `ZFJH` varchar(20) DEFAULT NULL COMMENT '主分機號',
  `KPJH` varchar(20) DEFAULT NULL COMMENT '開票機號',
  `FXSJ` varchar(26) DEFAULT NULL COMMENT '發行時間',
  `IP` varchar(20) DEFAULT NULL COMMENT '客戶IP地址',
  `ZCSJ` datetime DEFAULT NULL COMMENT '註冊時間',
  `ID` varchar(32) NOT NULL COMMENT 'ID',
  `MAC` varchar(32) DEFAULT NULL COMMENT 'MAC地址',
  `DQMC` varchar(120) DEFAULT NULL COMMENT '地區名稱',
  `DQBM` varchar(6) DEFAULT NULL COMMENT '地區編碼',
  `OS` varchar(120) DEFAULT NULL COMMENT '作業系統',
  `SBLX` varchar(120) DEFAULT NULL COMMENT '裝置型別',
  `WLFWS` varchar(120) DEFAULT NULL COMMENT '網路服務商',
  `LATITUDE` varchar(20) DEFAULT NULL COMMENT '維度',
  `LONGITUDE` varchar(20) DEFAULT NULL COMMENT '經度',
  `NAME` varchar(500) DEFAULT NULL COMMENT '企業名稱',
  `VERSION` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*!40101 SET character_set_client = @saved_cs_client */;

根據binlog時間點恢復:
[root@linuxidc ~]# mysqlbinlog --start-datetime="2017-1-16 19:00:00" --stop-datetime="2017-1-16 19:38:00" mysql-bin.000398 > /tmp/time.sql
[root@idb4 tmp]# more /tmp/time.sql
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#170116 19:37:02 server id 1  end_log_pos 120 CRC32 0x1cf0554e  Start: binlog v 4, server v 5.6.27-log created 170116 19:37:02
BINLOG '
XrB8WA8BAAAAdAAAAHgAAAAAAAQANS42LjI3LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAAXAAEGggAAAAICAgCAAAACgoKGRkAAU5V
8Bw=
'/*!*/;
# at 120
#170116 19:37:02 server id 1  end_log_pos 215 CRC32 0x16c50c8a  Query   thread_id=238   exec_time=0     error_code=0
SET TIMESTAMP=1484566622/*!*/;
SET @@session.pseudo_thread_id=238/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=1075838976/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C latin1 *//*!*/;
SET @@session.character_set_client=8,@@session.collation_connection=8,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
.............................................................................................................................................................



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-2132661/,如需轉載,請註明出處,否則將追究法律責任。

相關文章