Reputation: 229
Has someone ever managed to restore a mongodump from stdin?
I generated the backup file using the referenced command:
mongodump --archive > file
But the reference never explains in what format the archive is, or how to restore it. Some people say you have to inject it from stdin, but they remains a mystery as there is no evidence that it is actually possible.
As a matter of fact, I did the following tries, without any success:
cat file | mongorestore
mongorestore -vvvvv --archive < file
mongorestore -vvvvv --archive=file
All these commands end up with the same error:
Failed: stream or file does not appear to be a mongodump archive
For information, I managed to restore an archive that was generated the classic way.
Why do I need to use the --archive to stdout?
My mongodb is inside a docker.
The option I'm using now creates the backup file inside the docker, that I then copy to the host, using the docker cp command, so I use twice the space needed (before it's removed inside the container).
Unfortunately, the docker has no mount from the host, and I cannot (yet) restart it to add the option. I was looking for a quick option.
Upvotes: 2
Views: 4084
Reputation: 3673
Docker version
# via archive
docker run --rm -i mongo mongodump --uri mongodb://192.168.0.1:27017/test --archive > dump.archive
docker run --rm -i mongo mongorestore --uri mongodb://127.0.0.1:27017/test --archive --drop < dump.archive
# direct pipe
docker run --rm -i mongo mongodump --uri mongodb://192.168.0.1:27017/test --archive | docker run --rm -i mongo mongorestore --uri mongodb://127.0.0.1:27017/test --archive --drop
Upvotes: 0
Reputation: 181
So according to MongoDB official mongodump doc and mongorestore doc:
To output the dump to the standard output stream in order to pipe to another process, run mongodump with the archive option but omit the filename. To restore from the standard input, run mongorestore with the --archive option but omit the filename.
So actually you don't have to mongodump to file first and then read from it, just simply pipe mongodump and mongorestore like this:
mongodump --archive | mongorestore --archive --drop
However, you may probably get another problem as I did https://stackoverflow.com/a/56550768/3785901.
In my case, I have to use --nsFrom and --nsTo instead of --db, or the mongorestore didn't work as expected. So the final command I successfully execute to mongodump/mongorestore is:
mongodump --host HOST:PORT --db SOURCE_DB --username USERNAME --password PASSWORD --archive | mongorestore --host HOST:PORT --nsFrom 'SOURCE_DB.*' --nsTo 'TARGET_DB.*' --username USERNAME --password PASSWORD --archive --drop
Good luck.
Upvotes: 9