#!/bin/sh
#/ Usage: ghe-restore-repositories-rsync <host>
#/ Restore an rsync snapshot of all Git repository data to a GitHub instance.
#/
#/ Note: This script typically isn't called directly. It's invoked by the
#/ ghe-restore command when the rsync strategy is used.
set -e

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

# Show usage and bail with no arguments
[ -z "$*" ] && print_usage

# Grab host arg
GHE_HOSTNAME="$1"

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

# The snapshot to restore should be set by the ghe-restore command but this lets
# us run this script directly.
: ${GHE_RESTORE_SNAPSHOT:=current}

# Transfer all git repository data from the latest snapshot to the GitHub
# instance in a single rsync invocation.
ghe-rsync -avH --delete \
    -e "ghe-ssh -p $(ssh_port_part "$GHE_HOSTNAME")" \
    --rsync-path="sudo -u git rsync" \
    "$GHE_DATA_DIR/$GHE_RESTORE_SNAPSHOT/repositories/" \
    "$(ssh_host_part "$GHE_HOSTNAME"):$GHE_REMOTE_DATA_USER_DIR/repositories" 1>&3
