英文:
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 <your opts here>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论