| BACKUP_DIR="/tmp/backup_repo" | |
| echo "Backup daemon started..." | |
| # Configure git globally so commits succeed | |
| git config --global user.email "backup-bot@huggingface.space" | |
| git config --global user.name "Hugging Face Backup Bot" | |
| while true; do | |
| # Wait for 30 minutes first (or sleep on startup so the app is up first) | |
| echo "Backup daemon sleeping for 30 minutes..." | |
| sleep 1800 | |
| if [ -n "$GITHUB_PAT" ] && [ -n "$GITHUB_REPO" ] && [ -n "$GITHUB_BRANCH" ]; then | |
| echo "Running pg_dump..." | |
| mkdir -p /tmp/backup_temp | |
| su - postgres -c "pg_dump -d postiz-db-local -F p" > /tmp/backup_temp/backup.sql || { | |
| echo "pg_dump failed." | |
| continue | |
| } | |
| echo "Cloning git backup repo..." | |
| rm -rf "$BACKUP_DIR" | |
| git clone "https://${GITHUB_PAT}@github.com/${GITHUB_REPO}.git" "$BACKUP_DIR" || { | |
| echo "Failed to clone backup repo." | |
| continue | |
| } | |
| cd "$BACKUP_DIR" | |
| # Ensure branch exists | |
| git checkout "$GITHUB_BRANCH" || git checkout -b "$GITHUB_BRANCH" || true | |
| cp /tmp/backup_temp/backup.sql "$BACKUP_DIR/backup.sql" | |
| # Check if there are changes to push | |
| if [ -n "$(git status --porcelain)" ]; then | |
| echo "Database changes detected. Committing and pushing..." | |
| git add backup.sql | |
| git commit -m "Auto-backup $(date -uIs)" | |
| git push origin "$GITHUB_BRANCH" || echo "Git push failed." | |
| else | |
| echo "No database changes detected." | |
| fi | |
| else | |
| echo "GitHub backup configuration variables missing. Skipping this backup cycle." | |
| fi | |
| done | |