37 lines
1.1 KiB
YAML
37 lines
1.1 KiB
YAML
|
---
|
||
|
- name: Backup PostgreSQL and set up cron job
|
||
|
hosts: postgres_servers
|
||
|
become: true
|
||
|
|
||
|
vars:
|
||
|
backup_dir: "/var/backups/postgresql"
|
||
|
|
||
|
tasks:
|
||
|
- name: Ensure backup directory exists
|
||
|
file:
|
||
|
path: "{{ backup_dir }}"
|
||
|
state: directory
|
||
|
owner: postgres
|
||
|
group: postgres
|
||
|
mode: '0755'
|
||
|
|
||
|
- name: Perform PostgreSQL database backup
|
||
|
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 }}"
|
||
|
|
||
|
- name: Create cron job for hourly PostgreSQL backup
|
||
|
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 }}"
|
||
|
|