Skip to content

Instantly share code, notes, and snippets.

@graphbear
Created January 27, 2016 16:10
Show Gist options
  • Save graphbear/a6edc5e4063736bc7334 to your computer and use it in GitHub Desktop.
Save graphbear/a6edc5e4063736bc7334 to your computer and use it in GitHub Desktop.
Postgres Physical Backup
#!/bin/bash
# a very basic phyisical backup script, this will also
# clean up archived WAL that is no longer needed
# define and create location for backups
BASE_DIR="/path_to/backups/physical"
ARCHIVED_WAL_DIR=/path_to/archived_xlogs
YMD=$(date "+%Y-%m-%d")
DIR="$BASE_DIR/$YMD"
mkdir -p $DIR
# remove any previous backup labels
rm $ARCHIVED_WAL_DIR/*.backup
# run the physical backup
pg_basebackup -Ft -z -D $DIR
# if the backup command exited successfully, then
if [ $? -eq 0 ] ; then
# get the new backup label that has een written to the archived
# WAL directory by the pg_basebackup process
BACKUP_LABEL=$(ls $ARCHIVED_WAL_DIR/*.backup)
BACKUP_LABEL=$(basename $BACKUP_LABEL)
# clean up archived WAL that is no longer needed
# this will fail if no backup label exists
pg_archivecleanup -d $ARCHIVED_WAL_DIR $BACKUP_LABEL
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment