diff --git a/patroni/scripts/restore_example.sh b/patroni/scripts/restore_example.sh new file mode 100755 index 000000000..8e88098a2 --- /dev/null +++ b/patroni/scripts/restore_example.sh @@ -0,0 +1,71 @@ +#!/bin/bash + +function usage() +{ + cat <<__EOT__ +Usage: $0 --scope=SCOPE --datadir=DATADIR --backup_directory=BACKUPDIR +__EOT__ +} + +optspec=":-:" +while getopts "$optspec" optchar; do + case "${optchar}" in + -) + case "${OPTARG}" in + help) + usage + exit 0 + ;; + scope) + PATRONI_SCOPE="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 )) + ;; + scope=*) + PATRONI_SCOPE="${OPTARG#*=}" + ;; + datadir) + PGDATA="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 )) + ;; + datadir=*) + PGDATA="${OPTARG#*=}" + ;; + backup_directory) + BACKUPDIR="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 )) + ;; + backup_directory=*) + BACKUPDIR="${OPTARG#*=}" + ;; + *) + true + ;; + esac;; + *) + if [ "$OPTERR" != 1 ] || [ "${optspec:0:1}" = ":" ]; then + echo "Non-option argument: '-${OPTARG}'" >&2 + usage + exit 1 + fi + ;; + esac +done + + +function die() +{ + echo "$1" + exit 1 +} + +[[ -z ${PATRONI_SCOPE} ]] && usage && exit 1 +[[ -z ${PGDATA} ]] && usage && exit 1 +[[ -z ${BACKUPDIR} ]] && usage && exit 1 + +[[ -d "${PGDATA}" ]] && die "Will not overwrite existing PGDATA directory, stopping example restore" + +LATESTBACKUP="$(ls -tr "${BACKUPDIR}/${PATRONI_SCOPE}/basebackup_"*.tar.gz | tail -n 1)" +[[ ! -f "${LATESTBACKUP}" ]] && die "Can not find the backup, stopping example restore" + +mkdir -p "${PGDATA}" +tar xzfp "${LATESTBACKUP}" -C "${PGDATA}" +chmod 0700 "${PGDATA}" + +echo "Restored ${LATESTBACKUP} into PGDATA=${PGDATA}" diff --git a/postgres2.yml b/postgres2.yml index 99e8b47e2..ca404fa52 100644 --- a/postgres2.yml +++ b/postgres2.yml @@ -72,6 +72,7 @@ postgresql: create_replica_method: - basebackup # - wal_e +# - restore_example # commented-out example for wal-e provisioning #wal_e: #command: /patroni/scripts/wale_restore.py @@ -80,6 +81,11 @@ postgresql: #threshold_backup_size_percentage: 30 #retries: 2 #use_iam: 1 +# commented-out example for restore from file + #restore_example: + #command: patroni/scripts/restore_example.sh + #no_master: true + #backup_directory: /tmp/backup/ #recovery_conf: #restore_command: envdir /etc/wal-e.d/env wal-e wal-fetch "%f" "%p" -p 1 recovery_conf: