|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -e -o pipefail -u |
| 4 | + |
| 5 | +DB_DUMP_DIR=/media/dbdump |
| 6 | +SEARCH_DUMP_DIR=/var/cache/musicbrainz/solr-backups |
| 7 | +BASE_DOWNLOAD_URL="${MUSICBRAINZ_BASE_DOWNLOAD_URL:-https://data.metabrainz.org/pub/musicbrainz}" |
| 8 | +WGET_CMD=(wget) |
| 9 | + |
| 10 | +SCRIPT_NAME=$(basename "$0") |
| 11 | +HELP=$(cat <<EOH |
| 12 | +Usage: $SCRIPT_NAME [<options>] |
| 13 | +
|
| 14 | +Fetch backup archives of the MusicBrainz SolrCloud collections. |
| 15 | +
|
| 16 | +Options: |
| 17 | + --base-download-url <url> Specify URL of a MetaBrainz/MusicBrainz download server. |
| 18 | + (Default: '$BASE_DOWNLOAD_URL') |
| 19 | + --wget-options <wget options> Specify additional options to be passed to wget, |
| 20 | + these should be separated with whitespace, |
| 21 | + the list should be a single argument |
| 22 | + (escape whitespaces if needed). |
| 23 | + -h, --help Print this help message and exit. |
| 24 | +
|
| 25 | +Environment: |
| 26 | + MUSICBRAINZ_BASE_DOWNLOAD_URL Default URL for MetaBrainz/MusicBrainz download server. |
| 27 | +EOH |
| 28 | +) |
| 29 | + |
| 30 | +# Parse arguments |
| 31 | + |
| 32 | +while [[ $# -gt 0 ]] |
| 33 | +do |
| 34 | + case "$1" in |
| 35 | + --base-download-url ) |
| 36 | + shift |
| 37 | + BASE_DOWNLOAD_URL="${1%/data/fullexport/}" |
| 38 | + if ! [[ $BASE_DOWNLOAD_URL =~ ^(ftp|https?):// ]] |
| 39 | + then |
| 40 | + echo >&2 "$SCRIPT_NAME: --base-download-url must begin with ftp://, http:// or https://" |
| 41 | + exit 64 # EX_USAGE |
| 42 | + fi |
| 43 | + ;; |
| 44 | + --wget-options ) |
| 45 | + shift |
| 46 | + IFS=' ' read -r -a WGET_OPTIONS <<< "$1" |
| 47 | + WGET_CMD+=("${WGET_OPTIONS[@]}") |
| 48 | + unset WGET_OPTIONS |
| 49 | + ;; |
| 50 | + -h | --help ) |
| 51 | + echo "$HELP" |
| 52 | + exit 0 # EX_OK |
| 53 | + ;; |
| 54 | + -* ) |
| 55 | + echo >&2 "$SCRIPT_NAME: unrecognized option '$1'" |
| 56 | + echo >&2 "Try '$SCRIPT_NAME --help' for usage." |
| 57 | + exit 64 # EX_USAGE |
| 58 | + ;; |
| 59 | + * ) |
| 60 | + echo >&2 "$SCRIPT_NAME: unrecognized argument '$1'" |
| 61 | + echo >&2 "Try '$SCRIPT_NAME --help' for usage." |
| 62 | + exit 64 # EX_USAGE |
| 63 | + ;; |
| 64 | + esac |
| 65 | + shift |
| 66 | +done |
| 67 | + |
| 68 | +# Show information about signing up for data use |
| 69 | + |
| 70 | +if [[ ! -a "$DB_DUMP_DIR/.for-commercial-use" && |
| 71 | + ! -a "$DB_DUMP_DIR/.for-non-commercial-use" && |
| 72 | + ! -a "$SEARCH_DUMP_DIR/.for-commercial-use" && |
| 73 | + ! -a "$SEARCH_DUMP_DIR/.for-non-commercial-use" ]] |
| 74 | +then |
| 75 | + prompt=$(cat <<-EOQ |
| 76 | + The data you are about to download is provided by the MetaBrainz Foundation. |
| 77 | + Are you planning to use this data for commercial or business purposes? |
| 78 | + (y/n) |
| 79 | + EOQ |
| 80 | + ) |
| 81 | + read -e -p "$prompt " -r |
| 82 | + while [[ ! ${REPLY:0:1} =~ [YNyn] ]] |
| 83 | + do |
| 84 | + read -e -p "Invalid reply. Yes or no? " -r |
| 85 | + done |
| 86 | + echo |
| 87 | + if [[ ${REPLY:0:1} =~ [Yy] ]] |
| 88 | + then |
| 89 | + prompt=$(cat <<-EOQ |
| 90 | + The MetaBrainz Foundation is supported by commercial users of our data and |
| 91 | + through end-user donations. If you are using our data in a commercial context, |
| 92 | + we require you to support MetaBrainz financially in order for us ensure the |
| 93 | + availability of these datasets in the future. |
| 94 | + |
| 95 | + Please sign up at https://metabrainz.org/supporters/account-type |
| 96 | + |
| 97 | + [Press any key when OK] |
| 98 | + EOQ |
| 99 | + ) |
| 100 | + read -e -N 1 -p "$prompt" -r -s |
| 101 | + echo OK |
| 102 | + touch "$SEARCH_DUMP_DIR/.for-commercial-use" |
| 103 | + else |
| 104 | + prompt=$(cat <<-EOQ |
| 105 | + Could you please sign up at https://metabrainz.org/supporters/account-type |
| 106 | + (for free!) so that we may better understand how our data is being used? |
| 107 | + |
| 108 | + We also encourage our non-commercial users who can afford it to make a donation |
| 109 | + to the MetaBrainz Foundation so that we may continue our mission: |
| 110 | + https://metabrainz.org/donate |
| 111 | + |
| 112 | + [Press any key when OK] |
| 113 | + EOQ |
| 114 | + ) |
| 115 | + read -e -N 1 -p "$prompt" -r -s |
| 116 | + echo OK |
| 117 | + touch "$SEARCH_DUMP_DIR/.for-non-commercial-use" |
| 118 | + fi |
| 119 | +fi |
| 120 | + |
| 121 | +# Check timestamp for previously downloaded archives |
| 122 | + |
| 123 | +echo "$(date): 1/3: Checking timestamps of available archives..." |
| 124 | + |
| 125 | +PREVIOUS_DUMP_TIMESTAMP='' |
| 126 | +if [[ -a "$SEARCH_DUMP_DIR/LATEST" ]] |
| 127 | +then |
| 128 | + PREVIOUS_DUMP_TIMESTAMP=$(<"$SEARCH_DUMP_DIR/LATEST") |
| 129 | + rm -f "$SEARCH_DUMP_DIR/LATEST" |
| 130 | +fi |
| 131 | + |
| 132 | +# Check timestamp for remotely available archives |
| 133 | + |
| 134 | +"${WGET_CMD[@]}" -nd -nH -P "$SEARCH_DUMP_DIR" \ |
| 135 | + "${BASE_DOWNLOAD_URL}/data/solr-backups/LATEST" |
| 136 | +DUMP_TIMESTAMP=$(<"$SEARCH_DUMP_DIR/LATEST") |
| 137 | + |
| 138 | +# Remove previously downloaded files if new archives are available |
| 139 | + |
| 140 | +if [[ $PREVIOUS_DUMP_TIMESTAMP != "$DUMP_TIMESTAMP" ]] |
| 141 | +then |
| 142 | + find "$SEARCH_DUMP_DIR" \ |
| 143 | + ! -path "$SEARCH_DUMP_DIR" \ |
| 144 | + ! -path "$SEARCH_DUMP_DIR/.for-commercial-use" \ |
| 145 | + ! -path "$SEARCH_DUMP_DIR/.for-non-commercial-use" \ |
| 146 | + ! -path "$SEARCH_DUMP_DIR/LATEST" \ |
| 147 | + -delete |
| 148 | +fi |
| 149 | + |
| 150 | +# Actually fetch latest MusicBrainz Solr backup archives |
| 151 | + |
| 152 | +echo "$(date): 2/3: Fetching MusicBrainz Solr backup archives..." |
| 153 | +"${WGET_CMD[@]}" -nd -nH -c -r -P "$SEARCH_DUMP_DIR" \ |
| 154 | + --accept 'MD5SUMS,*.tar.zst' --no-parent --relative \ |
| 155 | + "${BASE_DOWNLOAD_URL}/data/solr-backups/$DUMP_TIMESTAMP/" |
| 156 | + |
| 157 | +# Check archives integrity |
| 158 | + |
| 159 | +echo "$(date): 3/3: Checking MD5 sums..." |
| 160 | +cd "$SEARCH_DUMP_DIR" && md5sum -c MD5SUMS && cd - >/dev/null |
| 161 | + |
| 162 | +echo "$(date): Done fetching MusicBrainz Solr backup archives." |
| 163 | +# vi: set noexpandtab softtabstop=0: |
0 commit comments