Skip to content

Instantly share code, notes, and snippets.

@lsm lsm/export_gridfs.sh
Created Jun 30, 2011

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

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.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.