2024-10-20 10:31:40 +00:00
|
|
|
---
|
2024-10-20 10:37:39 +00:00
|
|
|
- name: Backup PostgreSQL and create cron
|
2024-10-20 10:31:40 +00:00
|
|
|
hosts: postgres_servers
|
|
|
|
become: true
|
|
|
|
|
|
|
|
vars:
|
2024-10-20 10:37:39 +00:00
|
|
|
backup_dir: "{{ backup_dir }}"
|
2024-10-20 10:31:40 +00:00
|
|
|
|
|
|
|
tasks:
|
2024-10-20 10:37:39 +00:00
|
|
|
- name: Create backup directory
|
2024-10-20 10:31:40 +00:00
|
|
|
file:
|
|
|
|
path: "{{ backup_dir }}"
|
|
|
|
state: directory
|
|
|
|
owner: postgres
|
|
|
|
group: postgres
|
|
|
|
mode: '0755'
|
|
|
|
|
2024-10-20 10:37:39 +00:00
|
|
|
- name: PostgreSQL database backup
|
2024-10-20 10:31:40 +00:00
|
|
|
become_user: postgres
|
|
|
|
command: >
|
|
|
|
pg_dump -U {{ postgres_user }}
|
|
|
|
-F c
|
|
|
|
-f "{{ backup_dir }}/db_backup_{{ postgres_db }}_{{ ansible_date_time.iso8601 }}.sql"
|
|
|
|
{{ postgres_db }}
|
|
|
|
environment:
|
|
|
|
PGPASSWORD: "{{ postgres_password }}"
|
|
|
|
|
2024-10-20 10:37:39 +00:00
|
|
|
- name: Create cron backup script
|
2024-10-20 10:31:40 +00:00
|
|
|
cron:
|
|
|
|
name: "PostgreSQL hourly backup"
|
|
|
|
user: postgres
|
|
|
|
minute: "0"
|
|
|
|
hour: "*"
|
|
|
|
job: 'pg_dump -U {{ postgres_user }} -F c {{ postgres_db }} > {{ backup_dir }}/db_backup_{{ postgres_db }}_$(date +\%F-\%H-\%M).sql'
|
|
|
|
environment:
|
|
|
|
PGPASSWORD: "{{ postgres_password }}"
|
|
|
|
|