aboutsummaryrefslogtreecommitdiffhomepage
path: root/templates/standby-barman-clone.sh.j2
blob: 2db44b6e5c7377b75a5f7bb79e555bae5d4d73e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/bash
# {{ ansible_managed }}

set -eo pipefail

BARMAN_DATABASE=$1
BARMAN_BACKUP_VERSION=$2

if [[ -z $BARMAN_DATABASE || -z $BARMAN_BACKUP_VERSION ]]
then
  echo "Usage : $0 BARMAN_DATABASE BARMAN_BACKUP_VERSION" >&2
  echo "  Example: $0 25kv 20161118T002405" >&2
  exit 1
fi

BACKUP_DATE=`date +%s`

echo "accept key if necessary"
sudo -u postgres ssh barman@{{ postgres_barman_server }} echo ""

echo Stopping PostgreSQL
pg_ctlcluster {{ postgres_version }} {{ postgres_cluster_name }} stop || true

echo Cleaning up old cluster directory
sudo -u postgres mv /var/lib/postgresql/{{ postgres_version }}/{{ postgres_cluster_name }}{,_$BACKUP_DATE}

echo Creating new directory
sudo -u postgres mkdir -p /var/lib/postgresql/{{ postgres_version }}/{{ postgres_cluster_name }}

echo Get previous backup from backups server
sudo -u postgres \
  time rsync --progress -pvia --exclude='*.conf' --exclude='server.crt' --exclude='server.key' --delete {{ rsync_options }} \
  {{ barman_remote_url }}/$BARMAN_DATABASE/base/$BARMAN_BACKUP_VERSION/data/ \
  /var/lib/postgresql/{{ postgres_version }}/{{ postgres_cluster_name }}/

echo Restoring .conf and server certificate
sudo -u postgres cp -a /var/lib/postgresql/{{ postgres_version }}/{{ postgres_cluster_name }}_$BACKUP_DATE/{*.conf,server.crt,server.key} /var/lib/postgresql/{{ postgres_version }}/{{ postgres_cluster_name }}/

echo Ensure rights are correcly set
chown -R postgres:postgres /var/lib/postgresql/{{ postgres_version }}/
chmod 0700 /var/lib/postgresql/{{ postgres_version }}/
chmod -R o-rwx /var/lib/postgresql/{{ postgres_version }}/

echo Starting PostgreSQL
sudo pg_ctlcluster {{ postgres_version }} {{ postgres_cluster_name }} start