diff options
Diffstat (limited to 'templates/standby-clone.sh.j2')
-rwxr-xr-x | templates/standby-clone.sh.j2 | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/templates/standby-clone.sh.j2 b/templates/standby-clone.sh.j2 new file mode 100755 index 0000000..3486342 --- /dev/null +++ b/templates/standby-clone.sh.j2 | |||
@@ -0,0 +1,44 @@ | |||
1 | #!/bin/bash | ||
2 | # {{ ansible_managed }} | ||
3 | |||
4 | BARMAN_DATABASE=$1 | ||
5 | BARMAN_BACKUP_VERSION=$2 | ||
6 | |||
7 | if [[ -z $BARMAN_DATABASE || -z $BARMAN_BACKUP_VERSION ]] | ||
8 | then | ||
9 | echo "Usage : $0 BARMAN_DATABASE BARMAN_BACKUP_VERSION" >&2 | ||
10 | echo " Example: $0 25kv 20161118T002405" >&2 | ||
11 | exit 1 | ||
12 | fi | ||
13 | |||
14 | BACKUP_DATE=`date +%s` | ||
15 | |||
16 | echo "accept key if necessary" | ||
17 | sudo -u postgres ssh barman@{{ postgres_barman_server }} echo "" | ||
18 | |||
19 | echo Stopping PostgreSQL | ||
20 | pg_ctlcluster {{ postgres_version }} {{ postgres_cluster_name }} stop | ||
21 | |||
22 | echo Cleaning up old cluster directory | ||
23 | sudo -u postgres mv /var/lib/postgresql/{{ postgres_version }}/{{ postgres_cluster_name }}{,_$BACKUP_DATE} | ||
24 | |||
25 | echo Creating new directory | ||
26 | sudo -u postgres mkdir -p /var/lib/postgresql/{{ postgres_version }}/{{ postgres_cluster_name }} | ||
27 | |||
28 | echo Get previous backup from backups server | ||
29 | sudo -u postgres \ | ||
30 | time rsync --progress -pvia --exclude='*.conf' --exclude='server.crt' --exclude='server.key' --delete \ | ||
31 | --password-file=/var/lib/postgresql/.rsync_pass \ | ||
32 | rsync://barman@{{ postgres_barman_server }}/backups/$BARMAN_DATABASE/base/$BARMAN_BACKUP_VERSION/data/ \ | ||
33 | /var/lib/postgresql/{{ postgres_version }}/{{ postgres_cluster_name }}/ | ||
34 | |||
35 | echo Restoring .conf and server certificate | ||
36 | 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 }}/ | ||
37 | |||
38 | echo Ensure rights are correcly set | ||
39 | chown -R postgres:postgres /var/lib/postgresql/{{ postgres_version }}/ | ||
40 | chmod 0700 /var/lib/postgresql/{{ postgres_version }}/ | ||
41 | chmod -R o-rwx /var/lib/postgresql/{{ postgres_version }}/ | ||
42 | |||
43 | echo Starting PostgreSQL | ||
44 | sudo pg_ctlcluster {{ postgres_version }} {{ postgres_cluster_name }} start | ||