Repository Public
1#!/bin/zsh2# =============================================================================3# SP BOUCHER | INFRASTRUCTURE & AUTOMATION4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project : MacLustr Social Engine & Remote Navigator8# File : scripts/restore.sh9# Module : scripts10# Repository : https://git.spboucher.ai/maclustr-social11# Project Website : https://social.spboucher.ai12# Created : 2026-12-0113# Last Modified : 2026-12-0114#15# Author : Simon-Pierre Boucher16# Position : Professor of Finance17# Institution : Université du Québec en Outaouais (UQO)18# Organization : SP Boucher19# Website : https://spboucher.ai20# Git : https://git.spboucher.ai21#22# Preferred Contact: admin@spboucher.ai23# Technical Contact: dev@spboucher.ai24# Security/Admin : admin@spboucher.ai25#26# Copyright (c) 202627# Simon-Pierre Boucher28# All rights reserved where applicable. This file is governed by the29# applicable project license, copyright notices and relevant law.30# Existing third-party rights and notices remain unaffected.31#32# License : None specified (no license file in this repository)33#34# Do not embed credentials, private keys, access tokens,35# certificates or confidential personal information.36#37# =============================================================================38# END OF SP BOUCHER SOURCE HEADER39# =============================================================================4041# restore.sh <archive.tar.enc> <base_cible> [dossier_données_cible]42# Restores into a NEW database (refuses an existing one) so a restore test never touches production.43set -euo pipefail44cd "${0:A:h}/.."45source scripts/prod-env.sh46ARCHIVE="${1:?archive requise}"; TARGET="${2:?base cible requise}"; DATA="${3:-$HOME/maclustr-social-restore-$TARGET}"47KEY="$MLS_CONFIG_DIR/backup.key"48[ -f "$ARCHIVE.sha256" ] && (cd "${ARCHIVE:h}" && shasum -a 256 -c "${ARCHIVE:t}.sha256" >/dev/null) || { [ -f "$ARCHIVE.sha256" ] && { echo "empreinte invalide"; exit 1; }; }49psql -Atc "SELECT 1 FROM pg_database WHERE datname='$TARGET'" postgres | grep -q 1 && { echo "la base $TARGET existe déjà : refus"; exit 1; }50TMP=$(mktemp -d); trap 'rm -rf "$TMP"' EXIT51openssl enc -d -aes-256-cbc -pbkdf2 -iter 200000 -pass "file:$KEY" -in "$ARCHIVE" | tar -C "$TMP" -xf -52createdb "$TARGET"; pg_restore --no-owner -d "$TARGET" "$TMP/db.dump"53mkdir -p "$DATA"; chmod 700 "$DATA"; tar -C "$DATA" -xf "$TMP/files.tar"54echo "restauré : base $TARGET, fichiers $DATA"55psql -At "$TARGET" -c "SELECT 'audit_events=' || count(*) FROM audit_events UNION ALL SELECT 'content_items=' || count(*) FROM content_items UNION ALL SELECT 'media_assets=' || count(*) FROM media_assets UNION ALL SELECT 'publication_jobs=' || count(*) FROM publication_jobs"56DATABASE_URL="postgres:///$TARGET?host=/tmp" node dist/cli.mjs audit-verify57MISSING=0; for ref in $(psql -At "$TARGET" -c "SELECT storage_ref FROM media_assets WHERE status <> 'deleted'"); do [ -f "$DATA/media/$ref" ] || MISSING=$((MISSING+1)); done58echo "médias manquants sur disque : $MISSING"; [ "$MISSING" -eq 0 ]59