diff options
author | Paul B <paul.bonaud@capitainetrain.com> | 2018-08-31 11:49:09 +0200 |
---|---|---|
committer | Paul B <paul.bonaud@capitainetrain.com> | 2018-08-31 12:00:24 +0200 |
commit | d0bc90e08c29e881c388c6803ed9c49dff1f1776 (patch) | |
tree | e5f633a7461f3d339ef89758fc7bb3f1b91563d3 /templates/standby-clone.sh.j2 | |
download | ansible-postgresql-role-1.0.0.tar.gz ansible-postgresql-role-1.0.0.tar.zst ansible-postgresql-role-1.0.0.zip |
Initial commit open sourcing Postgresql Ansible role1.0.0
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 | ||