#!/usr/bin/env bash set -euo pipefail [[ "${EUID}" -eq 0 ]] || { echo "Run as root (sudo)." exit 1 } REPO="$(cd "$(dirname "$0")/.." && pwd)" TOR_DIRS_FILE="${REPO}/scripts/generated/tor-dirs.txt" BACKUP_ROOT="${CYBERLUX_ONION_BACKUP_ROOT:-/var/backups/cyberlux-onion-keys}" CURRENT_DIR="${BACKUP_ROOT}/current" if [[ ! -f "${TOR_DIRS_FILE}" ]]; then echo "Missing ${TOR_DIRS_FILE}. Run the generator first." exit 1 fi if [[ ! -d "${CURRENT_DIR}" ]]; then echo "[*] No onion key backup found at ${CURRENT_DIR}; nothing to restore." exit 0 fi mkdir -p /var/lib/tor restored=0 timestamp="$(date +%s)" while IFS= read -r dir || [[ -n "${dir}" ]]; do [[ -z "${dir}" ]] && continue src="${CURRENT_DIR}/${dir}" dst="/var/lib/tor/${dir}" [[ -d "${src}" ]] || continue if [[ -d "${dst}" ]] && [[ -s "${dst}/hostname" ]]; then continue fi if [[ -e "${dst}" ]]; then mv "${dst}" "${dst}.stale.${timestamp}" fi cp -a "${src}" "${dst}" ((restored++)) || true done < "${TOR_DIRS_FILE}" if (( restored == 0 )); then echo "[*] Onion key restore check complete; nothing was missing." else echo "[*] Restored ${restored} onion service directories from backup." fi