|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Based on cookiecutter-django template |
| 3 | +# https://github.com/pydanny/cookiecutter-django/blob/master/%7B%7Bcookiecutter.project_slug%7D%7D/compose/production/postgres/maintenance/_sourced/messages.sh |
| 4 | + |
| 5 | +set -o errexit |
| 6 | +set -o pipefail |
| 7 | +set -o nounset |
| 8 | + |
| 9 | + |
| 10 | +working_dir="$(dirname ${0})" |
| 11 | +source "${working_dir}/_sourced/messages.sh" |
| 12 | + |
| 13 | + |
| 14 | +if [[ -z ${1+x} ]]; then |
| 15 | + message_error "Backup filename is not specified yet it is a required parameter. Make sure you provide one and try again." |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | + |
| 19 | +backup_remote_full_path="gs://${GOOGLE_BUCKET_NAME}${DATABASE_BACKUP_PATH}${1}.tar.gz" |
| 20 | +echo $backup_remote_full_path |
| 21 | + |
| 22 | +if ! gsutil -q stat "${backup_remote_full_path}"; then |
| 23 | + message_error "No backup with the specified filename found. Check out the 'backups' scripts script output to see if there is one and try again." |
| 24 | + exit 1 |
| 25 | +fi |
| 26 | + |
| 27 | +message_welcome "Restoring the '${POSTGRES_DB}' database from the '${1}' backup..." |
| 28 | + |
| 29 | +if [[ "${POSTGRES_USER}" == "postgres" ]]; then |
| 30 | + message_error "Restoring as 'postgres' user is not supported. Assign 'POSTGRES_USER' env with another one and try again." |
| 31 | + exit 1 |
| 32 | +fi |
| 33 | + |
| 34 | +export PGHOST="${POSTGRES_HOST}" |
| 35 | +export PGPORT="${POSTGRES_PORT}" |
| 36 | +export PGUSER="${POSTGRES_USER}" |
| 37 | +export PGPASSWORD="${POSTGRES_PASSWORD}" |
| 38 | +export PGDATABASE="${POSTGRES_DB}" |
| 39 | + |
| 40 | + |
| 41 | +message_info "Downloading backup file..." |
| 42 | +gsutil cp "${backup_remote_full_path}" . |
| 43 | + |
| 44 | +message_info "Unzipping backup file..." |
| 45 | +tar x -zvf "${1}.tar.gz" |
| 46 | +rm -r "${1}.tar.gz" |
| 47 | + |
| 48 | +backup_directory="${1}" |
| 49 | +encrypted_backup_filename="${backup_directory}/backup_${1}.gz.enc" |
| 50 | +decrypted_backup_filename="${backup_directory}/backup_${1}.gz" |
| 51 | +encrypted_pass_filename="${backup_directory}/backup_${1}.gz.pass.enc" |
| 52 | +checksum_filename="${backup_directory}/backup_${1}.gz.sha256" |
| 53 | + |
| 54 | +message_info "Decrypting passphrase..." |
| 55 | +private_encryption_key_path="private_encryption_key.key" |
| 56 | +echo ${ENCRYPTION_PRIVATE_KEY} | base64 -d > "${private_encryption_key_path}" |
| 57 | +encription_passphrase="${ENCRYPTION_PASSPHRASE}" |
| 58 | +openssl rsautl -decrypt -inkey "${private_encryption_key_path}" -in "${encrypted_pass_filename}" -out "${encrypted_pass_filename}.dec" |
| 59 | + |
| 60 | +message_info "Decrypting backup file..." |
| 61 | +cat "${encrypted_pass_filename}.dec" | gpg --passphrase-fd 0 --batch --yes --output "${decrypted_backup_filename}" --decrypt "${encrypted_backup_filename}" |
| 62 | + |
| 63 | +message_info "Validating checksum..." |
| 64 | +if ! cat "${checksum_filename}" | sha256sum -c; then |
| 65 | + message_error "Backup SHA256 checksum invalid!" |
| 66 | + exit 1 |
| 67 | +fi |
| 68 | + |
| 69 | +message_info "Dropping the database..." |
| 70 | +dropdb "${PGDATABASE}" |
| 71 | + |
| 72 | +message_info "Creating a new database..." |
| 73 | +createdb --owner="${POSTGRES_USER}" "${PGDATABASE}" |
| 74 | + |
| 75 | +message_info "Applying the backup to the new database..." |
| 76 | +gunzip -c "${decrypted_backup_filename}" | psql "${POSTGRES_DB}" |
| 77 | + |
| 78 | +message_info "Cleaning up..." |
| 79 | +rm -f "${private_encryption_key_path}" |
| 80 | +rm -rf "${backup_directory}" |
| 81 | + |
| 82 | +message_success "The '${POSTGRES_DB}' database has been restored from the '${1}' backup." |
0 commit comments