anchorage_layer_simple/data/restart-services-cron.sh
ncantu 937646cc45 Daily backup to git cron, backup/restore scripts, docs
**Motivations:**
- Export Signet and mining wallet backups to git with only 2 versions kept
- Document and add backup/restore scripts for signet and mining wallet

**Correctifs:**
- Backup-to-git uses SSH URL for passwordless cron; copy timestamped files only; prune to 2 versions; remove *-latest from backup repo

**Evolutions:**
- data/backup-to-git-cron.sh: daily export to git.4nkweb.com/4nk/backup
- save-signet-datadir-backup.sh, restore-signet-from-backup.sh, export-mining-wallet.sh, import-mining-wallet.sh
- features/backup-to-git-daily-cron.md, docs/MAINTENANCE.md backup section
- .gitignore: data/backup-to-git.log

**Pages affectées:**
- .gitignore, data/backup-to-git-cron.sh, docs/MAINTENANCE.md, features/backup-to-git-daily-cron.md
- save-signet-datadir-backup.sh, restore-signet-from-backup.sh, export-mining-wallet.sh, import-mining-wallet.sh
- Plus autres fichiers modifiés ou non suivis déjà présents dans le working tree
2026-02-04 03:07:57 +01:00

130 lines
4.0 KiB
Bash
Executable File

#!/bin/bash
# Daily restart of project systemd services (local only).
# Run via cron. Requires passwordless sudo for systemctl restart.
# Log: data/restart-services.log
#
# Local only: no SSH, no remote commands. Manages systemd units and
# local Docker (mempool stack, bitcoind container) on the current machine.
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
LOG_FILE="$SCRIPT_DIR/restart-services.log"
MEMPOOL_COMPOSE="${PROJECT_DIR}/mempool/docker-compose.signet.yml"
BITCOIND_CONTAINER="bitcoin-signet-instance"
SERVICES=(
bitcoin-signet
bitcoin
anchorage-api
api-relay
clamav-api
faucet-api
filigrane-api
signet-dashboard
userwallet
website-skeleton
)
log() { echo "$(date -Iseconds) $*" | tee -a "$LOG_FILE"; }
cd "$PROJECT_DIR" || exit 1
log "=== Restart services cron (local only, no SSH) ==="
# 1. Verify enabled (warn if not)
for u in "${SERVICES[@]}"; do
if ! systemctl is-enabled "$u" &>/dev/null; then
log "WARN: $u is not enabled (will not start at boot)"
fi
done
# 2. Restart active units; track attempted
restarted=()
for u in "${SERVICES[@]}"; do
if ! systemctl is-active --quiet "$u" 2>/dev/null; then
log "SKIP: $u not active (not installed or not running on this machine)"
continue
fi
log "Restarting $u..."
if sudo systemctl restart "$u"; then
log " $u OK"
restarted+=("$u")
else
log " $u FAILED"
restarted+=("$u")
fi
done
# 3. Verify active for restarted units
for u in "${restarted[@]}"; do
if ! systemctl is-active --quiet "$u" 2>/dev/null; then
log "WARN: $u not active after restart"
fi
done
# 4. Docker: bitcoind container (local only) - START FIRST
# Bitcoind must be running before mempool can connect to it
if docker ps -a -q -f "name=^${BITCOIND_CONTAINER}$" 2>/dev/null | grep -q .; then
log "Starting/restarting $BITCOIND_CONTAINER..."
bitcoind_ok=0
if docker ps -q -f "name=^${BITCOIND_CONTAINER}$" 2>/dev/null | grep -q .; then
# Container is running, just restart it
if docker restart "$BITCOIND_CONTAINER" &>/dev/null; then
bitcoind_ok=1
fi
else
# Container exists but is stopped, start it
if docker start "$BITCOIND_CONTAINER" &>/dev/null; then
bitcoind_ok=1
fi
fi
if [ "$bitcoind_ok" -eq 1 ]; then
log " $BITCOIND_CONTAINER started, waiting for RPC to be ready..."
# Wait for bitcoind RPC to be ready (max 60 seconds)
max_wait=60
wait_count=0
while [ $wait_count -lt $max_wait ]; do
BITCOIN_DATADIR=$(docker exec "$BITCOIND_CONTAINER" printenv BITCOIN_DIR 2>/dev/null || echo "/root/.bitcoin")
if docker exec "$BITCOIND_CONTAINER" bitcoin-cli -datadir="$BITCOIN_DATADIR" getblockchaininfo &>/dev/null; then
log " $BITCOIND_CONTAINER RPC ready"
break
fi
sleep 1
wait_count=$((wait_count + 1))
done
if [ $wait_count -ge $max_wait ]; then
log " WARN: $BITCOIND_CONTAINER RPC not ready after ${max_wait}s"
fi
else
log " $BITCOIND_CONTAINER FAILED"
fi
else
log "SKIP: $BITCOIND_CONTAINER container not found (bitcoind may run as systemd)"
fi
# 5. Docker: mempool stack (local only) - START AFTER BITCOIND
# Mempool depends on bitcoind being ready
if [ -f "$MEMPOOL_COMPOSE" ] && command -v docker &>/dev/null; then
log "Starting/restarting mempool (docker)..."
mem_ok=1
(cd "${PROJECT_DIR}/mempool" && docker compose -f docker-compose.signet.yml up -d &>/dev/null) && mem_ok=0
[ "$mem_ok" -ne 0 ] && (cd "${PROJECT_DIR}/mempool" && docker-compose -f docker-compose.signet.yml up -d &>/dev/null) && mem_ok=0
if [ "$mem_ok" -eq 0 ]; then
log " mempool started"
# Wait a bit for mempool to initialize
sleep 5
else
log " mempool FAILED"
fi
else
if [ ! -f "$MEMPOOL_COMPOSE" ]; then
log "SKIP: mempool compose not found (not deployed on this machine)"
else
log "SKIP: mempool requires docker (not available)"
fi
fi
log "=== Done ==="
tail -n 100 "$LOG_FILE" > "$LOG_FILE.tmp" && mv "$LOG_FILE.tmp" "$LOG_FILE"