Skip to content

Instantly share code, notes, and snippets.

@lsm
Created June 30, 2011 11:20
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lsm/1056037 to your computer and use it in GitHub Desktop.
Save lsm/1056037 to your computer and use it in GitHub Desktop.
Export files from MongoDB GridFS with directory paths
#!/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 -db $_db get $file
done < <(mongofiles -h $_host -db $_db list)
@asm74
Copy link

asm74 commented Nov 19, 2015

any file with spaces in file name won't be exported. Need to replace $file with "$file"

@uapasha
Copy link

uapasha commented Dec 20, 2016

For those looking for answer in 2016. For a recent mongo db it is usefull to use someting like this:

#!/bin/bash

_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
    mongofiles -h $_host -d $_db --port 3001 get "$file"
done < <(mongofiles -h $_host -d $_db --port 3001 list)

Besides if you are working with something like meteor collectionFS you can add --prefix=cfs_gridfs.images or similar parameter to mongofiles command strings

@Samigos
Copy link

Samigos commented Dec 12, 2019

Hey guys! I have a NodeJS app that stores all of its data, even images, to a MongoDB server. Now I want to use an S3 to store these images (43gb) and so, I need to transfer them there.

Whilst I have figured out how to transfer files from a server to an S3 using the s3cmd tool, I have NO idea how to export my images from the db! I know they are stored in the fs.files and fs.chunks collections, but I can't figure out how to actually use them as input to s3cmd!

I found this script, but I'm not sure how to use it... Any help will be much appreciated!!!

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