#!/bin/bash set -e # Start Redis echo "Starting Redis..." redis-server --daemonize yes # Start PostgreSQL echo "Starting PostgreSQL..." service postgresql start # Wait for PostgreSQL to start up until pg_isready -h localhost; do echo "Waiting for PostgreSQL..." sleep 1 done # Check if database already exists, if not create it if ! su - postgres -c "psql -lqt" | cut -d \| -f 1 | grep -qw "postiz-db-local"; then echo "Creating PostgreSQL user and database..." su - postgres -c "psql -c \"CREATE USER \\\"postiz-user\\\" WITH PASSWORD 'postiz-password' SUPERUSER;\"" su - postgres -c "psql -c \"CREATE DATABASE \\\"postiz-db-local\\\" OWNER \\\"postiz-user\\\";\"" fi # Restore backup from GitHub if configured if [ -n "$GITHUB_PAT" ] && [ -n "$GITHUB_REPO" ] && [ -n "$GITHUB_BRANCH" ]; then echo "GitHub backup configuration found. Restoring..." BACKUP_DIR="/tmp/backup_repo" rm -rf "$BACKUP_DIR" git clone --depth 1 --branch "$GITHUB_BRANCH" "https://${GITHUB_PAT}@github.com/${GITHUB_REPO}.git" "$BACKUP_DIR" || { echo "Could not clone branch $GITHUB_BRANCH. Trying to clone repository root..." git clone --depth 1 "https://${GITHUB_PAT}@github.com/${GITHUB_REPO}.git" "$BACKUP_DIR" || true } if [ -d "$BACKUP_DIR" ] && [ -f "$BACKUP_DIR/backup.sql" ]; then echo "Restoring database from backup..." su - postgres -c "psql -d postiz-db-local -f $BACKUP_DIR/backup.sql" || echo "Restore from backup failed." else echo "No backup.sql found. Starting with a fresh database." fi else echo "GitHub backup configuration not provided. Skipping restore." fi # Start Nginx echo "Starting Nginx..." nginx # Start the Backup Daemon in the background if [ -n "$GITHUB_PAT" ] && [ -n "$GITHUB_REPO" ] && [ -n "$GITHUB_BRANCH" ]; then echo "Starting GitHub backup daemon..." chmod +x /app/var/docker/backup.sh /app/var/docker/backup.sh & fi # Run the application via PM2 echo "Starting Postiz via PM2..." exec pnpm run pm2