Jos*_*ine 4 mongodb backup scripting
我希望将 mongodump 运行到辅助(不是主)。所以我需要作为输出主机名和一个辅助端口。
# ./mongo -udbadm admin --port 27100 -p --quiet --eval "printjson(rs.status().members.map(function(m) { return {'name':m.name, 'stateStr':m.stateStr} }))"
Enter password:
[
{
"name" : "example-1.domain:27200",
"stateStr" : "SECONDARY"
},
{
"name" : "example-2.domain:27200",
"stateStr" : "PRIMARY"
},
{
"name" : "example-3.domain:27200",
"stateStr" : "SECONDARY"
}
]
Run Code Online (Sandbox Code Playgroud)
使用 shell 脚本工具,我可以毫无问题地提取带有主机名和端口的辅助设备。我的问题是如何以 MongoDB 方式完成这项工作?您推荐我什么工具?我听说这jq
对于 JSON 数据更好。但不知道如何仅提取一个辅助主机。
值得注意的是,如果您将副本集字符串传递给它,则默认从集合中的辅助副本读取(注意:在 3.0.5+ 中,如果您连接到分片集群中的mongodump
a,则情况并非如此)。mongos
如果您的副本集名称是 repl1,它将类似于:
./mongodump --host repl1/example-1.domain:27200,example-2.domain:27200
Run Code Online (Sandbox Code Playgroud)
作为参考,在较新的版本中(因为工具在 中重新编写Go
),这是通过在未连接到 a 时设置Monotonic
模式来完成的(来源mongos
来自 3.0.6))。
要回答您的具体问题,解析 rs.status() 的一种非常简单的方法是使用过滤器,它将适合一行:
rs.status().members.filter(function(rsStatus) { return rsStatus.state === 2;})[0].name
Run Code Online (Sandbox Code Playgroud)
这将返回在返回的成员数组中找到的第一个(因此[0]
)次要( )的名称字段state === 2
rs.status()