mongodb Mongorestore 到不同的数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36321899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mongorestore to a different database
提问by Cyker
In MongoDB, is it possible to dump a database and restore the content to a different database? For example like this:
在 MongoDB 中,是否可以转储数据库并将内容恢复到不同的数据库?例如像这样:
mongodump --db db1 --out dumpdir
mongorestore --db db2 --dir dumpdir
But it doesn't work. Here's the error message:
但它不起作用。这是错误消息:
building a list of collections to restore from dumpdir dir
don't know what to do with subdirectory "dumpdir/db1", skipping...
done
构建要从 dumpdir 目录恢复的集合列表
不知道如何处理子目录“dumpdir/db1”,跳过...
完毕
回答by Blakes Seven
You need to actually point at the "database name" containerdirectory "within" the output directory from the previous dump:
您需要实际指向上一个转储输出目录“内”的“数据库名称”容器目录:
mongorestore -d db2 dumpdir/db1
And usually just <path> is fine as a positional argument rather than with -dir
which would only be needed when "out of position" i.e "in the middle of the arguments list".
通常只是 <path> 作为位置参数很好,而不是-dir
只有在“不在位置”时才需要,即“在参数列表的中间”。
p.s. For archive backup file (testedwith mongorestore v3.4.10)
ps 用于存档备份文件(使用 mongorestore v3.4.10测试)
mongorestore --gzip --archive=${BACKUP_FILE_GZ} --nsFrom "${DB_NAME}.*" --nsTo "${DB_NAME_RESTORE}.*"
回答by user1587520
In addition to the answer of Blakes Seven, if your databases use authentication I got this to work using the --uri
option, which requires a recent mongo version (>3.4.6):
除了 Blakes Seven 的回答之外,如果您的数据库使用身份验证,我可以使用该--uri
选项使其工作,该选项需要最近的 mongo 版本(> 3.4.6):
mongodump --uri="mongodb://$sourceUser:$sourcePwd@$sourceHost/$sourceDb" --gzip --archive | mongorestore --uri="mongodb://$targetUser:$targetPwd@$targetHost/$targetDb" --nsFrom="$sourceDb.*" --nsTo="$targetDb.*" --gzip --archive
回答by Greg
Thank you! @Blakes Seven
谢谢!@布莱克七
Adding Docker notes: container names are interchangeable with container ID's
添加 Docker 注释:容器名称可与容器 ID 互换
(assumes authenticated, assumes named container=my_db and new_db)
(假设已通过身份验证,假设命名为 container=my_db 和 new_db)
dump:
倾倒:
docker exec -it my_db bash -c "mongodump --uri mongodb://db:password@localhost:27017/my_db --archive --gzip | cat > /tmp/backup.gz"
copy to workstation:
复制到工作站:
docker cp my_db:/tmp/backup.gz c:\backups\backup.gz
copy into new container(form backups folder):
复制到新容器(形成备份文件夹):
docker cp .\backup.gz new_db:/tmp
restore from container tmp folder:
从容器 tmp 文件夹中恢复:
docker exec -it new_db bash -c "mongorestore --uri mongodb://db:password@localhost:27017/new_db --nsFrom 'my_db.*' --nsTo 'new_db.*' --gzip --archive=/tmp/backup.gz"