How to export files from MongoDb GridFS

(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com, or follow me on Twitter.

by Vladimir Momirov: This is an awesome script:

The biggest problem is that I used GridFS to store uploaded files. After unsuccessfully searching online I decided to write my own bash export script. How to export all the files out of mongodb gridfs? Here’s the script:

#! /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
directory=${file%/*}
mkdir -p $directory
mongofiles -h $_host -db $_db get $file
done < <(mongofiles -h $_host -db $_db list) Script takes two parameters, first one is the hostname of the mongodb instance and the second is database. For example: ./gridfs localhost scen will export all files from scen database on localhost to current directory, if you have directory structure in GridFS, it will create that directories too.

Post external references

  1. 1
    http://blog.vladimirm.com/2011/06/export-files-from-mongodb-gridfs-with-directory-paths/
Source