#!/usr/bin/env bash

# Simple script to copy WAL archives from one server to another
# to be called as archive_command
# using RSYNC

# set as archive_command = "wal-archive %p %f ${destination}"
# where ${destination} is something like barman@host:/path/to/backups/incoming

# configure these parameters
###############################

# location of trigger file to disable archiving
NOARCHFILE="/var/lib/postgresql/NOARCHIVE"

# path to rsync, if required
RSYNC="rsync"

###############################
# you shouldn't need to configure any of the below

SOURCE="$1" # %p
FILE="$2" # %f
DEST="${3}/${2}"
export RSYNC_RSH="ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -c arcfour"

# See whether we want all archiving off
test -f ${NOARCHFILE} && exit 0

# Copy the file to the spool area on the replica, error out if
# the transfer fails
rsync --quiet --archive ${SOURCE} ${DEST}

if [ $? -ne 0 ]; then
        exit 1
fi
