|
1 | 1 | #!/usr/bin/ansible-playbook |
2 | 2 | --- |
| 3 | +#--------------------------------------------------------------# |
| 4 | +# backup pg_cls if pgbackrest enabled [pg_backup] |
| 5 | +# - full backup when ansible_play_hosts_all contains primary |
| 6 | +# - full or incremental backup when ansible_play_hosts_all contains no primary |
| 7 | +#--------------------------------------------------------------# |
| 8 | +# tested scenarios: |
| 9 | +# - `./pgsql.yml -l <pg_cls> -t pg_hugepage` |
| 10 | +# - full backup |
| 11 | +# - `./pgsql.yml -l '<replica>,<primary>,&<pg_cls>' -t pg_hugepage` |
| 12 | +# - full backup |
| 13 | +# - `./pgsql.yml -l '<replica>,&<pg_cls>' -t pg_hugepage` |
| 14 | +# - full or incremental backup |
| 15 | +#--------------------------------------------------------------# |
3 | 16 |
|
4 | 17 | - name: set variable |
5 | 18 | tags: [ pg_hugepage, patroni, pg_launch, pt_restart ] |
6 | | - when: pg_role_runtime is undefined |
| 19 | + when: pg_role_runtime is undefined or pg_primary_host_runtime is undefined |
7 | 20 | block: |
8 | 21 | - name: run pg-role |
9 | 22 | command: /pg/bin/pg-role |
|
13 | 26 | set_fact: |
14 | 27 | pg_role_runtime: "{{ pg_role_cmd.stdout | default(pg_role) | trim }}" |
15 | 28 |
|
| 29 | + - name: set variable pg_role_runtime |
| 30 | + set_fact: |
| 31 | + pg_role_runtime: "{{ pg_role_cmd.stdout | default(pg_role) | trim }}" |
| 32 | + |
| 33 | + - name: set pg_primary_host_runtime |
| 34 | + tags: [ pg_hugepage, patroni, pg_launch, pt_restart ] |
| 35 | + import_tasks: util/patroni_primary_runtime.yml |
16 | 36 |
|
17 | 37 | - name: pg_backup |
18 | 38 | tags: [ pg_hugepage, patroni, pg_launch, pt_restart ] |
19 | 39 | when: pgbackrest_enabled|bool |
| 40 | + become_user: "{{ pg_dbsu }}" |
20 | 41 | vars: |
21 | | - dbsu: "{{ pg_dbsu|default('postgres') }}" |
22 | 42 | pg_role: "{{ pg_role_runtime }}" |
23 | 43 | block: |
24 | 44 | - name: full backup cls {{ pg_cluster }} |
25 | | - become_user: "{{ dbsu }}" |
26 | 45 | when: pg_role == 'primary' |
27 | 46 | command: /pg/bin/pg-backup full |
28 | 47 | register: back_ret_cmd |
29 | 48 | ignore_errors: false |
30 | 49 |
|
31 | 50 | - name: show backup result for {{ pg_cluster }} |
32 | | - when: pg_role == 'primary' |
| 51 | + when: pg_role == 'primary' and back_ret_cmd is defined |
33 | 52 | debug: |
34 | 53 | msg: | |
35 | 54 | STDOUT {{ back_ret_cmd.stdout_lines }}, |
36 | 55 | STDERR {{ back_ret_cmd.stderr_lines }} |
37 | 56 | changed_when: false |
38 | 57 |
|
| 58 | + - name: set variable pt_backup_at_primary from primary |
| 59 | + when: pg_role != 'primary' and pg_role_runtime is defined |
| 60 | + set_fact: |
| 61 | + pt_backup_at_primary: "{{ (hostvars[pg_primary_host_runtime].back_ret_cmd | default(false) | string | trim | bool) }}" |
| 62 | + changed_when: false |
| 63 | + |
| 64 | + - name: full or incr backup cls {{ pg_cluster }} if contains no primary at primary |
| 65 | + when: pg_role != 'primary' and (not pt_backup_at_primary) and pg_role_runtime is defined |
| 66 | + command: /pg/bin/pg-backup |
| 67 | + register: back_ret_cmd |
| 68 | + delegate_to: "{{ pg_primary_host_runtime }}" |
| 69 | + run_once: true |
| 70 | + |
| 71 | + |
39 | 72 | rescue: |
40 | 73 | - name: pg-backup failed for {{ pg_cluster }} |
41 | 74 | debug: |
|
0 commit comments