Skip to content

Instantly share code, notes, and snippets.

@marufshidiq
Created April 12, 2020 07:03
Show Gist options
  • Save marufshidiq/8bb457ae0bbf9e1d06873515b362cfcf to your computer and use it in GitHub Desktop.
Save marufshidiq/8bb457ae0bbf9e1d06873515b362cfcf to your computer and use it in GitHub Desktop.
Backup and Restore mongodb inside docker-compose container

How to backup

docker-compose exec -T <mongodb_service_name> mongodump --archive --gzip --db <mongodb_database> > dump.gz

How to restore

docker-compose exec -T <mongodb_service_name> mongorestore --archive --gzip < dump.gz
@javorosas
Copy link

Pure gold. Thanks for this.

@davichoso
Copy link

omg!!

@mukhtiarahmed
Copy link

Thank you for this

@RaphaelDeLaGhetto
Copy link

Howdy! Thank you for this wonderful howto. I'm sure I've used this solution in the past, but suddenly it doesn't work. I have two Docker configurations managed with docker-compose. I'm trying to dump and restore one database to the other. The docker-compose.yml settings, etc., are identical:

version: '3'
services:
  # ...
  mongo:                       
    image: mongo               
    volumes:                   
      - db_data:/data/db
  #...  
volumes:
  db_data:

At the moment, this is what happens if I execute the instructions you have shared:

2022-05-13T15:04:08.927+0000	Failed: EOF
2022-05-13T15:04:08.928+0000	0 document(s) restored successfully. 0 document(s) failed to restore.

I've tried all manner of trouble-shooting steps found online, but nothing works (--nsInclude, authentication params, etc.). The best I get is:

2022-05-13T15:24:22.680+0000	error connecting to host: could not connect to server: connection() : auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed.

I know this is an old post, but I would be grateful for any suggestions you might have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment