mongodb backup and restore

一、mongodb的冷备

mongodb的冷备就是:复制库的相关文件。因此在冷备前,要关闭服务器,本全中使用平滑关闭server的命令。

>use admin

>db.shutdownServer()

或者可以通过fsync方式使MongoDB将数据写入缓存中,然后再复制备份

>use admin

>db.runCommand({"fsync":1,"lock":1})

锁库后执行插入数据命令,发现无任何反应。备份完后,要解锁(防止这个时候停电或其它原因,导致未缓存中的数据丢失)。

>use admin

>db.$cmd.sys.unlock.findOne()

>db.currentOp()

如果currentOp 只返回{"inprog":[]}结果,说明解锁成功。 解锁后,数据才会成功写入数据库。

解锁也可以使用以下命令:

>use admin

>db.fsyncUnlock()

二、mongodb的热备

可以在不停止服务的情况下,使用MongoDB提供的两个工具来实现备份和恢复。这个两个工具在MongoDB的bin目录下可以看到:mongodump/mongorestor

1、常用命令格

mongodump -h IP --port PORT -u UserName -p Password -d DB_name -c Collection_Name  -o BackupPath

如果不指定用户,可以去掉-u和-p;省略-h,默认导出本机的数据库;省略- -port默认端口27017;省略-d默认导出所有数据库。

2、导出所有数据库

[[email protected] mongodb]# mongodump -h 127.0.0.1 -o /home/user01/backup/

3、导出指定数据库

[[email protected] mongodb]# mongodump -h 192.168.1.108 -d tank -o /home/user01/backup

三、mongorestore还原数据库

1、常用命令格式

mongorestore -h IP --port PORT -u UserName -p Password -d DB_Name -c Collection_Name  --drop BackupPath_Or_FileName

--drop的意思是,先删除所有的记录,然后恢复。

2、恢复所有数据库到mongodb中

[[email protected] mongodb]# mongorestore /home/user01/backup/  #这里的路径是所有库的备份路径

3、还原指定的数据库

[[email protected] mongodb]# mongorestore -d tank /home/user01/backup/tank/  #tank这个数据库的备份路径

[[email protected] mongodb]# mongorestore -d tank_new /home/user01/backup/tank/  #将tank还有tank_new数据库中

这二个命令,可以实现数据库的备份与还原,文件格式是json和bson的。无法指写到表备份或者还原。

四、mongoexport导出表,或者表中部分字段

1、常用命令格式

mongoexport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 -f 字段 -q 条件导出 --csv -o 文件名

上面的参数好理解,重点说一下:

-f    导出指字段,以字号分割,-f name,email,age导出name,email,age这三个字段

-q    可以根查询条件导出,-q ‘{ "uid" : "100" }‘ 导出uid为100的数据

--csv 表示导出的文件格式为csv的,这个比较有用,因为大部分的关系型数据库都是支持csv,在这里有共同点

2、导出整张表

[[email protected] mongodb]# mongoexport -d tank -c users -o /home/user01/backup/tank/users.dat

3、导出表中部分字段

[[email protected] mongodb]# mongoexport -d tank -c users --csv -f uid,name,sex -o tank/users.csv

4、根据条件敢出数据

[[email protected] mongodb]# mongoexport -d tank -c users -q ‘{uid:{$gt:1}}‘ -o tank/users.json

五、mongoimport导入表,或者表中部分字段

1、常用命令格式

1),还原整表导出的非csv文件

mongoimport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 --upsert --drop 文件名

重点说一下--upsert,其他参数上面的命令已有提到,--upsert 插入或者更新现有数据

2),还原部分字段的导出文件

mongoimport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 --upsertFields 字段 --drop 文件名

--upsertFields根--upsert一样

3),还原导出的csv文件

mongoimport -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -c 表名 --type 类型 --headerline --upsert --drop 文件名

上面三种情况,还可以有其他排列组合的。

2、还原导出的表数据

[[email protected] mongodb]# mongoimport -d tank -c users --upsert tank/users.dat

3、部分字段的表数据导入

[[email protected] mongodb]# mongoimport -d tank -c users  --upsertFields uid,name,sex  tank/users.dat

4、还原csv文件

[[email protected] mongodb]# mongoimport -d tank -c users --type csv --headerline --file tank/users.csv

六、实例:

[[email protected] ~]# service mongod start

Starting mongod:                                           [确定]

[[email protected] ~]# mongo localhost:27027

MongoDB shell version: 3.2.8

connecting to: localhost:27027/test

Server has startup warnings:

> use admin

switched to db admin

> db.createUser(

... {user:"admin",pwd:"123456",

... roles:[{role:"root",db:"admin"}]

... })

Successfully added user: {

"user" : "admin",

"roles" : [

{

"role" : "root",

"db" : "admin"

}

]

}

> use person

switched to db person

> db.p1.insert({name:"thompson",gender:"male",age:"24"})

WriteResult({ "nInserted" : 1 })

> db.p1.find()

{ "_id" : ObjectId("57a2a28aa6d4803a1c952529"), "name" : "thompson", "gender" : "male", "age" : "24" }

> exit

bye

[[email protected] ~]# mongo localhost:27027

MongoDB shell version: 3.2.8

connecting to: localhost:27027/test

> show dbs;

admin   0.000GB

local   0.000GB

person  0.000GB

> exit

bye

[[email protected] ~]# vim /etc/mongod.conf

[[email protected] ~]# sed -n ‘32,33p‘ /etc/mongod.conf    需要开启认证功能

security:

authorization: enabled

[[email protected] ~]# service mongod restart    设备完配置文件后必须重新启动才能生效

Stopping mongod:                                           [确定]

Starting mongod:                                           [确定]

[[email protected] ~]# mongo localhost:27027

MongoDB shell version: 3.2.8

connecting to: localhost:27027/test

> show dbs        如果未认证,系统提示错误

2016-08-04T10:06:08.491+0800 E QUERY    [thread1] Error: listDatabases failed:{

"ok" : 0,

"errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",

"code" : 13

} :

[email protected]/mongo/shell/utils.js:25:13

[email protected]/mongo/shell/mongo.js:62:1

[email protected]/mongo/shell/utils.js:761:19

[email protected]/mongo/shell/utils.js:651:15

@(shellhelp2):1:1

> use admin

switched to db admin

> db.auth("admin","123456")        认证

1

> use person

switched to db person

> db.createUser(                            创建新用户

... {user:"person",pwd:"123",

... roles:[{role:"readWrite",db:"person"}]

... })

Successfully added user: {

"user" : "person",

"roles" : [

{

"role" : "readWrite",

"db" : "person"

}

]

}

> use admin

switched to db admin

> db.system.users.find()

{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "KFiaKAkrDqCJ/H8uIIhwzA==", "storedKey" : "faWxuPj1hZ4jV3VhL9Z0zylBL0Y=", "serverKey" : "qYSi5BRZY/GPTuBeF60KCvB5dqg=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }

{ "_id" : "person.person", "user" : "person", "db" : "person", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "0tRiioYKdcx+On3uXgR/Sw==", "storedKey" : "8M69xFSgqniSeU7uvLqpzaclECs=", "serverKey" : "Znu2x5fAzMgrMKlxpj2I//1lcWc=" } }, "roles" : [ { "role" : "readWrite", "db" : "person" } ] }

> exit

bye

[[email protected] ~]# mkdir backup

[[email protected] ~]# mongodump -h localhost --port 27027 -d person -o backup/ -u person -p 123

2016-08-04T10:09:36.701+0800 writing person.p1 to

2016-08-04T10:09:36.701+0800 done dumping person.p1 (1 document)

[[email protected] ~]# ls backup/person/

p1.bson  p1.metadata.json

[[email protected] ~]#

[[email protected] ~]# mongo localhost:27027/admin -u admin -p

MongoDB shell version: 3.2.8

Enter password:

connecting to: localhost:27027/admin

> use person

switched to db person

> show collections

p1

> db.p1.drop()

true

> show collections

> exit

bye

[[email protected] ~]# mongorestore -h localhost --port 27027 -d person backup/person/ -u person -p 123

2016-08-04T10:11:42.234+0800 building a list of collections to restore from backup/person dir

2016-08-04T10:11:42.235+0800 reading metadata for person.p1 from backup/person/p1.metadata.json

2016-08-04T10:11:42.256+0800 restoring person.p1 from backup/person/p1.bson

2016-08-04T10:11:42.268+0800 restoring indexes for collection person.p1 from metadata

2016-08-04T10:11:42.268+0800 finished restoring person.p1 (1 document)

2016-08-04T10:11:42.268+0800 done

[[email protected] ~]#

[[email protected] ~]# mongo localhost:27027/person -u person -p 123

MongoDB shell version: 3.2.8

connecting to: localhost:27027/person

> show collections

p1

> db.p1.find()

{ "_id" : ObjectId("57a2a28aa6d4803a1c952529"), "name" : "thompson", "gender" : "male", "age" : "24" }

>

> use person

switched to db person

> db.grantRolesToUser("person",[{role:"dbAdmin",db:"person"}])        为用户附加其它角色

> use admin

switched to db admin

> db.system.users.find()

{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "KFiaKAkrDqCJ/H8uIIhwzA==", "storedKey" : "faWxuPj1hZ4jV3VhL9Z0zylBL0Y=", "serverKey" : "qYSi5BRZY/GPTuBeF60KCvB5dqg=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }

{ "_id" : "person.person", "user" : "person", "db" : "person", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "0tRiioYKdcx+On3uXgR/Sw==", "storedKey" : "8M69xFSgqniSeU7uvLqpzaclECs=", "serverKey" : "Znu2x5fAzMgrMKlxpj2I//1lcWc=" } }, "roles" : [ { "role" : "dbAdmin", "db" : "person" }, { "role" : "readWrite", "db" : "person" } ] }

>

> use person

switched to db person

> db.revokeRolesFromUser("person",[{role:"dbAdmin",db:"person"}])        用户角色回收

> use admin

switched to db admin

> db.system.users.find()

{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "KFiaKAkrDqCJ/H8uIIhwzA==", "storedKey" : "faWxuPj1hZ4jV3VhL9Z0zylBL0Y=", "serverKey" : "qYSi5BRZY/GPTuBeF60KCvB5dqg=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }

{ "_id" : "person.person", "user" : "person", "db" : "person", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "0tRiioYKdcx+On3uXgR/Sw==", "storedKey" : "8M69xFSgqniSeU7uvLqpzaclECs=", "serverKey" : "Znu2x5fAzMgrMKlxpj2I//1lcWc=" } }, "roles" : [ { "role" : "readWrite", "db" : "person" } ] }

> exit

bye

[[email protected] ~]# rm backup/* -rf

[[email protected] ~]# mongoexport -h localhost --port 27027 -u person -p 123 -d person -c p1 -o backup/person.p1.dat

2016-08-04T10:22:06.773+0800 connected to: localhost:27027

2016-08-04T10:22:06.773+0800 exported 1 record

[[email protected] ~]# mongoimport -h localhost --port 27027 -u person -p 123 -d person -c p2 --upsert backup/person.p1.dat

2016-08-04T10:25:16.414+0800 connected to: localhost:27027

2016-08-04T10:25:16.434+0800 imported 1 document

[[email protected] ~]# mongo localhost:27027/person -u person -p 123

MongoDB shell version: 3.2.8

connecting to: localhost:27027/person

> show collections

p1

p2

> db.p2.find()

{ "_id" : ObjectId("57a2a28aa6d4803a1c952529"), "name" : "thompson", "gender" : "male", "age" : "24" }

> db.p1.find()

{ "_id" : ObjectId("57a2a28aa6d4803a1c952529"), "name" : "thompson", "gender" : "male", "age" : "24" }

{ "_id" : ObjectId("57a2a9c43f2b617cfdd64c63"), "name" : "eric", "gender" : "female", "age" : 18 }

{ "_id" : ObjectId("57a2bfe38381eac036252b7c"), "name" : "test1", "gender" : "male", "age" : 20 }

> exit

bye

[[email protected] ~]# rm -rf backup/* ; ls backup/

[[email protected] ~]# mongodump -h localhost --port 27027 -d person -o backup/ -u person -p 123    备份时备份整个库中的所有表

2016-08-04T12:36:36.321+0800 writing person.p1 to

2016-08-04T12:36:36.321+0800 writing person.p2 to

2016-08-04T12:36:36.322+0800 done dumping person.p1 (3 documents)

2016-08-04T12:36:36.322+0800 done dumping person.p2 (1 document)

[[email protected] ~]# ls backup/

person

[[email protected] ~]# ls backup/person/

p1.bson  p1.metadata.json  p2.bson  p2.metadata.json

[[email protected] ~]# mongo localhost:27027/person -u person -p 123

MongoDB shell version: 3.2.8

connecting to: localhost:27027/person

> db.p1.drop()

true

> db.p2.drop()

true

> show collections

> exit

bye

[[email protected] ~]# mongorestore -h localhost --port 27027 -d person -c p1 backup/person/p1.bson -u person -p 123

        注:可以单独恢复指定的表

2016-08-04T12:38:55.541+0800 checking for collection data in backup/person/p1.bson

2016-08-04T12:38:55.541+0800 reading metadata for person.p1 from backup/person/p1.metadata.json

2016-08-04T12:38:55.560+0800 restoring person.p1 from backup/person/p1.bson

2016-08-04T12:38:55.639+0800 restoring indexes for collection person.p1 from metadata

2016-08-04T12:38:55.640+0800 finished restoring person.p1 (3 documents)

2016-08-04T12:38:55.641+0800 done

[[email protected] ~]# mongo localhost:27027/person -u person -p 123

MongoDB shell version: 3.2.8

connecting to: localhost:27027/person

> show tables

p1

> db.p1.find()

{ "_id" : ObjectId("57a2a9c43f2b617cfdd64c63"), "name" : "eric", "gender" : "female", "age" : 18 }

{ "_id" : ObjectId("57a2bfe38381eac036252b7c"), "name" : "test1", "gender" : "male", "age" : 20 }

{ "_id" : ObjectId("57a2a28aa6d4803a1c952529"), "name" : "thompson", "gender" : "male", "age" : "24" }

> exit

bye

[[email protected] ~]

时间: 2024-11-03 12:21:47

mongodb backup and restore的相关文章

TFS Express backup and restore

 When we setup source control server, we should always make a backup and restore plan for it. This article is to describe how to backup and restore a TFS Express instance from one server to another server. This blog is an English version, for Chine

Backup and restore of FAST Search for SharePoint 2010

一个同事问我一个问题: 如果FAST Search for SharePoint 2010被full restore到了一个之前的时间点, 那么当FAST Search重新开始一个增量爬网的时候, 会发生什么? FAST Search会查看内容数据库并发现上一次爬网的记录并为新item或更改的item制作索引么? FAST Search会发现索引与现在内容的不一致么? 还是说它直接会再来一次full crawl?   Some Basics =================== Fast Se

How to backup and restore database in SQL Server

/*By Dylan SUN*/ If you want to backup and restore one database in SQL Server. Firstly, create a shared folder, and add everyone with read/write right. Secondly, backup your database. You can use the following script : backup database DatabaseName to

第一章、关于SQL Server数据库的备份和还原(sp_addumpdevice、backup、Restore)

在sql server数据库中,备份和还原都只能在服务器上进行,备份的数据文件在服务器上,还原的数据文件也只能在服务器上,当在非服务器的机器上启动sql server客户端的时候,也可以通过该客户端来备份和还原数据库,但是这种操作实质是在服务器上进行的,备份的数据文件在服务器上,还原的数据文件也只能在服务器上,这个原则不会变,只是使用了客户端的一个工具来操作这个过程而已. 1.1.备份数据库 备份数据库有两种方式: 第一种是在企业管理器中,利用工具对数据库进行备份,这种备份的文件只会有一个,即以

SQL2005中使用backup、restore来备份和恢复数据库

在SQL2005数据库中利用SQL语句进行数据备份与还原: 备份backup:backup database 数据库名称 tO disk = 备份路径例:BACKUP DATABASE test TO disk = 'd:\bak\test.bak' 恢复restore:restore  database 数据库名称from disk = 备份路径例:RESTORE DATABASE test FROM disk = 'd:\bak\test.bak'

Experience on Namenode backup and restore --- checkpoint

Hadoop version: Hadoop 2.2.0.2.0.6.0-0009 Well, We can do this by building Secondary Namenode, Checkpoint node or Backup node. Example: Assuming you have a Secondary Namenode. 1. Check secondary namenode checkpoint status: dfs.namenode.secondary.http

GPO - Backup and Restore

Backup the GPO to a second server is very important. Restore a GPO if necessary. Note: WMI filter and Links need to be re-configured after restoration. 原文地址:https://www.cnblogs.com/keepmoving1113/p/12246893.html

suitecrm 如何backup and restore ,从一个server 转移到另一个 server . 并保证customer package , customer module 不丢

原server部分 1 :  suite backup 分为 数据库和 网站 两部分 , 在 网站目录下 config.php , 可以看到 数据库名字 等信息 . 在 /home 目录下 , 新建 liuyang 目录 ---   mkdir liuyang 给予写权限 ---  sudo chmod -R a+rw /home/liuyang 2 :登陆 suitecrm 网站 ,admin----backup 到刚才的目录 3 : 备份数据库   先登陆 : mysql -u root -

How to restore and recover a database from an RMAN backup. (Doc ID 881395.1)

APPLIES TO: Oracle Database - Enterprise Edition - Version 10.1.0.2 to 11.2.0.2 [Release 10.1 to 11.2]Oracle Database - Enterprise Edition - Version 11.2.0.4 to 11.2.0.4 [Release 11.2]Oracle Database - Enterprise Edition - Version 11.2.0.3 to 11.2.0.