dokku / dokku-mongo

a mongo plugin for dokku
MIT License
174 stars 32 forks source link

How to import data from he dokku host? #76

Closed timaschew closed 7 years ago

timaschew commented 7 years ago

As I commented here: https://github.com/dokku/dokku-mongo/issues/52#issuecomment-254660447

So it's not working the way the docs are telling how to use import and export. I was thinking of using dokku mongo:connect <name> but the dump is not part of the app. The dump is only available on the dokku host, I don't want to add the dump to the git repo.

How can I import data?

josegonzalez commented 7 years ago

What happens when you use import/export?

timaschew commented 7 years ago

Wow, thank you for the fast feedback :)

Because I want to import from a local mongodb (installed locally on my OS X) I used both tools mongoexport and mongodump to create the dump/archive.

mongoexport

mongoexport --db sas --collection documents --out documents.json
2016-10-19T01:17:57.963+0200    connected to: localhost
2016-10-19T01:17:58.351+0200    exported 326 records

result:

dokku mongo:import sas < documents.json
tar: This does not look like a tar archive
tar: Skipping to next header
tar: Exiting with failure status due to previous errors

But even when I try to put the json file into a dump directory and make a tarball I get this error:

root@dokku:/home/user# dokku mongo:import sas < documents.dump.tar
tar: This does not look like a tar archive
tar: Skipping to next header
tar: Exiting with failure status due to previous errors

mongodump

mongodump --archive=sas.archive --db sas
2016-10-19T01:21:35.177+0200    writing sas.documents to archive 'sas.archive'
2016-10-19T01:21:35.270+0200    done dumping sas.documents (326 documents)

result:

dokku mongo:import sas < sas.archive
tar: This does not look like a tar archive
tar: Skipping to next header
tar: Exiting with failure status due to previous errors

I tried to do a a dokku mongo:export from dokku managed mongodb, just to see how the file should look like. Even if the database is empty (no documents) the output is this:

dokku mongo:export sas
./0000700000000000000000000000000013001527402007671 5ustar  

And if I save it as a tar file I can't extract it, I get this error:

ar -xfv test.tar
tar: v: Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
timaschew commented 7 years ago

For now I have a workaround:

  1. create export via mongoexport
  2. copy file to dokku host via scp
  3. copy file from dokku host to container via docker cp
  4. run custom import script via dokku enter
timaschew commented 7 years ago

Okay found a solution

  1. Make a dump via dokku mongo:export mongodb-sas > dump-2016-12-31 BTW: the output has weird whitespaces, some of them should be newlines

  2. Copy file via scp to local machine

  3. Extract archive via tar -xvf dump-2016-12-31.tar Output looks like this

    x ./
    x ./mongodb-sas/
    x ./mongodb-sas/documents.bson
    x ./mongodb-sas/customer-sessions.bson
    x ./mongodb-sas/customer-sessions.metadata.json
    x ./mongodb-sas/documents.metadata.json
  4. use mongorestore to import a collection on your local mongodb

mongorestore --collection documents --db sas dump-2016-12-31/mongodb-sas/documents.bson
timaschew commented 2 years ago

I needed do this again and I found something weird. When doing dokku mongo:export I can't use dokku mongo:import (error: Failed: gzip: invalid header) and also not mongoimport, because of this error:

2021-10-12T21:23:05.797+0000    no collection specified
2021-10-12T21:23:05.798+0000    using filename '' as collection
2021-10-12T21:23:05.798+0000    error validating settings: invalid collection name: collection name cannot be an empty string
2021-10-12T21:23:05.799+0000    try 'mongoimport --help' for more information

but mongorestore works fine!

Here is the full command:

tar -xvf mongodb-sas-2021-10-12.tar
docker cp mongodb-sas-2021-10-12 CONTAINER_ID:/mongodb-sas-dump # lookup container id via docker ps
dokku mongo:enter mongodb-sas
mongorestore --db mongodb_sas --uri=mongodb://mongodb-sas:****@localhost:27017 mongodb-sas-dump/

The directory mongodb-sas-dump contains the extracted tar archive from dokku mongo:export.

josegonzalez commented 2 years ago

what was the dokku command you ran?

timaschew commented 2 years ago

Hm, it looks I was wrong, this works fine (now):