Repository Public
1#!/bin/zsh2# =============================================================================3# SP BOUCHER | INFRASTRUCTURE & AUTOMATION4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# Project : MacLustr SMS Gateway8# File : scripts/backup.sh9# Module : scripts10# Repository : https://git.spboucher.ai/maclustr-sms11# Project Website : https://sms.spboucher.ai12# Created : 2026-11-3013# Last Modified : 2026-11-3014#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# Sauvegarde nocturne chiffrée, avec test de restauration.42# base → pg_dump, chiffré AES-256 (clé : ~/.config/maclustr-sms/backup.key)43# clé données → NON incluse (elle doit vivre ailleurs que la sauvegarde ; voir docs/DEPLOYMENT.md)44# Vérification : l'archive est déchiffrée et restaurée dans une base jetable, les comptes de lignes sont comparés.45# Copie hors nœud : si MLSMS_OFFSITE (ex. « M3U96b:backups/maclustr-sms ») est défini, l'archive chiffrée y est poussée.46set -euo pipefail47export PATH="/opt/homebrew/bin:/opt/homebrew/opt/postgresql@17/bin:/usr/bin:/bin:/usr/sbin:/sbin"48DB="${MLSMS_DB:-maclustr_sms}"49OUT="$HOME/backups/maclustr-sms"50KEY="$HOME/.config/maclustr-sms/backup.key"51STAMP=$(date -u +%Y%m%dT%H%M%SZ)52mkdir -p "$OUT"53F="$OUT/db-$STAMP.dump.enc"54pg_dump -Fc "$DB" | openssl enc -aes-256-cbc -pbkdf2 -iter 200000 -salt -pass "file:$KEY" -out "$F"5556SCRATCH="maclustr_sms_verify_$$"57createdb "$SCRATCH"58trap 'dropdb --if-exists "$SCRATCH"' EXIT59openssl enc -d -aes-256-cbc -pbkdf2 -iter 200000 -pass "file:$KEY" -in "$F" | pg_restore --no-owner -d "$SCRATCH"60for t in users devices device_credentials sim_profiles applications api_keys messages message_events webhooks audit_logs system_settings; do61 a=$(psql -tAc "select count(*) from $t" "$DB"); b=$(psql -tAc "select count(*) from $t" "$SCRATCH")62 # la base vivante a pu grossir pendant le dump : la copie ne doit ni la dépasser, ni être vide si l'originale ne l'est pas63 if [ "$b" -gt "$a" ] || { [ "$a" -gt 0 ] && [ "$b" -eq 0 ]; }; then echo "$(date -u +%FT%TZ) ÉCHEC DE VÉRIFICATION sur $t (vivante $a, restaurée $b)"; exit 1; fi64done65ls -1t "$OUT"/db-*.dump.enc | tail -n +31 | xargs -r rm -f66MSG="$(date -u +%FT%TZ) sauvegarde ok : $F ($(du -h "$F" | cut -f1)), restauration vérifiée"67if [ -n "${MLSMS_OFFSITE:-}" ]; then68 if rsync -a -e "ssh -o BatchMode=yes -o ConnectTimeout=15" "$F" "$MLSMS_OFFSITE/" 2>/dev/null; then MSG="$MSG, copie hors nœud ok ($MLSMS_OFFSITE)"; else MSG="$MSG, COPIE HORS NŒUD EN ÉCHEC ($MLSMS_OFFSITE)"; fi69fi70echo "$MSG"71zsh "$(dirname "$0")/rotate-logs.sh" || true72