Skip to content

Commit

Permalink
Merge pull request #143 from zalando/configurable-number-of-backups-t…
Browse files Browse the repository at this point in the history
…o-retain-by-wal-e

Make number of backups to retain configurable using environment
  • Loading branch information
a1exsh committed Apr 5, 2017
2 parents 539cc52 + d0b0f28 commit adca420
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
3 changes: 2 additions & 1 deletion postgres-appliance/configure_spilo.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ def get_placeholders(provider):
placeholders.setdefault('PGHOME', os.path.expanduser('~'))
placeholders.setdefault('APIPORT', '8008')
placeholders.setdefault('BACKUP_SCHEDULE', '00 01 * * *')
placeholders.setdefault('BACKUP_NUM_TO_RETAIN', 2)
placeholders.setdefault('CRONTAB', '[]')
placeholders.setdefault('PGROOT', os.path.join(placeholders['PGHOME'], 'pgroot'))
placeholders.setdefault('WALE_TMPDIR', os.path.abspath(os.path.join(placeholders['PGROOT'], '../tmp')))
Expand Down Expand Up @@ -381,7 +382,7 @@ def write_crontab(placeholders, path, overwrite):
return

lines = ['PATH={}'.format(path)]
lines += ['{BACKUP_SCHEDULE} /postgres_backup.sh "{WALE_ENV_DIR}" "{PGDATA}"'.format(**placeholders)]
lines += ['{BACKUP_SCHEDULE} /postgres_backup.sh "{WALE_ENV_DIR}" "{PGDATA}" "{BACKUP_NUM_TO_RETAIN}"'.format(**placeholders)]
lines += yaml.load(placeholders['CRONTAB'])
lines += [''] # EOF requires empty line for cron

Expand Down
2 changes: 1 addition & 1 deletion postgres-appliance/launch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ chown -R postgres:postgres "$PGLOG"

if python3 /configure_spilo.py all; then
(
sudo PATH="$PATH" -u postgres /patroni_wait.sh -t 3600 -- /postgres_backup.sh "$WALE_ENV_DIR" "$PGDATA"
sudo PATH="$PATH" -u postgres /patroni_wait.sh -t 3600 -- /postgres_backup.sh "$WALE_ENV_DIR" "$PGDATA" "$BACKUP_NUM_TO_RETAIN"
) &
fi

Expand Down
9 changes: 7 additions & 2 deletions postgres-appliance/postgres_backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ shift
PGDATA=$1
shift

NUM_TO_RETAIN=$1
shift

IN_RECOVERY=$(psql -tXqAc "select pg_is_in_recovery()")
[[ $IN_RECOVERY != "f" ]] && log "Cluster is in recovery, not running backup" && exit 0

# leave only 2 base backups before creating a new one
# leave at least 2 base backups before creating a new one
[[ "$NUM_TO_RETAIN" -lt 2 ]] && NUM_TO_RETAIN=2

# --aws-instance-profile flag is just ignored when running in GCE.
envdir "${WALE_ENV_DIR}" wal-e --aws-instance-profile delete --confirm retain 2
envdir "${WALE_ENV_DIR}" wal-e --aws-instance-profile delete --confirm retain "${NUM_TO_RETAIN}"

# Ensure we don't have more workes than CPU's
POOL_SIZE=4
Expand Down

0 comments on commit adca420

Please sign in to comment.