#!/bin/bash
# =============================================================================
# Matrix Synapse Backup Script
# Creates a complete backup for migration
# =============================================================================
# Run as root

set -e

BACKUP_DIR="${BACKUP_DIR:-/opt/synapse/backups}"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_NAME="matrix_backup_${TIMESTAMP}"
BACKUP_PATH="${BACKUP_DIR}/${BACKUP_NAME}"

echo "=========================================="
echo "Matrix Synapse Backup Script"
echo "Backup location: ${BACKUP_PATH}"
echo "=========================================="

mkdir -p "${BACKUP_PATH}"

# 1. Backup PostgreSQL
echo "[1/5] Backing up PostgreSQL database..."
sudo -u postgres pg_dump -Fc synapse > "${BACKUP_PATH}/synapse.dump"
echo "  Database: $(du -h ${BACKUP_PATH}/synapse.dump | cut -f1)"

# 2. Backup Synapse config and keys
echo "[2/5] Backing up configuration..."
cp /opt/synapse/homeserver.yaml "${BACKUP_PATH}/"
cp /opt/synapse/*.signing.key "${BACKUP_PATH}/" 2>/dev/null || true
cp /opt/synapse/*.log.config "${BACKUP_PATH}/" 2>/dev/null || true
cp /root/.matrix_secrets "${BACKUP_PATH}/" 2>/dev/null || true

# 3. Backup media
echo "[3/5] Backing up media files (this may take a while)..."
if [ -d /opt/synapse/media_store ]; then
    tar -czf "${BACKUP_PATH}/media_store.tar.gz" -C /opt/synapse media_store
    echo "  Media: $(du -h ${BACKUP_PATH}/media_store.tar.gz | cut -f1)"
else
    echo "  No media directory found"
fi

# 4. Backup Element config
echo "[4/5] Backing up Element config..."
cp /opt/element/web/config.json "${BACKUP_PATH}/element_config.json" 2>/dev/null || true

# 5. Backup TURN config
echo "[5/5] Backing up TURN config..."
cp /etc/turnserver.conf "${BACKUP_PATH}/" 2>/dev/null || true

# Create restore instructions
cat > "${BACKUP_PATH}/RESTORE.md" << 'RESTORE'
# Matrix Restore Instructions

## On the new server:

1. Run the install script first (it will create a fresh install)

2. Stop services:
   ```
   systemctl stop synapse nginx coturn
   ```

3. Restore database:
   ```
   sudo -u postgres dropdb synapse
   sudo -u postgres createdb -O synapse -E UTF8 -l C -T template0 synapse
   sudo -u postgres pg_restore -d synapse synapse.dump
   ```

4. Restore config files:
   ```
   cp homeserver.yaml /opt/synapse/
   cp *.signing.key /opt/synapse/
   cp *.log.config /opt/synapse/
   chown -R synapse:synapse /opt/synapse
   ```

5. Restore media:
   ```
   cd /opt/synapse
   tar -xzf /path/to/backup/media_store.tar.gz
   chown -R synapse:synapse media_store
   ```

6. Restore TURN config:
   ```
   cp turnserver.conf /etc/turnserver.conf
   ```

7. Restore Element config:
   ```
   cp element_config.json /opt/element/web/config.json
   ```

8. Start services:
   ```
   systemctl start coturn nginx synapse
   ```
RESTORE

# Create archive
echo ""
echo "Creating final archive..."
cd "${BACKUP_DIR}"
tar -czf "${BACKUP_NAME}.tar.gz" "${BACKUP_NAME}"
rm -rf "${BACKUP_NAME}"

FINAL_SIZE=$(du -h "${BACKUP_DIR}/${BACKUP_NAME}.tar.gz" | cut -f1)

echo ""
echo "=========================================="
echo "✅ Backup Complete!"
echo "=========================================="
echo ""
echo "Backup file: ${BACKUP_DIR}/${BACKUP_NAME}.tar.gz"
echo "Size: ${FINAL_SIZE}"
echo ""
echo "Download: scp root@server:${BACKUP_DIR}/${BACKUP_NAME}.tar.gz ."
