From 77caafb019047a8e274e6d587e31d58854c7b79f Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Mon, 2 Mar 2020 21:44:29 +0000 Subject: Add scripts for generating database dumps --- scripts/guix-data-service-create-small-backup | 228 ++++++++++++++++++++++++++ 1 file changed, 228 insertions(+) create mode 100755 scripts/guix-data-service-create-small-backup (limited to 'scripts/guix-data-service-create-small-backup') diff --git a/scripts/guix-data-service-create-small-backup b/scripts/guix-data-service-create-small-backup new file mode 100755 index 0000000..c5c5678 --- /dev/null +++ b/scripts/guix-data-service-create-small-backup @@ -0,0 +1,228 @@ +#!/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 <