#!/bin/sh
#/ Usage: ghe-backup-redis
#/ Take a snapshot of all Redis data. This is needed because older versions of
#/ the remote side ghe-export-redis command use a blocking SAVE instead of a
#/ non-blocking BGSAVE.
#/
#/ Note: This script typically isn't called directly. It's invoked by the
#/ ghe-backup command.
set -e

# Bring in the backup configuration
cd $(dirname "$0")/../..
. share/github-backup-utils/ghe-backup-config

# Perform a host-check and establish GHE_REMOTE_XXX variables.
ghe_remote_version_required "$GHE_HOSTNAME"

# Force a redis BGSAVE, and wait for it to complete.
sudo=
[ "$GHE_VERSION_MAJOR" -ge 2 ] && sudo="sudo"
ghe-ssh "$GHE_HOSTNAME" /bin/sh <<EOF
    set -e
    timestamp=\$(redis-cli LASTSAVE)
    redis-cli BGSAVE 1>/dev/null
    while [ \$(redis-cli LASTSAVE) -eq \$timestamp ]; do
        sleep 1
    done
    $sudo cat '$GHE_REMOTE_DATA_USER_DIR/redis/dump.rdb'
EOF
