Sync MongoDB Via ssh

后端 未结 3 1443
走了就别回头了
走了就别回头了 2020-12-22 21:01

Unlike Mysql, I found it quite challenging trying to sync MongoDB files -
They can not be piped back, since they don\'t send the data to stdout
(If I understand corr

3条回答
  •  没有蜡笔的小新
    2020-12-22 21:26

    I don't really like to have one database connect to another - IMHO it breaks the separation of environments and makes it complicated to automate such scripts.

    My solution is to use the mongo dump/restore which use "dump directories" and to use tar for streaming the files. A trivial implementation (for copying from one remote to another remote) might look like this:

    ssh remote1 'mongodump > /dev/null && tar -zc dump && rm -rf dump' | \
      ssh remote2 'tar -zx && mongorestore dump && rm -rf dump'
    

    Notes:

    1. that both mongodump and mongorestore has very verbose output, but mongodump's will both mess with the tar streaming and also apparently mongodump will actually refuse to work if you run without a pseudo-terminal and without output redirection.
    2. mongodump has an option to dump to stdout, but I couldn't figure out what kind of format it uses and I didn't understand how to get mongorestore to load that.

提交回复
热议问题