#!/bin/bash
# RESTORE SCRIPT
echo "WARNING: This will overwrite your current Gitea/DB/Runner data."
read -p "Are you sure? (y/N): " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then exit 1; fi

docker compose down

echo "Restoring Database Volume..."
docker compose up -d db
echo "Waiting for DB to initialize..."
sleep 15
cat database.sql | docker compose exec -T db psql -U ${POSTGRES_USER:-gitea} -d ${POSTGRES_DB:-gitea}

echo "Restoring Gitea Files..."
docker run --rm --volumes-from gitea -v $(pwd):/backup alpine tar xzf /backup/gitea_data.tar.gz -C /data

echo "Restoring Runner Files..."
docker run --rm --volumes-from gitea-runner -v $(pwd):/backup alpine tar xzf /backup/runner_data.tar.gz -C /data

echo "Restarting stack..."
docker compose up -d
echo "Restore Complete."
