#!/bin/sh set -eux TMP_DATABASE_NAME=guix_data_service_small DEFAULT_BACKUP_DIRECTORY="/var/lib/guix-data-service/dumps" BACKUP_DIRECTORY="${GUIX_DATA_SERVICE_BACKUP_DIRECTORY:-$DEFAULT_BACKUP_DIRECTORY}" if [ -z "${1:-}" ]; then DATE="$(date "+%Y-%m-%d")" else DATE="$1" fi FULL_BACKUP_NAME="guix_data_service_full.dump" SMALL_BACKUP_NAME="guix_data_service_small.dump" renice 19 -p $$ ionice -p $$ -c 3 TMP_DATABASE=$(mktemp -d -t guix-data-service-database-XXXX) URI=$(pg_tmp -w 0 -d "$TMP_DATABASE") function cleanup { pg_tmp stop -w 1 -d "$TMP_DATABASE" } trap cleanup EXIT psql --no-psqlrc "$URI" --command="CREATE USER guix_data_service" psql --no-psqlrc "$URI" --command="CREATE DATABASE $TMP_DATABASE_NAME WITH OWNER guix_data_service" URI_FOR_DATABASE="${URI/test/$TMP_DATABASE_NAME}" pg_restore --username=guix_data_service --jobs=8 --dbname="$URI_FOR_DATABASE" \ "$BACKUP_DIRECTORY/$DATE/$FULL_BACKUP_NAME" psql -v ON_ERROR_STOP=1 --echo-queries --no-psqlrc "$URI_FOR_DATABASE" -U guix_data_service <