aboutsummaryrefslogtreecommitdiffhomepage
path: root/templates/standby-clone.sh.j2
blob: 16a693029b1ea79135a01572c2299d7812ace345 (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
46
47
48
#!/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 \
{% if postgres_barman_rsync_enabled|default(false) -%}
  --password-file=/var/lib/postgresql/.rsync_pass \
{%- endif %}
  {% if postgres_barman_rsync_enabled|default(false) -%}rsync://{%- endif -%}barman@{{ postgres_barman_server }}{%- if postgres_barman_rsync_enabled|default(false) -%}/backups{%- else -%}:~{%- endif -%}/$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