SHARE
TWEET

Untitled

a guest Aug 13th, 2016 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. LOADING=false
  2.  
  3. usage()
  4. {
  5.     cat << EOF
  6.     usage: $0 [options] dbname
  7.  
  8.     OPTIONS:
  9.         -h      Show this help.
  10.         -l      Load instead of export
  11.         -u      Mongo username
  12.         -p      Mongo password
  13.         -H      Mongo host string (ex. localhost:27017)
  14. EOF
  15. }
  16.  
  17. while getopts "hlu:p:H:" opt; do
  18.     MAXOPTIND=$OPTIND
  19.    
  20.     case $opt in
  21.         h)
  22.             usage
  23.             exit
  24.             ;;
  25.         l)
  26.             LOADING=true
  27.             ;;
  28.         u)
  29.             USERNAME="$OPTARG"
  30.             ;;
  31.         p)
  32.             PASSWORD="$OPTARG"
  33.             ;;
  34.         H)
  35.             HOST="$OPTARG"
  36.             ;;
  37.         \?)
  38.             echo "Invalid option $opt"
  39.             exit 1
  40.             ;;
  41.     esac
  42. done
  43.  
  44. shift $(($MAXOPTIND-1))
  45.  
  46. if [ -z "$1" ]; then
  47.     echo "Usage: export-mongo [opts] <dbname>"
  48.     exit 1
  49. fi
  50.  
  51. DB="$1"
  52. if [ -z "$HOST" ]; then
  53.     CONN="localhost:27017/$DB"
  54. else
  55.     CONN="$HOST/$DB"
  56. fi
  57.  
  58. ARGS=""
  59. if [ -n "$USERNAME" ]; then
  60.     ARGS="-u $USERNAME"
  61. fi
  62. if [ -n "$PASSWORD" ]; then
  63.     ARGS="$ARGS -p $PASSWORD"
  64. fi
  65.  
  66. echo "*************************** Mongo Export ************************"
  67. echo "**** Host:      $HOST"
  68. echo "**** Database:  $DB"
  69. echo "**** Username:  $USERNAME"
  70. echo "**** Password:  $PASSWORD"
  71. echo "**** Loading:   $LOADING"
  72. echo "*****************************************************************"
  73.  
  74. if $LOADING ; then
  75.     echo "Loading into $CONN"
  76.     tar -xzf $DB.tar.gz
  77.     pushd $DB >/dev/null
  78.  
  79.     for path in *.json; do
  80.         collection=${path%.json}
  81.         echo "Loading into $DB/$collection from $path"
  82.         mongoimport $ARGS -d $DB -c $collection $path
  83.     done
  84.  
  85.     popd >/dev/null
  86.     rm -rf $DB
  87. else
  88.     DATABASE_COLLECTIONS=$(mongo $CONN $ARGS --quiet --eval 'db.getCollectionNames()' | sed 's/,/ /g')
  89.    
  90.     mkdir /tmp/$DB
  91.     pushd /tmp/$DB 2>/dev/null
  92.      
  93.     for collection in $DATABASE_COLLECTIONS; do
  94.         mongoexport --host $HOST -u $USERNAME -p $PASSWORD -db $DB -c $collection --jsonArray -o $collection.json >/dev/null
  95.     done
  96.  
  97.     pushd /tmp 2>/dev/null
  98.     tar -czf "$DB.tar.gz" $DB 2>/dev/null
  99.     popd 2>/dev/null
  100.     popd 2>/dev/null
  101.     mv /tmp/$DB.tar.gz ./ 2>/dev/null
  102.     rm -rf /tmp/$DB 2>/dev/null
  103. fi
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top