Skip to content

Commit f33e00b

Browse files
committed
Add control over whether backups are rotated or not, rotate by date.
1 parent 4eefe4a commit f33e00b

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,13 @@ Role Variables
9696
### Backups ###
9797
9898
- `postgresql_backup_dir`: If set, enables [PITR][postgresql_pitr] backups. Set
99-
this to a directory where your database will be backed up.
99+
this to a directory where your database will be backed up. The most recent
100+
backup will be in a subdirectory named `current`.
101+
102+
- `postgresql_backup_rotate`: Boolean, defaults to `true`, which will cause the
103+
`current` directory to be renamed prior to creating a new backup. If set to
104+
`false`, `current` will be deleted (this is useful if you are using snapshots
105+
or some other means to archive previous backups).
100106
101107
- `postgresql_backup_local_dir`: Filesystem path on the PostgreSQL server where
102108
backup scripts will be placed and working WALs will be written prior to a WAL

defaults/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ postgresql_default_version: 9.4
44
postgresql_backup_local_dir: ~postgres/backup
55
postgresql_backup_active_dir: "{{ postgresql_backup_local_dir }}/active"
66
postgresql_backup_mail_recipient: postgres
7+
postgresql_backup_rotate: true

templates/archive_wal.sh.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ file=$2
1212
server=`hostname`
1313

1414
active_dir='{{ postgresql_backup_active_dir }}'
15-
backup_dir="{{ postgresql_backup_dir }}/$server/current/wal"
15+
backup_dir="{{ postgresql_backup_dir }}/current/wal"
1616
mutex="{{ postgresql_backup_local_dir }}/walmutex"
1717
mailto='{{ postgresql_backup_mail_recipient }}'
1818
mutex_attempts=50

templates/scheduled_backup.sh.j2

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55

66
server=`hostname`
77
data='{{ postgresql_pgdata }}'
8-
dir="{{ postgresql_backup_dir }}/$server/current"
9-
old="{{ postgresql_backup_dir }}/$server/previous"
10-
older="{{ postgresql_backup_dir }}/$server/older"
8+
dir='{{ postgresql_backup_dir }}/current'
119
mailto='{{ postgresql_backup_mail_recipient }}'
12-
mutex="{{ postgresql_backup_local_dir }}/nightlymutex"
10+
mutex='{{ postgresql_backup_local_dir }}/nightlymutex'
11+
rotate='{{ postgresql_backup_rotate | default("True") }}'
1312

1413
handler()
1514
{
@@ -64,19 +63,13 @@ start=`date +%s`
6463

6564
echo "nightly $$" > $mutex
6665

67-
# remove oldest backup
68-
if [ -d $older ]; then
69-
handler rm -rf $older
70-
fi
71-
72-
# move old backup to older backup
73-
if [ -d $old ]; then
74-
handler mv $old $older
75-
fi
76-
77-
# move current backup to old backup
66+
# move previous backup
7867
if [ -d $dir ]; then
79-
handler mv $dir $old
68+
if [ $rotate = "True" ]; then
69+
handler mv $dir {{ postgresql_backup_dir }}/`date -u +%Y%m%dT%H%M%SZ`
70+
else
71+
handler rm -rf $dir
72+
fi
8073
fi
8174

8275
# set up new directory

0 commit comments

Comments
 (0)