123 lines
3.5 KiB
Bash
Executable File
123 lines
3.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
cd "$SCRIPT_DIR/.."
|
|
|
|
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'
|
|
|
|
info() { echo -e "${GREEN}[fittracker]${NC} $*"; }
|
|
warn() { echo -e "${YELLOW}[fittracker]${NC} $*"; }
|
|
error() { echo -e "${RED}[fittracker]${NC} $*"; exit 1; }
|
|
|
|
check_env() {
|
|
if [ ! -f .env ]; then
|
|
warn ".env not found — copying from .env.example"
|
|
cp .env.example .env
|
|
warn "Please edit .env and set required secrets, then re-run this script."
|
|
exit 1
|
|
fi
|
|
|
|
source .env
|
|
[ -z "${DB_PASSWORD:-}" ] && error "DB_PASSWORD must be set in .env"
|
|
[ -z "${SECRET_KEY:-}" ] && error "SECRET_KEY must be set in .env (use: openssl rand -hex 32)"
|
|
[ -z "${ADMIN_PASSWORD:-}" ] && error "ADMIN_PASSWORD must be set in .env"
|
|
info "Environment looks good"
|
|
}
|
|
|
|
generate_secrets() {
|
|
info "Generating .env from template..."
|
|
cp .env.example .env
|
|
SECRET=$(openssl rand -hex 32)
|
|
DB_PASS=$(openssl rand -base64 16 | tr -d '/+=')
|
|
ADMIN_PASS=$(openssl rand -base64 12 | tr -d '/+=')
|
|
REDIS_PASS=$(openssl rand -base64 12 | tr -d '/+=')
|
|
|
|
sed -i "s/changeme_generate_with_openssl_rand_hex_32/$SECRET/" .env
|
|
sed -i "s/changeme_strong_password/$DB_PASS/" .env
|
|
sed -i "s/changeme_admin_password/$ADMIN_PASS/" .env
|
|
sed -i "s/redispass/$REDIS_PASS/" .env
|
|
|
|
echo ""
|
|
echo -e "${GREEN}Generated secrets:${NC}"
|
|
echo " Admin username: admin"
|
|
echo " Admin password: $ADMIN_PASS"
|
|
echo " (saved to .env)"
|
|
echo ""
|
|
warn "Save these credentials! The admin password won't be shown again."
|
|
}
|
|
|
|
cmd_start() {
|
|
check_env
|
|
info "Starting FitTracker..."
|
|
docker compose up -d --build
|
|
info "Started! Visit http://localhost:${HTTP_PORT:-80}"
|
|
}
|
|
|
|
cmd_stop() {
|
|
info "Stopping FitTracker..."
|
|
docker compose down
|
|
}
|
|
|
|
cmd_logs() {
|
|
docker compose logs -f "${1:-}"
|
|
}
|
|
|
|
cmd_setup() {
|
|
info "First-time setup"
|
|
generate_secrets
|
|
cmd_start
|
|
}
|
|
|
|
cmd_backup() {
|
|
source .env
|
|
BACKUP_FILE="fittracker_backup_$(date +%Y%m%d_%H%M%S).sql"
|
|
info "Backing up database to $BACKUP_FILE..."
|
|
docker compose exec -T db pg_dump \
|
|
-U "${DB_USER:-fittracker}" fittracker > "$BACKUP_FILE"
|
|
info "Backup saved: $BACKUP_FILE"
|
|
}
|
|
|
|
cmd_restore() {
|
|
[ -z "${1:-}" ] && error "Usage: $0 restore <backup.sql>"
|
|
source .env
|
|
info "Restoring from $1..."
|
|
docker compose exec -T db psql \
|
|
-U "${DB_USER:-fittracker}" fittracker < "$1"
|
|
info "Restore complete"
|
|
}
|
|
|
|
cmd_update() {
|
|
info "Pulling latest and rebuilding..."
|
|
git pull
|
|
docker compose build --no-cache
|
|
docker compose up -d
|
|
info "Update complete"
|
|
}
|
|
|
|
case "${1:-help}" in
|
|
setup) cmd_setup ;;
|
|
start) cmd_start ;;
|
|
stop) cmd_stop ;;
|
|
restart) cmd_stop; cmd_start ;;
|
|
logs) cmd_logs "${2:-}" ;;
|
|
backup) cmd_backup ;;
|
|
restore) cmd_restore "${2:-}" ;;
|
|
update) cmd_update ;;
|
|
*)
|
|
echo "FitTracker management script"
|
|
echo ""
|
|
echo "Usage: $0 <command>"
|
|
echo ""
|
|
echo "Commands:"
|
|
echo " setup First-time setup (generates secrets, starts containers)"
|
|
echo " start Start all containers"
|
|
echo " stop Stop all containers"
|
|
echo " restart Restart all containers"
|
|
echo " logs Follow logs (optionally: logs backend)"
|
|
echo " backup Backup PostgreSQL database"
|
|
echo " restore Restore from backup: restore <file.sql>"
|
|
echo " update Pull and rebuild"
|
|
;;
|
|
esac
|