#!/bin/bash
# PeerCortex safe deploy script — always backup before restart
BACKUP_DIR=/opt/peercortex-app/backups
mkdir -p $BACKUP_DIR
TS=$(date +%Y%m%d_%H%M%S)
cp /opt/peercortex-app/server.js $BACKUP_DIR/server.js.$TS
cp /opt/peercortex-app/public/index.html $BACKUP_DIR/index.html.$TS
# Keep only last 20 backups
ls -t $BACKUP_DIR/server.js.* 2>/dev/null | tail -n +21 | xargs rm -f 2>/dev/null
ls -t $BACKUP_DIR/index.html.* 2>/dev/null | tail -n +21 | xargs rm -f 2>/dev/null
echo "[backup] Saved server.js + index.html ($TS)"
pm2 restart peercortex
