#!/bin/bash

# This file is a simple wrapper around wal-e which records good recovery points in consul.
# Note that wal-e is configured for each cluster independently
#
# Usage:   backup-wale $cluster $version
# Example: backup-wale sitedb 9.3

cluster=${1}
version=${2}
basedir=/var/lib/postgresql/
datadir=${basedir}${version}/main

virtenv_dir=${basedir}/virt_env/wal_e

envdir /etc/wal-e.d/env ${virtenv_dir}/bin/wal-e --aws-instance-profile backup-push ${datadir}

# We set the 'latest' in consul into the future to allow enough time
# to restore to a consistent state when using `barman restore last`.
# The primary consumer of this is the staging sitedb mirrors.
latest=$(date --utc -d '+10 minutes' +'%F %T')
curl -X PUT -d "${latest} UTC" http://consul.internal.justin.tv/v1/kv/backups/db/${cluster}/latest?dc=us-west2
