其他分享
首页 > 其他分享> > 备份恢复工具介绍

备份恢复工具介绍

作者:互联网

备份恢复

备份恢复工具介绍

(1)**   mongoexport/mongoimport		
导出json类文本,只能针对collections/表 做导入导出	

(2)***** mongodump/mongorestore		#导出BSON类二进制

# 对MongoDB来说,物理和逻辑的区别:
mongoexport/import		json 文本
mongodump/mongorestore	bson 二进制

备份工具区别在哪里

应用场景总结:
(1) mongoexport/mongoimport
导出 json csv 

1、异构平台迁移  mysql  <---> mongodb
2、同平台,跨大版本:mongodb 2  ----> mongodb 3

(2) mongodump/mongorestore (dump /restore) 
日常备份恢复时使用,导出格式是BSON

导出工具mongoexport


mongoexport具体用法如下所示:
$ mongoexport --help  
参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字,也就是表名
-f:指明要导出那些列
-o:指明到要导出的文件名
-q:指明导出数据的过滤条件
--authenticationDatabase admin

1.单表备份至json格式
mongoexport -uroot -proot123 --port 27017 --authenticationDatabase admin -d oldboy -c log -o /mongodb/log.json

注:备份文件的名字可以自定义,默认导出了JSON格式的数据。

[mongod@mysql-node01 ~]$ mongoexport -uroot -pw --port 27017 --authenticationDatabase admin -d simon -c log -o /mongodb/log.json
2020-10-25T08:49:34.841+0800	connected to: localhost:27017
2020-10-25T08:49:34.939+0800	exported 10000 records
[mongod@mysql-node01 ~]$ cd /mongodb
[mongod@mysql-node01 mongodb]$ head -3 log.json
{"_id":{"$oid":"5f9395096933b97a97bf97a3"},"uid":0.0,"name":"mongodb","age":6.0,"date":{"$date":"2020-10-24T02:44:25.730Z"}}
{"_id":{"$oid":"5f9395096933b97a97bf97a4"},"uid":1.0,"name":"mongodb","age":6.0,"date":{"$date":"2020-10-24T02:44:25.731Z"}}
{"_id":{"$oid":"5f9395096933b97a97bf97a5"},"uid":2.0,"name":"mongodb","age":6.0,"date":{"$date":"2020-10-24T02:44:25.732Z"}}
[mongod@mysql-node01 mongodb]$

2. 单表备份至csv格式
如果我们需要导出CSV格式的数据,则需要使用----type=csv参数:
-f 指定需要导出的列
 mongoexport -uroot -proot123 --port 27017 --authenticationDatabase admin -d test -c log --type=csv -f uid,name,age,date  -o /mongodb/log.csv
 
 
mongoexport -uroot -pw --port 27017 --authenticationDatabase admin -d simon -c log --type=csv -f uid,name,age,date -o /mongodb/log.csv
2020-10-25T08:55:12.261+0800	connected to: localhost:27017
2020-10-25T08:55:12.310+0800	exported 10000 records

head -3 log.csv 
uid,name,age,date
0,mongodb,6,2020-10-24T02:44:25.730Z
1,mongodb,6,2020-10-24T02:44:25.731Z


导入工具mongoimport

$ mongoimport --help
参数说明:
-h:指明数据库宿主机的IP
-u:指明数据库的用户名
-p:指明数据库的密码
-d:指明数据库的名字
-c:指明collection的名字
-f:指明要导入那些列
-j, --numInsertionWorkers=<number>  number of insert operations to run concurrently                                                  (defaults to 1)
//并行
数据恢复:
1.恢复json格式表数据到log1
mongoimport -uroot -proot123 --port 27017 --authenticationDatabase admin -d oldboy -c log1 /mongodb/log.json
        
        
[mongod@mysql-node01 mongodb]$ mongoimport -uroot -pw --port 27017 --authenticationDatabase admin -d simon -c log1 ./log.json 
2020-10-25T08:58:44.385+0800	connected to: localhost:27017
2020-10-25T08:58:44.467+0800	imported 10000 documents
[mongod@mysql-node01 mongodb]$
[mongod@mysql-node01 mongodb]$ mongo -uroot -p --port 27017 --authenticationDatabase admin
MongoDB shell version v3.6.20
Enter password: 
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("bd14f8f1-b3a0-43c2-9d71-f79ba68586c4") }
MongoDB server version: 3.6.20
> 
> use simon
switched to db simon
> show tables
log
log1
test
> 
> db.log1.count()
10000
> 
>

2.恢复csv格式的文件到log2
上面演示的是导入JSON格式的文件中的内容,如果要导入CSV格式文件中的内容,则需要通过--type参数指定导入格式,具体如下所示:
错误的恢复

注意:
(1)标准mongodb导出的csv格式的文件,有头行,有列名字
--headerline:指明第一行是列名,不需要导入。

mongoimport   -uroot -proot123 --port 27017 --authenticationDatabase admin   -d oldboy -c log2 --type=csv --headerline --file  /mongodb/log.csv

[mongod@mysql-node01 mongodb]$ mongoimport -uroot -pw  --host 10.0.50.61 --port 27017 --authenticationDatabase admin -d simon -c log2 --type=csv --headerline --file ./log.csv 
2020-10-25T09:08:09.788+0800	connected to: 10.0.50.61:27017
2020-10-25T09:08:09.852+0800	imported 10000 documents
[mongod@mysql-node01 mongodb]$
[mongod@mysql-node01 mongodb]$
[mongod@mysql-node01 mongodb]$ mongo -uroot -p --port 27017 --authenticationDatabase adminMongoDB shell version v3.6.20
Enter password: 
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("15627068-d1f8-420b-a00a-40122a7a3e8a") }
MongoDB server version: 3.6.20
> 
> 
> use simon
switched to db simon
> show tables
log
log1
log2
test
> db.log2.count()
10000
> 
> db.log2.totalSize()
389120
> 


(2)csv格式的文件头行,没有列名字
-f 指定列名,告诉mongodb 数据对应的字段名称

mongoimport   -uroot -proot123 --port 27017 --authenticationDatabase admin   -d oldboy -c log3 --type=csv -f id,name,age,date --file  /mongodb/log.csv

[mongod@mysql-node01 mongodb]$ mongoimport -uroot -pw --host 10.0.50.61 --port 27017 --authenticationDatabase admin -d simon -c log3  --type=csv -f uid,name,age,date ./log.csv
2020-10-25T09:14:05.679+0800	connected to: 10.0.50.61:27017
2020-10-25T09:14:05.742+0800	imported 10000 documents
[mongod@mysql-node01 mongodb]$

[mongod@mysql-node01 mongodb]$ mongo -uroot -p
MongoDB shell version v3.6.20
Enter password: 
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ca0e9acd-b8c2-48c4-a18d-4aba827b8fbe") }
MongoDB server version: 3.6.20
> use simon
switched to db simon
> db.log3.count()
10000
> 
> db.log3.find()
{ "_id" : ObjectId("5f94d15dd4536f9d39cb021d"), "uid" : 0, "name" : "mongodb", "age" : 6, "date" : "2020-10-24T02:44:25.730Z" }
{ "_id" : ObjectId("5f94d15dd4536f9d39cb021e"), "uid" : 1, "name" : "mongodb", "age" : 6, "date" : "2020-10-24T02:44:25.731Z" }
> 
> 

异构平台迁移

mysql   -----> mongodb  
world数据库下city表进行导出,导入到mongodb

(1)mysql开启安全路径
vim /etc/my.cnf   --->添加以下配置
secure-file-priv=/tmp

--重启数据库生效
/etc/init.d/mysqld restart

(2)导出mysql的city表数据
source /root/world.sql

mysql> select * from city into outfile '/tmp/city.csv' fields terminated by ',';
Query OK, 4079 rows affected (0.01 sec)

mysql> 

(3)导出列名,添加至导出文件
desc world.city
  ID          | int(11)  | NO   | PRI | NULL    | auto_increment |
| Name        | char(35) | NO   |     |         |                |
| CountryCode | char(3)  | NO   | MUL |         |                |
| District    | char(20) | NO   |     |         |                |
| Population

vim /tmp/city.csv   ----> 添加第一行列名信息

mysql> select group_concat(column_name) from information_schema.columns where table_schema='world' and table_name='city';
+-----------------------------------------+
| group_concat(column_name)               |
+-----------------------------------------+
| ID,Name,CountryCode,District,Population |
+-----------------------------------------+
1 row in set (0.00 sec)

mysql>
[root@mysql-node01 tmp]# sed -i '1 i\ID,Name,CountryCode,District,Population' ./city.csv 
[root@mysql-node01 tmp]# 
[root@mysql-node01 tmp]# 
[root@mysql-node01 tmp]# head -1 ./city.csv
ID,Name,CountryCode,District,Population
[root@mysql-node01 tmp]#
[root@mysql-node01 tmp]# su - mongod
[mongod@mysql-node01 ~]$ mongoimport -uroot -pw --authenticationDatabase admin -d simon -c city --type=csv --headerline /tmp/city.csv 
2020-10-25T09:43:38.495+0800	connected to: localhost
2020-10-25T09:43:38.531+0800	imported 4079 documents
[mongod@mysql-node01 ~]$

(4)在mongodb中导入备份
没有头行字段的话,就用-f参数指定字段名称

[root@mysql-node01 tmp]# sed -i '1 d' /tmp/city.csv

mongoimport -uroot -proot123 --port 27017 --authenticationDatabase admin -d world  -c city --type=csv -f ID,Name,CountryCode,District,Population --file  /tmp/city1.csv

[mongod@mysql-node01 mongodb]$ mongoimport -uroot -pw --authenticationDatabase admin -d simon -c city2 --type=csv -f ID,Name,CountryCode,District,Population --file ./city.csv 
2020-10-25T09:48:41.107+0800	connected to: localhost
2020-10-25T09:48:41.144+0800	imported 4079 documents
[mongod@mysql-node01 mongodb]$  
[mongod@mysql-node01 mongodb]$ mongo  -uroot -p
MongoDB shell version v3.6.20
Enter password: 
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("a9fdacfc-cfbe-430e-9d56-f280fb2d3dd0") }
MongoDB server version: 3.6.20
> 
> use simon
switched to db simon
> show tables
city
city2
log
log1
log2
log3
test
> db.city2.count()
4079
> 
> 
> db.city2.find
db.city2.find(               db.city2.findOne(            db.city2.findOneAndReplace(
db.city2.findAndModify(      db.city2.findOneAndDelete(   db.city2.findOneAndUpdate(
> db.city2.findOne()
{
	"_id" : ObjectId("5f94d979d4536f9d39cb39a8"),
	"ID" : 1,
	"Name" : "Kabul",
	"CountryCode" : "AFG",
	"District" : "Kabol",
	"Population" : 1780000
}
> 

use world
db.city.find({CountryCode:"CHN"});

-------------
world共100张表,全部迁移到mongodb

select table_name ,group_concat(column_name) from columns where table_schema='world' group by table_name;

select * from world.city into outfile '/tmp/world_city.csv' fields terminated by ',';

select concat("select * from ",table_schema,".",table_name ," into outfile '/tmp/",table_schema,"_",table_name,".csv' fields terminated by ',';")
from information_schema.tables where table_schema ='world';

导入:
提示,使用infomation_schema.columns + information_schema.tables

mysql导出csv:
select * from test_info   
into outfile '/tmp/test.csv'   
fields terminated by ','    ------字段间以,号分隔
optionally enclosed by '"'   ------字段用"号括起
escaped by '"'           ------字段中使用的转义符为"
lines terminated by '\r\n';  ------行以\r\n结束

mysql导入csv:
load data infile '/tmp/test.csv'   
into table test_info    
fields terminated by ','  
optionally enclosed by '"' 
escaped by '"'   
lines terminated by '\r\n'; 

标签:node01,--,mongodb,备份,介绍,mysql,工具,csv,27017
来源: https://www.cnblogs.com/oldSimon/p/16290819.html