Last active
January 31, 2016 10:46
-
-
Save vaclavsvejcar/c1b1d53a082975cad78c to your computer and use it in GitHub Desktop.
Command line utility for uploading directory structure into GridFS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
## Command line utility for uploading directory structure into GridFS | |
## Copyright (c) 2015-2016 norcane | |
## | |
## Authors: | |
## - Vaclav Svejcar ([email protected]) | |
## ------------------------------------------------------------------ | |
## This program is free software: you can redistribute it and/or modify | |
## it under the terms of the GNU General Public License as published by | |
## the Free Software Foundation, either version 3 of the License, or | |
## (at your option) any later version. | |
## | |
## This program is distributed in the hope that it will be useful, | |
## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
## GNU General Public License for more details. | |
## | |
## You should have received a copy of the GNU General Public License | |
## along with this program. If not, see <http://www.gnu.org/licenses/>. | |
## ------------------------------------------------------------------ | |
## Whether to remove all files from GridFS before upload, defaults to 'false' | |
REMOVE_FILES=false | |
## Parse the command line arguments | |
while [[ $# > 0 ]] | |
do | |
key="$1" | |
case $key in | |
## Represents the directory to upload files from | |
-d|--directory) | |
BASE_DIR="${2%/}" | |
shift | |
;; | |
## Represents the database name | |
-n|--db-name) | |
DB_NAME="$2" | |
shift | |
;; | |
## Represents the option wheter to remove all files from GridFS | |
-r|--remove-all-files) | |
REMOVE_FILES=true | |
;; | |
## Represents whether to show the 'help' text instead | |
-h|--help) | |
echo "Command line utility for uploading directory structure into GridFS" | |
echo "Copyright (c) 2015-2016 norcane" | |
echo "------------------------------------------------------------------" | |
echo "This program is free software: you can redistribute it and/or modify" | |
echo "it under the terms of the GNU General Public License as published by" | |
echo "the Free Software Foundation, either version 3 of the License, or" | |
echo "(at your option) any later version." | |
echo "" | |
echo "This program is distributed in the hope that it will be useful," | |
echo "but WITHOUT ANY WARRANTY; without even the implied warranty of" | |
echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the" | |
echo "GNU General Public License for more details." | |
echo | |
echo "You should have received a copy of the GNU General Public License" | |
echo "along with this program. If not, see <http://www.gnu.org/licenses/>." | |
echo "------------------------------------------------------------------" | |
echo "OPTIONS:" | |
echo -e "\t-d|--directory <path_to_dir> - directory to upload files from" | |
echo -e "\t-n|--db-name <db_name> - name of the database" | |
echo -e "\t-r|--remove-all-files - remove all existing files before uplading" | |
exit | |
;; | |
*) | |
;; | |
esac | |
shift | |
done | |
## For all files within the BASE_DIR directory... | |
find $BASE_DIR -type f | while read FILE; do | |
## 1/ Get the virtual filename & file content type | |
FILE_FULL=${FILE#$BASE_DIR} | |
CONTENT_TYPE=`file -b --mime-type $FILE` | |
## 2/ If '-r|--remove-all-files' option present, remove all files from GridFS | |
if [ $REMOVE_FILES = true ] ; then | |
echo -e "] Removing all files from GridFS" | |
mongo $DB_NAME --eval "db.getCollection('fs.files').remove({})" >/dev/null | |
mongo $DB_NAME --eval "db.getCollection('fs.chunks').remove({})" >/dev/null | |
fi | |
## 3/ Upload files into the GridFS | |
echo "] Uploading file '$FILE' ($CONTENT_TYPE) to database '$DB_NAME' as '$FILE_FULL'" | |
mongofiles put $FILE_FULL -l $FILE --db $DB_NAME --type $CONTENT_TYPE --replace &>/dev/null | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment