#!/bin/sh # git-sync: bidirectional mirror between IONOS (git.plate-software.de) and TrueNAS Gitea # IONOS = source of truth for cannamanage (Work Lumen pushes there) # TrueNAS push mirror already handles TrueNAS → IONOS for homelab pushes # This script handles the missing direction: IONOS → TrueNAS (pull mirror) set -e IONOS_TOKEN="${IONOS_TOKEN}" TRUENAS_TOKEN="${TRUENAS_TOKEN}" TRUENAS_HOST="${TRUENAS_HOST:-192.168.188.119:30008}" IONOS_HOST="${IONOS_HOST:-git.plate-software.de}" GITEA_USER="${GITEA_USER:-pplate}" INTERVAL="${INTERVAL:-300}" # 5 minutes default WORKDIR="/tmp/git-sync" mkdir -p "$WORKDIR" sync_repo() { REPO="$1" echo "[$(date '+%Y-%m-%d %H:%M:%S')] Syncing $REPO ..." IONOS_URL="https://${GITEA_USER}:${IONOS_TOKEN}@${IONOS_HOST}/${GITEA_USER}/${REPO}.git" TRUENAS_URL="http://${GITEA_USER}:${TRUENAS_TOKEN}@${TRUENAS_HOST}/${GITEA_USER}/${REPO}.git" REPO_DIR="${WORKDIR}/${REPO}" # bare clone has HEAD file instead of .git directory if [ ! -f "${REPO_DIR}/HEAD" ]; then echo " Cloning $REPO from IONOS..." git clone --mirror "$IONOS_URL" "$REPO_DIR" cd "$REPO_DIR" git remote add truenas "$TRUENAS_URL" else cd "$REPO_DIR" # Update remote URLs (tokens may rotate, protocol may change) git remote set-url origin "$IONOS_URL" git remote set-url truenas "$TRUENAS_URL" 2>/dev/null || git remote add truenas "$TRUENAS_URL" echo " Fetching from IONOS..." git fetch --all --prune 2>&1 | tail -5 fi echo " Pushing to TrueNAS..." git push truenas --all --force 2>&1 | tail -5 git push truenas --tags --force 2>&1 | tail -3 echo " Done." } # Repos to sync IONOS → TrueNAS REPOS="${REPOS:-cannamanage}" echo "=== git-sync starting, interval=${INTERVAL}s ===" echo "Repos: $REPOS" while true; do for REPO in $REPOS; do sync_repo "$REPO" || echo "[WARN] sync failed for $REPO — will retry next cycle" done echo "[$(date '+%Y-%m-%d %H:%M:%S')] Sleeping ${INTERVAL}s..." sleep "$INTERVAL" done