<commit_before>#!/usr/bin/env bash


FROM_HOST="localhost:27017"
FROM_DB_NAME="meteor"
DUMP_ROOT_DIR="./.dump"

function print_restore_status {
  local drop_flag=$1
  local drop_enabled=$([[ ${drop_flag} == "" ]] && echo "NO" || echo "YES")
  local _db_restore_status_msg

  read -r -d '' _db_restore_status_msg << EOF
${COLOR_ERROR}${TEXT_BOLD}
######################################
# WARNING!!!                         #
# You may lost data at remote server #
######################################
${COLOR_DEFAULT}
Server: ${SERVER_DESCRIPTION}
URL: ${TEXT_UNDERLINE}${ROOT_URL}${COLOR_DEFAULT}
Mongo: ${MONGO_HOST}
Drop enabled: ${TEXT_BOLD}${COLOR_ERROR}${drop_enabled}${COLOR_DEFAULT}

Are you sure? (Enter 'yes' to continue):
EOF

  printf "${_db_restore_status_msg} "
}

function db-restore_help {
  cat << EOF
This script enables restoring of database from local project directory to remote server

boo db-restore server_name [--no-drop|-D] [-v|--verbose]

Options:
--no-drop|-D  - prevent all collections drop before dump restore
-v|--verbose  - verbose mode (print all logs)
EOF
}

function db-restore {
  local server_name=$1
  source_deploy_conf ${server_name}

  local app_local_db_path="$(pwd)/.meteor/local/db"
  local drop_flag='--drop'
  local output_stream=/dev/null

  # parse script arguments
  while [[ $(($#-1)) -gt 0 ]]; do
    local key="$2"

    case ${key} in
      --no-drop|-D)
      drop_flag=''
      ;;
      -v|--verbose)
      output_stream=/dev/stdout
      ;;
      *)
      echo_error "Unknown option: ${key}"
      exit 1
      ;;
    esac

    shift # past argument or value
  done

  # first get confirmation... just in case :)
  print_restore_status ${drop_flag}

  read CONFIRM

  if [[ ${CONFIRM} =~ ^yes$ ]]; then
    echo "Starting local database server... "
    mongod --dbpath "${app_local_db_path}" > ${output_stream} &

    local mongod_pid=$!
    sleep 20

    echo "Making local database dump..."
    mongodump -h "${FROM_HOST}" -d "${FROM_DB_NAME}" -o "${DUMP_ROOT_DIR}" &> ${output_stream}

    kill ${mongod_pid}

    echo "Restoring database from dump..."

    mongorestore "mongodb://${MONGO_HOST}/${MONGO_DB}${MONGO_URL_QUERY}" \
      ${MONGO_CUSTOM_FLAGS} ${drop_flag} -u ${MONGO_USER} -p ${MONGO_PASSWORD} \
      "${DUMP_ROOT_DIR}/${FROM_DB_NAME}"
<commit_msg>Added explicit db parameter<commit_after>#!/usr/bin/env bash


FROM_HOST="localhost:27017"
FROM_DB_NAME="meteor"
DUMP_ROOT_DIR="./.dump"

function print_restore_status {
  local drop_flag=$1
  local drop_enabled=$([[ ${drop_flag} == "" ]] && echo "NO" || echo "YES")
  local _db_restore_status_msg

  read -r -d '' _db_restore_status_msg << EOF
${COLOR_ERROR}${TEXT_BOLD}
######################################
# WARNING!!!                         #
# You may lost data at remote server #
######################################
${COLOR_DEFAULT}
Server: ${SERVER_DESCRIPTION}
URL: ${TEXT_UNDERLINE}${ROOT_URL}${COLOR_DEFAULT}
Mongo: ${MONGO_HOST}
Drop enabled: ${TEXT_BOLD}${COLOR_ERROR}${drop_enabled}${COLOR_DEFAULT}

Are you sure? (Enter 'yes' to continue):
EOF

  printf "${_db_restore_status_msg} "
}

function db-restore_help {
  cat << EOF
This script enables restoring of database from local project directory to remote server

boo db-restore server_name [--no-drop|-D] [-v|--verbose]

Options:
--no-drop|-D  - prevent all collections drop before dump restore
-v|--verbose  - verbose mode (print all logs)
EOF
}

function db-restore {
  local server_name=$1
  source_deploy_conf ${server_name}

  local app_local_db_path="$(pwd)/.meteor/local/db"
  local drop_flag='--drop'
  local output_stream=/dev/null

  # parse script arguments
  while [[ $(($#-1)) -gt 0 ]]; do
    local key="$2"

    case ${key} in
      --no-drop|-D)
      drop_flag=''
      ;;
      -v|--verbose)
      output_stream=/dev/stdout
      ;;
      *)
      echo_error "Unknown option: ${key}"
      exit 1
      ;;
    esac

    shift # past argument or value
  done

  # first get confirmation... just in case :)
  print_restore_status ${drop_flag}

  read CONFIRM

  if [[ ${CONFIRM} =~ ^yes$ ]]; then
    echo "Starting local database server... "
    mongod --dbpath "${app_local_db_path}" > ${output_stream} &

    local mongod_pid=$!
    sleep 20

    echo "Making local database dump..."
    mongodump -h "${FROM_HOST}" -d "${FROM_DB_NAME}" -o "${DUMP_ROOT_DIR}" &> ${output_stream}

    kill ${mongod_pid}

    echo "Restoring database from dump..."

    mongorestore "mongodb://${MONGO_HOST}/${MONGO_DB}${MONGO_URL_QUERY}" \
      ${MONGO_CUSTOM_FLAGS} ${drop_flag} --db ${MONGO_DB} -u ${MONGO_USER} -p ${MONGO_PASSWORD} \
      "${DUMP_ROOT_DIR}/${FROM_DB_NAME}"

{'fraction_non_alphanumeric': 0.17946524064171124, 'fraction_numerical': 0.005561497326203209, 'mean_word_length': 3.56640625, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4847024', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1565, 'n_words': 381}