Skip to content

Instantly share code, notes, and snippets.

@valorad
Last active January 8, 2022 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valorad/f425126ed95ff1b30ddd4b5d64599009 to your computer and use it in GitHub Desktop.
Save valorad/f425126ed95ff1b30ddd4b5d64599009 to your computer and use it in GitHub Desktop.
Mongo Backup Collection
mongoContainerOutput="/path/to/container/output/folder"
hostOutput="/path/to/host/output/folder"
mongoUser="root"
mongoPassword='mongo-db-user-password'
mongoHost="127.0.0.1:27017"
mongoCollectionAuth="admin"
mongoCollectionDB="dbCollectionName"
dockerContainer="mongo_5_1"
mongoURI="mongodb://$mongoUser:$mongoPassword@$mongoHost/$mongoCollectionDB?authSource=$mongoCollectionAuth"
rm -rf $hostOutput/"$mongoCollectionDB"
docker exec -it $dockerContainer mongodump --uri $mongoURI -o $mongoContainerOutput
sudo chown $UID:$UID -R $hostOutput/"$mongoCollectionDB"
tar -czvf $hostOutput/"$mongoCollectionDB-$(date '+%Y%m%d-%s').tar.gz" -C "$hostOutput/$mongoCollectionDB/" .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment