Skip to content

Instantly share code, notes, and snippets.

@L04DB4L4NC3R
Forked from lsm/export_gridfs.sh
Last active September 5, 2019 21:56
Show Gist options
  • Save L04DB4L4NC3R/5eb0ea9eb8f6ed5565b21768cc8ef804 to your computer and use it in GitHub Desktop.
Save L04DB4L4NC3R/5eb0ea9eb8f6ed5565b21768cc8ef804 to your computer and use it in GitHub Desktop.
Export files from MongoDB GridFS with directory paths

So mongodb stores every document in the form of bson. A bson document has a max limit of 16 MB. Therefore mongodb uses something known as gridFS when saving huge files. It creates fs.chunks for storing base64 files and fs.files to store file metadata.

mongodump creates a dump of the whole DB. Like a bson snapshot. But due to version changes we have to run it in a docker container which has mongoDB 4.0 installed. It shows permission denied while creating a file. So I just exec'ed into the container to run the mongodump command.

docker run --rm -v $(pwd):/workdir/ -w /workdir/ mongo:4.0 mongodump -h server -d $database --out /workdir/dump/
#!/bin/bash
# http://www.vladimirm.com/blog/2011/06/export-files-from-mongodb-gridfs-with-directory-paths/
_host="${1:?Usage: gridfs host db}"
_db="${2:?Usage: gridfs host db}"
while read -r line; do
file=$(echo "$line" | awk -F'\t' '{ print $1 }')
[[ $file == 'connected to'* ]] && continue
directory=${file%/*}
mkdir -p $directory
mongofiles -h $_host -d $_db get $file
done < <(mongofiles -h $_host -d $_db list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment