#!/usr/bin/env bash ## # Fetch database dump. # # This is a router script to call relevant scripts based on type. # # IMPORTANT! This script runs outside the container on the host system. # # shellcheck disable=SC1090,SC1091 t=$(mktemp) && export -p >"${t}" && set -a && . ./.env && if [ -f ./.env.local ]; then . ./.env.local; fi && set +a && . "${t}" && rm "${t}" && unset t set -eu [ "${VORTEX_DEBUG-}" = "1" ] && set -x # Database index suffix. When set (e.g., "2"), all DB-related variable lookups # use the indexed variant (e.g., VORTEX_FETCH_DB2_SOURCE instead of # VORTEX_FETCH_DB_SOURCE). _db_index="${VORTEX_DB_INDEX:-}" # The "container_registry" source works only for database-in-image storage # (when the $VORTEX_DB_IMAGE variable has a value). _v="VORTEX_FETCH_DB${_db_index}_SOURCE" VORTEX_FETCH_DB_SOURCE="${!_v:-url}" # Force DB fetch even if the cache exists. _v="VORTEX_FETCH_DB${_db_index}_FORCE" VORTEX_FETCH_DB_FORCE="${!_v:-}" # Proceed with fetch. _v="VORTEX_FETCH_DB${_db_index}_PROCEED" VORTEX_FETCH_DB_PROCEED="${!_v:-1}" # Database dump file name. _v="VORTEX_FETCH_DB${_db_index}_FILE" _vs="VORTEX_DB${_db_index}_FILE" VORTEX_FETCH_DB_FILE="${!_v:-${!_vs:-${VORTEX_DB_FILE:-db.sql}}}" # Directory with database dump file. _v="VORTEX_FETCH_DB${_db_index}_DIR" _vs="VORTEX_DB${_db_index}_DIR" VORTEX_FETCH_DB_DIR="${!_v:-${!_vs:-${VORTEX_DB_DIR:-./.data}}}" # ------------------------------------------------------------------------------ # @formatter:off info() { [ "${TERM:-}" != "dumb" ] && tput colors >/dev/null 2>&1 && printf "\033[36m[INFO] %s\033[0m\n" "${1}" || printf "[INFO] %s\n" "${1}"; } note() { printf " %s\n" "${1}"; } task() { _TASK_START=$(date +%s); [ "${TERM:-}" != "dumb" ] && tput colors >/dev/null 2>&1 && printf "\033[34m[TASK] %s\033[0m\n" "${1}" || printf "[TASK] %s\n" "${1}"; } pass() { _d=""; [ -n "${_TASK_START:-}" ] && _d=" ($(($(date +%s) - _TASK_START))s)" && unset _TASK_START; [ "${TERM:-}" != "dumb" ] && tput colors >/dev/null 2>&1 && printf "\033[32m[ OK ] %s%s\033[0m\n" "${1}" "${_d}" || printf "[ OK ] %s%s\n" "${1}" "${_d}"; } fail() { [ "${TERM:-}" != "dumb" ] && tput colors >/dev/null 2>&1 && printf "\033[31m[FAIL] %s\033[0m\n" "${1}" || printf "[FAIL] %s\n" "${1}"; exit "${2:-1}"; } # @formatter:on info "Started database${_db_index:+ ${_db_index}} fetch." [ "${VORTEX_FETCH_DB_PROCEED}" != "1" ] && pass "Skipped database fetch as VORTEX_FETCH_DB_PROCEED is not set to 1." && exit 0 # A dump store ('gdrive' or 's3') receives the dump that the nightly refresh # pushes, so with a push requested the dump comes from its upstream source. # The store may hold no dump yet on the first push. if [ -z "${_db_index}" ] && [ "${VORTEX_PUSH_DB_PROCEED:-}" = "1" ]; then case "${VORTEX_FETCH_DB_SOURCE}" in gdrive | s3) case "${VORTEX_REFRESH_DB_SOURCE:-}" in "" | gdrive | s3 | container_registry | none) fail "The database dump file pushed to ${VORTEX_FETCH_DB_SOURCE} is fetched from an upstream source, but VORTEX_REFRESH_DB_SOURCE is \"${VORTEX_REFRESH_DB_SOURCE:-}\". Set it to the source of the dump file: url, ftp, acquia or lagoon." ;; esac note "Fetching from the ${VORTEX_REFRESH_DB_SOURCE} source in VORTEX_REFRESH_DB_SOURCE as VORTEX_PUSH_DB_PROCEED is set to 1." VORTEX_FETCH_DB_SOURCE="${VORTEX_REFRESH_DB_SOURCE}" ;; esac fi # The fetch script is resolved before the existing dump check, so an # unsupported source fails even when a cached dump would be reused. case "${VORTEX_FETCH_DB_SOURCE}" in url) fetch_script="vortex-fetch-db-url" ;; ftp) fetch_script="vortex-fetch-db-ftp" ;; acquia) fetch_script="vortex-fetch-db-acquia" ;; lagoon) fetch_script="vortex-fetch-db-lagoon" ;; container_registry) fetch_script="vortex-fetch-db-container-registry" ;; s3) fetch_script="vortex-fetch-db-s3" ;; gdrive) fetch_script="vortex-fetch-db-gdrive" ;; none) fetch_script="" ;; *) fail "Unsupported database source \"${VORTEX_FETCH_DB_SOURCE}\" in VORTEX_FETCH_DB${_db_index}_SOURCE. Use one of: url, ftp, acquia, lagoon, container_registry, s3, gdrive, none." ;; esac # Skip file existence check for container_registry source as the database is # stored as a Docker image, not a file. if [ "${VORTEX_FETCH_DB_SOURCE}" != "container_registry" ]; then db_file_basename="${VORTEX_FETCH_DB_FILE%.*}" [ -d "${VORTEX_FETCH_DB_DIR:-}" ] && found_db=$(find "${VORTEX_FETCH_DB_DIR}" -name "${db_file_basename}.sql" -o -name "${db_file_basename}.tar") if [ -n "${found_db:-}" ]; then note "Found existing database dump file(s)." ls -Alh "${VORTEX_FETCH_DB_DIR}" 2>/dev/null || true if [ "${VORTEX_FETCH_DB_FORCE}" != "1" ]; then note "Using existing database dump file(s)." note "Fetch will not proceed." note "Remove existing database file or set VORTEX_FETCH_DB${_db_index}_FORCE value to 1 to force fetch." exit 0 else note "Will fetch a fresh copy of the database." fi fi fi if [ -n "${fetch_script}" ]; then "$(dirname "${BASH_SOURCE[0]}")/${fetch_script}" else note "Skipped fetching as VORTEX_FETCH_DB${_db_index}_SOURCE is set to \"none\"." fi ls -Alh "${VORTEX_FETCH_DB_DIR}" || true # The semaphore file marks a completed database fetch. [ -n "${VORTEX_FETCH_DB_SEMAPHORE:-}" ] && touch "${VORTEX_FETCH_DB_SEMAPHORE}" pass "Finished database${_db_index:+ ${_db_index}} fetch."