Skip to content

Instantly share code, notes, and snippets.

@JulienSansot
Last active April 18, 2022 03:07
Show Gist options
  • Save JulienSansot/d5161c4172f26065e413 to your computer and use it in GitHub Desktop.
Save JulienSansot/d5161c4172f26065e413 to your computer and use it in GitHub Desktop.
mongodb dump and restore
#with docker
docker run -it -v "$PWD"/mongo_dump:/data/dump --rm mongo:3.0.5 sh -c 'exec mongodump --out /data/dump --host=1.2.3.4 --db the_database'
docker run -it -v "$PWD"/mongo_dump:/data/dump --rm mongo:3.0.5 sh -c 'exec mongorestore --host=1.2.3.4 --db the_database /data/dump/the_database --drop'
#dump
sudo mongodump
#restore (droping previous data)
sudo mongorestore
#options
#--host mongodb1.example.net --port 3017 --username user --password pass --out /opt/backup/mongodump-2013-10-24
You may also consider using the mongorestore --objcheck option to check the integrity of objects while inserting them into the database, or you may consider the mongorestore --drop option to drop each collection from the database before restoring from backups.
# dump mongodb
# get the date from 1.2.3.4
sudo mongodump --host=1.2.3.4 --db db
# restore mongodb:
# restore to 1.2.3.4
mongorestore --host 1.2.3.4 --db db dump/db/ --drop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment