python - 使用 python 转储和恢复 mongo db

标签 python linux mongodb shell

你好,我有一个简单的脚本,它使用来自远程 mongo 服务器的 shell 命令转储 mongo 数据库,然后运行 ​​shell 命令将该数据库恢复到另一台服务器上的开发数据库。

脚本位于/home/ubuntu/mongo,转储位于/home/ubuntu/mongo/dump

这是我的代码:

!/usr/bin/python

import time
import subprocess
import os
import shutil

now=int(time.time())
cmd="mongodump --host  -db chronotrack"
print subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)

cmd="mongorestore --host   -db chronotrack  dump/"
print subprocess.check_output(cmd,stderr=subprocess.STDOUT,shell=True)

#mongorestore --host  --port 3017 --db mongodevdb --username mongodevdb --password YourSecretPwd --drop /backup/dump

这是我的错误:

ERROR: root directory must be a dump of a single database
       when specifying a db name with --db
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)
  --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
  --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
  --journal               enable journaling
  -d [ --db ] arg         database to use
  -c [ --collection ] arg collection to use (some commands)
  --objcheck              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
  --keepIndexVersion      don't upgrade indexes to newest version

最佳答案

找到答案:

"mongorestore --host  -db chronotrack  --drop dump/chronotrack"

关于python - 使用 python 转储和恢复 mongo db,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28504832/

相关文章:

php - 如何识别两个相同的 nohup 命令?

php - 通过 Cron 显示 PHP 脚本运行的次数

javascript - 更新 mongoose 对象数组

python - 从后面 3 个字符后添加 ", "

python - Sphinx 意外的部分标题 - numpydoc

python - 如何在Python中获取容器内存限制?

javascript - MongoDB/Mongoose - 通过执行加法更新文档中的数字

python - 对 python 列表执行批量算术运算

linux - 解析和不解析标志和参数时回显信息

mongodb - golang mgo 编码 bson