62. 如何从 mysqldump 产生的全库备份中只恢复某一个库、某一张表?
一、–ignore-table 备份库是忽略这个库中的某个表:
mysqldump -uroot -p’Zhkj@554996’ -S /tmp/mysql3307.sock s2166ptzy —ignore-table=s2166ptzy.tab_user_login_record > 2017-08-25dump.sql
不导出指定表。指定忽略多个表时,需要重复多次,每次一个表。每个表必须同时指定数据库和表名。例如:–ignore-table=database.table1 —ignore-table=database.table2
例如:-B参数包含了建库语句
mysqldump -uroot -p’Zhkj@554996’ -S /tmp/mysql3307.sock -B s2166ptzy —ignore-table=s2166ptzy.tab_user_picture —ignore-table=s2166ptzy.tab_user >>12dump.sql
验证:导入12dump.sql文件到数据,查看表tab_user 和tab_user_picture是否存在,加入表不存在,说明参数是正确的
mysql> source /tmp/12dump.sql;
mysql> select count(*) from tab_user_picture ;
ERROR 1146 (42S02): Table ‘s2166ptzy.tab_user_picture’ doesn’t exist
mysql> select count(*) from tab_user ;
ERROR 1146 (42S02): Table ‘s2166ptzy.tab_user’ doesn’t exist
二、mysql 全备:
mysqldump -uroot -p’Zhkj@554996’ -S /tmp/mysql3307.sock —default-character-set=utf8 —single-transaction —extended-insert=false —hex-blob —master-data=2 —log-error=/tmp/test.err —routines —triggers —events —quick —flush-logs —all-databases > fulldump.sql
三、从全备份中只恢复s2166ptzy库
mysql -uroot -p’Zhkj@554996’ -S /tmp/mysql3307.sock s2166ptzy —one-database </tmp/fulldump.sql
–one-database简写-o的参数,方便了我们的恢复灵活性。
经测试,上述这样恢复数据库是非常的慢的
四、从全备份中只恢复sys_action_log表
1)从备份查询恢复表的表结构
[root@localhost ~]# sed -e’/./{H;$!d;}’ -e ‘x;/CREATE TABLE sys_action_log
/!d;q’ /tmp/fulldump.sql
DROP TABLE IF EXISTS sys_action_log
;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE sys_action_log
(
id
int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT ‘主键’,
action_id
int(10) unsigned NOT NULL DEFAULT ‘0’ COMMENT ‘行为id’,
user_id
int(10) unsigned NOT NULL DEFAULT ‘0’ COMMENT ‘执行用户id’,
action_ip
bigint(20) NOT NULL COMMENT ‘执行行为者ip’,
model
varchar(50) NOT NULL DEFAULT ‘’ COMMENT ‘触发行为的表’,
record_id
int(10) unsigned NOT NULL DEFAULT ‘0’ COMMENT ‘触发行为的数据id’,
remark
varchar(255) NOT NULL DEFAULT ‘’ COMMENT ‘日志备注’,
status
tinyint(2) NOT NULL DEFAULT ‘1’ COMMENT ‘状态’,
create_time
int(10) unsigned NOT NULL DEFAULT ‘0’ COMMENT ‘执行行为的时间’,
PRIMARY KEY (id
),
KEY action_ip_ix
(action_ip
),
KEY action_id_ix
(action_id
),
KEY user_id_ix
(user_id
)
) ENGINE=InnoDB AUTO_INCREMENT=7237 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT=‘行为日志表’;
/*!40101 SET character_set_client = @saved_cs_client */;
2)从备份查询出表的表数据
[root@mha2 backup]# grep ‘INSERT INTO sys_action_log
’ fulldump.sql >data.sql
source /tmp/data.sql
将某个库先恢复操作,在单独将某个表备份,在恢复某个表