diff --git a/dev.sh b/dev.sh index 2f431f2..171faa6 100755 --- a/dev.sh +++ b/dev.sh @@ -12,6 +12,12 @@ cleanup() { } trap cleanup EXIT INT TERM +# Optional: sync content from server +if [ "${SYNC:-}" = "1" ] || [ "${1:-}" = "--sync" ]; then + echo "Syncing content from server..." + ./sync.sh pull +fi + # Start API in background echo "Starting RustyCMS API on http://127.0.0.1:3000 ..." cargo run & diff --git a/sync.sh b/sync.sh new file mode 100755 index 0000000..8529c3f --- /dev/null +++ b/sync.sh @@ -0,0 +1,87 @@ +#!/usr/bin/env bash +# Sync content between local and server +# +# Usage: +# ./sync.sh pull # Server → Local (before starting dev) +# ./sync.sh push # Local → Server (after local edits) +# ./sync.sh watch # Auto-push on local file changes (requires fswatch) +# ./sync.sh status # Show diff between local and server + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd "$SCRIPT_DIR" + +SERVER="root@167.86.74.105" +SSH_KEY="$HOME/.ssh/contabo_rsa" +REMOTE_CONTENT="/opt/rustycms/content" +LOCAL_CONTENT="./content" + +SSH_OPTS="-o StrictHostKeyChecking=no -i $SSH_KEY" +RSYNC_OPTS="-avz --delete -e \"ssh $SSH_OPTS\"" + +# Colors +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' + +pull() { + echo -e "${BLUE}← Pulling content from server...${NC}" + rsync -avz --delete \ + -e "ssh $SSH_OPTS" \ + "$SERVER:$REMOTE_CONTENT/" \ + "$LOCAL_CONTENT/" + echo -e "${GREEN}✓ Pull complete${NC}" +} + +push() { + echo -e "${BLUE}→ Pushing content to server...${NC}" + rsync -avz --delete \ + -e "ssh $SSH_OPTS" \ + "$LOCAL_CONTENT/" \ + "$SERVER:$REMOTE_CONTENT/" + echo -e "${GREEN}✓ Push complete${NC}" +} + +status() { + echo -e "${BLUE}~ Comparing local ↔ server...${NC}" + rsync -avzn --delete \ + -e "ssh $SSH_OPTS" \ + "$LOCAL_CONTENT/" \ + "$SERVER:$REMOTE_CONTENT/" \ + | grep -v "^sending\|^sent\|^total\|^$" || true + echo -e "${YELLOW}(dry-run, no changes made)${NC}" +} + +watch() { + if ! command -v fswatch &>/dev/null; then + echo "fswatch not found. Install with: brew install fswatch" + exit 1 + fi + echo -e "${BLUE}👁 Watching ./content for changes (Ctrl+C to stop)...${NC}" + fswatch -o "$LOCAL_CONTENT" | while read -r _; do + echo -e "${BLUE}→ Change detected, pushing...${NC}" + rsync -az --delete \ + -e "ssh $SSH_OPTS" \ + "$LOCAL_CONTENT/" \ + "$SERVER:$REMOTE_CONTENT/" \ + && echo -e "${GREEN}✓ Pushed$(date +' %H:%M:%S')${NC}" + done +} + +case "${1:-}" in + pull) pull ;; + push) push ;; + status) status ;; + watch) watch ;; + *) + echo "Usage: ./sync.sh [pull|push|status|watch]" + echo "" + echo " pull Server → Local (fetch latest content before dev)" + echo " push Local → Server (upload local edits)" + echo " status Show what would change on push (dry-run)" + echo " watch Auto-push on local file changes (requires fswatch)" + exit 1 + ;; +esac