#!/bin/sh -xe
-dir=test-2021-09-11
+basebackup=test-2021-09-11
+restore=/tmp/r
-mkdir /tmp/r
-chown postgres:postgres /tmp/r
-chmod 700 /tmp/r
+mkdir $restore
+chown postgres:postgres $restore
+chmod 700 $restore
-tar xvf /tmp/test-2021-09-11/base.tar.gz -C /tmp/r/
-tar xvf /tmp/test-2021-09-11/pg_wal.tar.gz -C /tmp/r/pg_wal/
+tar xvf /tmp/test-2021-09-11/base.tar.gz -C $restore/
+tar xvf /tmp/test-2021-09-11/pg_wal.tar.gz -C $restore/pg_wal/
-cp /var/lib/pgsql/10/recovery.conf /tmp/r/
-vi /tmp/r/recovery.conf
+cp /var/lib/pgsql/10/recovery.conf $restore/
+vi $restore/recovery.conf
+cd $restore
cat postgresql.conf | grep -v archive_mode | grep -v archive_command > postgresql.conf.new && mv postgresql.conf.new postgresql.conf
-su postgres -c '/usr/pgsql-10/bin/pg_ctl -D /tmp/r start -o "-p 5433"'
+su postgres -c '/usr/pgsql-10/bin/pg_ctl -D '$restore' start -o "-p 5433"'
-echo "STOP with: su postgres -c '/usr/pgsql-10/bin/pg_ctl -D /tmp/r stop'"
+cat > $restore.destroy << __SHELL__
+su postgres -c '/usr/pgsql-10/bin/pg_ctl -D $restore stop'
+rm -Rf $restore
+__SHELL__
+
+cd -
+psql -U postgres -p 5433
+
+su postgres -c '/usr/pgsql-10/bin/pg_ctl -D '$restore' stop'
+rm -Rf $restore