好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Windows或Linux系统中备份和恢复MongoDB数据的教程

windows下面mongodb数据库备份和恢复

我可以讲数据备份到c:\data\dump目录下面,首先创建这个路径。然后进入到mongodb的bin目录下面

我的是:

?

1

C:\Program Files\mongodb\bin

备份脚本是:

?

1

2

// 备份

mongodump -h 127.0.0.1:27017 -d test -o c:\data\dump

恢复脚本是:

?

1

2

// 恢复

  mongorestore -h 127.0.0.1:27017 -d test --directoryperdb c:\data\dump\ test

解释一下用到的命令

-h:MongoDB所在服务器地址 -d:需要恢复的数据库实例,例如:test,当然这个名称也可以和备份时候的不一样,比如test2 -o:备份的数据存放位置,例如:c:\data\dump,当然该目录需要提前建立,在备份完成后,系统自动在dump目录下建立一个test目录,这个目录里面存放该数据库实例的备份数据。 --directoryperdb:备份数据所在位置,例如:c:\data\dump\test,这里为什么要多加一个test,而不是备份时候的dump,读者自己查看提示吧! --drop:恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,慎用哦!

原始解释:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

-v [ --verbose ]           be more verbose (include multiple times

                    for more verbosity e.g. -vvvvv)

--version               print the program's version and exit

-h [ --host ] arg           mongo host to connect to ( <set

                    name>/s1,s2 for sets)

--port arg              server port. Can also use --host

                    hostname:port

--ipv6                enable IPv6 support (disabled by

                    default)

-u [ --username ] arg         username

-p [ --password ] arg         password

--authenticationDatabase arg     user source (defaults to dbname)

--authenticationMechanism arg (=MONGODB-CR)

                    authentication mechanism

--dbpath arg             directly access mongod database files

                    in the given path, instead of

                    connecting to a mongod server - needs

                    to lock the data directory, so cannot

                    be used if a mongod is currently

                    accessing the same path

--directoryperdb           each db is in a separate directly

                    (relevant only if dbpath specified)

--journal               enable journaling (relevant only if

                    dbpath specified)

-d [ --db ] arg            database to use

-c [ --collection ] arg        collection to use (some commands)

--objcheck              validate object before inserting

                    (default)

--noobjcheck             don't validate object before inserting

--filter arg             filter to apply before inserting

--drop                drop each collection before import

--oplogReplay             replay oplog for point-in-time restore

--oplogLimit arg           include oplog entries before the

                    provided Timestamp (seconds[:ordinal])

                    during the oplog replay; the ordinal

                    value is optional

--keepIndexVersion          don't upgrade indexes to newest version

--noOptionsRestore          don't restore collection options

--noIndexRestore           don't restore indexes

--w arg (=0)             minimum number of replicas per write

linux下面mongodb数据库备份和恢复

linux下面我们可以创建一个自动备份脚本,可以设置定时任务,也可以手动备份。我是手动备份的。

首先创建一个sh命令,我是放在home下面的。

?

1

vim /home/mongoBeiFen .sh

输入如下内容:

?

1

2

3

4

5

6

7

#!/bin/bash

shijie=` date +%Y%m%d%H`

backmongodbFile=mongodb$shijie. tar .gz

cd /home/mongoDbback/

/usr/local/mongo/bin/mongodump -h 127.0.0.1 --port 27017 -u mongo -p 123456 -d my_mongodb -o my_mongodb_dump/

tar czf $backmongodbFile my_mongodb_dump/

rm my_mongodb_dump -rf

解释:

存放备份的文件夹是/home/mongoDbback/

-u是数据库名用户名 -p是密码 -d是数据库名 具体和window差不多,大家可以看下上面windows的解释。

备份的时候只要运行一下

?

1

. /mongoBeiFen .sh 

就可以了。

数据库恢复:

?

1

/usr/local/mongo/bin/mongorestore -d my_mongodb my_mongodb_dump /my_mongodb/ *

指向每个文件

 

复制代码 代码如下:


/usr/local/mongo/bin/mongorestore -h 127.0.0.1 --port 27017 -- drop --directoryperdb  my_mongodb_dump/my_mongodb

 

 

 

指向一个目录
有问题的话可以尝试window的写法:

 

复制代码 代码如下:

 

/usr/local/mongo/bin/mongorestore -h 127.0.0.1:27017 -d test --drop  --directoryperdb  my_mongodb_dump/my_mongodb

 

查看更多关于Windows或Linux系统中备份和恢复MongoDB数据的教程的详细内容...

  阅读:21次