「基礎入門」MongoDBデータバックアップ、リストアおよび移行管理

1.全てのデータベースをバックアップ
mkdir testbak
cd testbak
mongodump
ディレクトリ:./dump/[databasename]/[collectionname].bson

2.指定データベースをバックアップ
mongodump -d pagedb

3.データベースのコレクションをバックアップ
mongodump -d pagedb -c page

4.全てのデータベースを復元
cd testbak
mongorestore –drop

5.指定DBのデータを復元
cd testbak
mongorestore -d pagedb –drop

6.指定DBのコレクションのデータを復元
cd testbak
mongorestore -d pagedb -c page –drop

7.MongoDBにデータをインポート
mongoimport -d pagedb -c page –type csv –headerline –drop < csvORtsvFile.csv
ヘルプ参考
mongoimport –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 ( /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
–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
-d [ –db ] arg database to use
-c [ –collection ] arg collection to use (some commands)
-f [ –fields ] arg comma separated list of field names e.g. -f name,age
–fieldFile arg file with fields names – 1 per line
–ignoreBlanks if given, empty fields in csv and tsv will be ignored
–type arg type of file to import. default: json (json,csv,tsv)
–file arg file to import from; if not specified stdin is used
–drop drop collection first
–headerline CSV,TSV only – use first line as headers
–upsert insert or update objects that already exist
–upsertFields arg comma-separated fields for the query part of the
upsert. You should make sure this is indexed
–stopOnError stop importing at first error rather than continuing
–jsonArray load a json array, not one item per line. Currently
limited to 4MB.

8.MongoDBからデータをエクスポート
mongoexport -d pagedb -c page -q {} -f _id,title,url,spiderName,pubDate –csv > pages.csv
クエリ条件を指定する場合、下記を実行する
mongoexport -d page -c Article -q '{“spiderName": “mafengwoSpider"}’ -f _id,title,content,images,publishDate,spiderName,url –jsonArray > mafengwoArticle.txt

9.リモート接続管理

9.1 mongoでリモート接続
mongo -u admin -p admin 192.168.0.199:27017/pagedb
ヘルプコマンド
mongo –help
MongoDB shell version: 1.8.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
–verbose increase verbosity
–ipv6 enable IPv6 support (disabled by default)
9.2 MongoDBに基づきjavascriptでリモート接続
> var x = new Mongo('192.168.0.197:27017’)
> var ydb = x.getDB('pagedb’);
> use ydb
switched to db ydb
> db
ydb
> ydb.page.findOne()
{
“_id" : ObjectId(“4eded6a5bf3bfa0014000003"),
“content" : “arkgame.com is a good site.",
“pubdate" : “2015-03-19",
“title" : “manager is a poor boy and live unhappy “,
“url" : “https://arkgame.com"
}

DataBase

Posted by arkgame