如何在服务器上使用golang进行mongodump,并将其传输到我的本地机器上?

huangapple go评论96阅读模式
英文:

How do I do mongodump on a server and transfer that to my local machine using golang?

问题

我写了一个Go程序(基本上是这个例子中的代码,https://stackoverflow.com/questions/21417223/simple-ssh-port-forward-in-golang),通过创建SSH隧道连接到远程MongoDB服务器。我可以使用mgo API查询服务器。现在,我想将数据库的几个集合复制到我的本地机器上,并在本地查询,而不是每次都查询服务器。需要注意的是,由于存在端口转发,我不能直接将其复制到我的本地机器上。我该如何实现这一点?

英文:

I wrote a go program ( which is basically the code in this example, https://stackoverflow.com/questions/21417223/simple-ssh-port-forward-in-golang ) that connects to a remote mongodb server by creating an ssh tunnel. I can query the server using mgo api. Now, instead of querying the server everytime, I want to copy a few collections of the database to my local machine and query locally. It is important to note that I cannot copy it directly to my local machine as there is port forwarding involved. How can I achieve this ?

答案1

得分: 3

你正在重新发明轮子。使用cron创建一个SSH隧道到目标,然后使用本地的mongodump连接到你的本地隧道端点。

ssh -f user@mongodb.example.com -L 27017:mongodb.example.com:27017 -N
mongodump localhost:27017 <你的选项在这里>
英文:

You are reinventing the wheel. Use cron to create an ssh tunnel to the destination and then use a local mongodump to connect against your local tunnel endpoint

ssh -f user@mongodb.example.com -L 27017:mongodb.example.com:27017 -N
mongodump localhost:27017 &lt;your opts here&gt;

huangapple
  • 本文由 发表于 2017年8月2日 11:24:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/45450931.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定