Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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
You can’t perform that action at this time.