#!/bin/sh

# archive_command script for Standalone Hot Backup
#
# Assumes that your backup_in_progress trigger file
# is located at a subdirectory below the database
# directory, along with an archive/ subdirectory
# to hold WAL files.

FULLPATH=$1
FILE=$2
ARCHIVE="../archive"

if [ ! -f ../backup_in_progress ]; then
  exit 0
fi

if [ -f ${ARCHIVE}/${FILE} ] ; then
  echo Archive file ${FILE} already exists in archive, skipping >&2
  exit 0
fi

if [ ! -d ${ARCHIVE} ] ; then
  echo Archive directory does not exist >&2
  exit 1
fi

cp ${FULLPATH} ${ARCHIVE}/${FILE}
if [ $? -ne 0 ] ; then
  echo $0 Archive copy of ${FILE} failed with error $? >&2
  exit 1
fi

exit 0
