【MongoDB之数据备份与恢复】

[root@master ~]# cd /opt/mongodb

[root@master mongodb]# ls

bin  data  GNU-AGPL-3.0  log  README  THIRD-PARTY-NOTICES

[root@master mongodb]# cd bin/

[root@master bin]# l

-bash: l: command not found

[root@master bin]# ls

bsondump  mongo  mongod  mongodump  mongoexport  mongofiles  mongoimport  mongorestore  mongos  mongosniff  mongostat

[root@master bin]# ./mongod help

Invalid command: help

Allowed options:

General options:

  -h [ --help ]         show this usage information

  --version             show version information

  -f [ --config ] arg   configuration file specifying additional options

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

                        verbosity e.g. -vvvvv)

  --quiet               quieter output

  --port arg            specify port number

  --bind_ip arg         comma separated list of ip addresses to listen on - all

                        local ips by default

  --logpath arg         file to send all output to instead of stdout

  --logappend           append to logpath instead of over-writing

  --pidfilepath arg     full path to pidfile (if not set, no pidfile is 

                        created)

  --fork                fork server process

  --dbpath arg          directory for datafiles

  --directoryperdb      each database will be stored in a separate directory

  --repairpath arg      root directory for repair files - defaults to dbpath

  --cpu                 periodically show cpu and iowait utilization

  --noauth              run without security

  --auth                run with security

  --objcheck            inspect client data for validity on receipt

  --quota               enable db quota management

  --quotaFiles arg      number of files allower per db, requires --quota

  --appsrvpath arg      root directory for the babble app server

  --nocursors           diagnostic/debugging option

  --nohints             ignore query hints

  --nohttpinterface     disable http interface

  --rest                turn on simple rest api

  --noscripting         disable scripting engine

  --noprealloc          disable data file preallocation

  --smallfiles          use a smaller default file size

  --nssize arg (=16)    .ns file size (in MB) for new databases

  --diaglog arg         0=off 1=W 2=R 3=both 7=W+some reads

  --sysinfo             print some diagnostic system information

  --upgrade             upgrade db if needed

  --repair              run repair on all dbs

  --notablescan         do not allow table scans

  --syncdelay arg (=60) seconds between disk syncs (0=never, but not 

                        recommended)

  --profile arg         0=off 1=slow, 2=all

  --slowms arg (=100)   value of slow for profile and console log

  --maxConns arg        max number of simultaneous connections

  --nounixsocket        disable listening on unix sockets

  --ipv6                enable IPv6 support (disabled by default)

Replication options:

  --master              master mode

  --slave               slave mode

  --source arg          when slave: specify master as <server:port>

  --only arg            when slave: specify a single database to replicate

  --pairwith arg        address of server to pair with

  --arbiter arg         address of arbiter server

  --slavedelay arg      specify delay (in seconds) to be used when applying 

                        master ops to slave

  --fastsync            indicate that this instance is starting from a dbpath 

                        snapshot of the repl peer

  --autoresync          automatically resync if slave data is stale

  --oplogSize arg       size limit (in MB) for op log

  --opIdMem arg         size limit (in bytes) for in memory storage of op ids

Sharding options:

  --configsvr           declare this is a config db of a cluster

  --shardsvr            declare this is a shard db of a cluster

  --noMoveParanoia      turn off paranoid saving of data for moveChunk.  this 

                        is on by default for now, but default will switch

[root@master bin]# cd ../

[root@master mongodb]# vu start_mongo

-bash: vu: command not found

[root@master mongodb]# vi  start_mongo

#!/bin/bash

bin/mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --logappend --port=6699 --fork

~

~

[root@master mongodb]# netstat -tnpl

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   

tcp        0      0 0.0.0.0:868                 0.0.0.0:*                   LISTEN      2806/rpc.statd      

tcp        0      0 0.0.0.0:6699                0.0.0.0:*                   LISTEN      3519/mongod         

tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      2765/portmap        

tcp        0      0 0.0.0.0:7699                0.0.0.0:*                   LISTEN      3519/mongod         

tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      3080/cupsd          

tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      3138/sendmail: acce 

tcp        0      0 :::22                       :::*                        LISTEN      3051/sshd           

[root@master mongodb]# cd bin/

[root@master bin]# ls

bsondump  mongo  mongod  mongodump  mongoexport  mongofiles  mongoimport  mongorestore  mongos  mongosniff  mongostat

[root@master bin]# mongo --help

-bash: mongo: command not found

[root@master bin]# ./mongo --help

MongoDB shell version: 1.6.3

usage: ./mongo [options] [db address] [file names (ending in .js)]

db address can be:

  foo                   foo database on local machine

  192.169.0.5/foo       foo database on 192.168.0.5 machine

  192.169.0.5:9999/foo  foo database on 192.168.0.5 machine on port 9999

options:

  --shell               run the shell after executing files

  --nodb                don't connect to mongod on startup - no 'db address' 

                        arg expected

  --quiet               be less chatty

  --port arg            port to connect to

  --host arg            server to connect to

  --eval arg            evaluate javascript

  -u [ --username ] arg username for authentication

  -p [ --password ] arg password for authentication

  -h [ --help ]         show this usage information

  --version             show version information

  --ipv6                enable IPv6 support (disabled by default)

file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified

[root@master bin]# ./bsondump --help

usage: ./bsondump [options] <bson filename>

options:

  --help                  produce help message

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

                          verbosity e.g. -vvvvv)

  -h [ --host ] arg       mongo host to connect to ("left,right" for pairs)

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

  -d [ --db ] arg         database to use

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

  -u [ --username ] arg   username

  -p [ --password ] arg   password

  --ipv6                  enable IPv6 support (disabled by default)

  --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        if dbpath specified, each db is in a separate 

                          directory

  --objcheck              validate object before inserting

  --filter arg            filter to apply before inserting

  --type arg (=json)      type of output: json,debug

[root@master bin]# ./mongodump  --help

options:

  --help                   produce help message

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

                           verbosity e.g. -vvvvv)

  -h [ --host ] arg        mongo host to connect to ("left,right" for pairs)

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

  -d [ --db ] arg          database to use

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

  -u [ --username ] arg    username

  -p [ --password ] arg    password

  --ipv6                   enable IPv6 support (disabled by default)

  --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         if dbpath specified, each db is in a separate 

                           directory

  -o [ --out ] arg (=dump) output directory

  -q [ --query ] arg       json query

[root@master bin]# ./mongorestore --help

usage: ./mongorestore [options] [directory or filename to restore from]

options:

  --help                  produce help message

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

                          verbosity e.g. -vvvvv)

  -h [ --host ] arg       mongo host to connect to ("left,right" for pairs)

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

  -d [ --db ] arg         database to use

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

  -u [ --username ] arg   username

  -p [ --password ] arg   password

  --ipv6                  enable IPv6 support (disabled by default)

  --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        if dbpath specified, each db is in a separate 

                          directory

  --objcheck              validate object before inserting

  --filter arg            filter to apply before inserting

  --drop                  drop each collection before import

  --indexesLast           wait to add indexes (faster if data isn't inserted in

                          index order)

[root@master bin]# ./mongostat --help

usage: ./mongostat [options] [sleep time]

sleep time: time to wait (in seconds) between calls

options:

  --help                     produce help message

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

                             verbosity e.g. -vvvvv)

  -h [ --host ] arg          mongo host to connect to ("left,right" for pairs)

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

  -d [ --db ] arg            database to use

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

  -u [ --username ] arg      username

  -p [ --password ] arg      password

  --ipv6                     enable IPv6 support (disabled by default)

  --noheaders                don't output column names

  -n [ --rowcount ] arg (=0) number of stats lines to print (0 for indefinite)

  --http                     use http instead of raw db connection

 Fields

   inserts/s    - # of inserts per second

   query/s      - # of queries per second

   update/s     - # of updates per second

   delete/s     - # of deletes per second

   getmore/s    - # of get mores (cursor batch) per second

   command/s    - # of commands per second

   flushes/s    - # of fsync flushes per second

   mapped       - amount of data mmaped (total data size) megabytes

   visze        - virtual size of process in megabytes

   res          - resident size of process in megabytes

   faults/s     - # of pages faults/sec (linux only)

   locked       - percent of time in global write lock

   idx miss     - percent of btree page misses (sampled)

   q t|r|w      - ops waiting for lock from db.currentOp() (total|read|write)

   conn         - number of open connections

[root@master bin]# ./mongos --help

Sharding options:

  -h [ --help ]         show this usage information

  --version             show version information

  -f [ --config ] arg   configuration file specifying additional options

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

                        verbosity e.g. -vvvvv)

  --quiet               quieter output

  --port arg            specify port number

  --bind_ip arg         comma separated list of ip addresses to listen on - all

                        local ips by default

  --logpath arg         file to send all output to instead of stdout

  --logappend           append to logpath instead of over-writing

  --pidfilepath arg     full path to pidfile (if not set, no pidfile is 

                        created)

  --fork                fork server process

  --configdb arg        1 or 3 comma separated config servers

  --test                just run unit tests

  --upgrade             upgrade meta data version

  --chunkSize arg       maximum amount of data per chunk

  --ipv6                enable IPv6 support (disabled by default)

[root@master bin]# ./mongo --port 6699

MongoDB shell version: 1.6.3

connecting to: 127.0.0.1:6699/test

> show dbs;

admin

local

test_mongo

> use test_mongo;

switched to db test_mongo

> show collections;

system.indexes

test

> db.test.find(); 

{ "_id" : ObjectId("595e3ca9e2baea15d0c694b9"), "id" : 2, "name" : "绋娣2" }

{ "_id" : ObjectId("595e3cb4e2baea15d0c694ba"), "id" : 1, "name" : "绋娣1" }

{ "_id" : ObjectId("595e3cc0e2baea15d0c694bb"), "id" : 3, "name" : "绋娣3" }



 

MongoDB 备份(mongodump)与恢复(mongorestore)

MongoDB数据备份

在Mongodb中我们使用mongodump命令来备份MongoDB数据。该命令可以导出所有数据到指定目录中。

mongodump命令可以通过参数指定导出的数据量级转存的服务器。

语法

mongodump命令脚本语法如下:

>mongodump -h dbhost -d dbname -o dbdirectory

-h:MongDB所在服务器地址,例如:127.0.0.1,当然也可以指定端口号:127.0.0.1:27017

-d:需要备份的数据库实例,例如:test

-o:备份的数据存放位置,例如:c:\data\dump,当然该目录需要提前建立,在备份完成后,系统自动在dump目录下建立一个test目录,这个目录里面存放该数据库实例的备份数据。



 

MongoDB数据恢复

mongodb使用 mongorestore 命令来恢复备份的数据。

语法

mongorestore命令脚本语法如下:

>mongorestore -h <hostname><:port> -d dbname <path>

--host <:port>, -h <:port>:

MongoDB所在服务器地址,默认为: localhost:27017

--db , -d :  需要恢复的数据库实例,例如:test,当然这个名称也可以和备份时候的不一样,比如test2

--drop:   恢复的时候,先删除当前数据,然后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,慎用哦!

<path>:  mongorestore 最后的一个参数,设置备份数据所在位置,例如:c:\data\dump\test。

你不能同时指定 <path> 和 --dir 选项,--dir也可以设置备份目录。

--dir:  指定备份的目录

你不能同时指定 <path> 和 --dir 选项。

[root@master bin]# 

[root@master bin]# ./mongodump  -d test_mongo -o /tmp/

couldn't connect to [127.0.0.1] couldn't connect to server 127.0.0.1

[root@master bin]# ./mongodump  -d test_mongo -o /tmp/ --port 6699

connected to: 127.0.0.1:6699

DATABASE: test_mongo     to     /tmp/test_mongo

        test_mongo.system.indexes to /tmp/test_mongo/system.indexes.bson

                 1 objects

        test_mongo.test to /tmp/test_mongo/test.bson

                 3 objects

[root@master bin]# ./mongorestore --port  6699 -d local    --directoryperdb  /tmp/test_mongo/

connected to: 127.0.0.1:6699

Thu Jul  6 15:21:11 /tmp/test_mongo/system.indexes.bson

Thu Jul  6 15:21:11      going into namespace [local.system.indexes]

Thu Jul  6 15:21:11      1 objects found

Thu Jul  6 15:21:11 /tmp/test_mongo/test.bson

Thu Jul  6 15:21:11      going into namespace [local.test]

Thu Jul  6 15:21:11      3 objects found

[root@master bin]# 



 

猜你喜欢

转载自gaojingsong.iteye.com/blog/2383503