我想用bash自动化我的mongo备份过程。我的计划是获取mongo上的所有数据库名,并将其存储到一个名为“Databasename.txt”的文件中。然后,我想从该文件中转储具有for循环的数据库,并将备份保存到其他目录。
问题是:
MongoDB shell version v5.0.9
connecting to: mongodb://127.0.0.1:27017/?authSource=admin&compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("some-random-string") }
MongoDB server version: 5.0.9
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
admin 0.000GB
config 0.000GB
local 0.000GB
database1 0.000GB
database2 0.000GB
bye我不需要这些信息,只想保存我的数据库名。我已经寻找了所有的可能性,但仍然没有完成。我该怎么做?
发布于 2022-07-14 06:36:51
试试mongo --quiet
但是,它仍然打印弃用警告。将它与--eval结合使用,但是命令show dbs是不可能的。
mongo --quiet --eval 'show dbs'
uncaught exception: SyntaxError: unexpected token: identifier :
@(shell eval):1:5
exiting with code -4所以,最后的解决方案是Mongo.getDBNames()或listDatabases。
mongo --quiet --eval "db.adminCommand( { listDatabases: 1 } ).databases.map(x => x.name).join('\n')" > database-name.txt
mongo --quiet --eval "db.getMongo().getDBNames().join('\n')" > database-name.txt应该返回的文件
admin
config
local
database1
database2https://stackoverflow.com/questions/72975409
复制相似问题