#!/usr/bin/env bash
set -Eeuo pipefail

if [[ ${EUID} -ne 0 ]]; then
    echo "ERROR: run as root" >&2
    exit 1
fi

if [[ $# -ne 1 || ! -d "$1" ]]; then
    echo "Usage: $0 /root/csfzx-security-backup-YYYYmmddHHMMSS" >&2
    exit 1
fi

BACKUP_DIR="$1"
NGINX_BIN="${NGINX_BIN:-/usr/sbin/nginx}"
if [[ ! -x "$NGINX_BIN" ]]; then
    NGINX_BIN="$(command -v nginx)"
fi

cp -a "$BACKUP_DIR/csfzx.yyangpt.cn.conf" \
    /usr/local/nginx/conf/vhost/csfzx.yyangpt.cn.conf
cp -a "$BACKUP_DIR/csfzx-lb-health.conf" \
    /usr/local/nginx/conf/vhost/csfzx-lb-health.conf
cp -a "$BACKUP_DIR/sshd_config" /etc/ssh/sshd_config

"$NGINX_BIN" -t
/usr/sbin/sshd -t
systemctl reload nginx
systemctl reload sshd

echo "Rollback completed from $BACKUP_DIR"
