#!/bin/bash set -e # Configuration DATASET_REPO="${DATASET_REPO:-https://github.com/personalbotai/picoclaw-memory.git}" # Default: personalbotai/picoclaw-memory SYNC_INTERVAL="${SYNC_INTERVAL:-300}" # Default: 5 minutes WORKSPACE_DIR="/root/.picoclaw/workspace" CONFIG_FILE="/root/.picoclaw/config.json" BACKUP_DIR="/root/.picoclaw/backup" # Prevent git from asking for credentials interactively export GIT_TERMINAL_PROMPT=0 if [ -z "$DATASET_REPO" ]; then echo "DATASET_REPO environment variable not set. Skipping dataset sync." exit 0 fi if [ -z "$GITHUB_TOKEN" ]; then echo "GITHUB_TOKEN environment variable not set. Cannot sync to dataset." exit 0 fi # Setup Git for Dataset setup_git() { echo "Setting up git for dataset sync..." git config --global user.name "${GIT_AUTHOR_NAME:-picoclaw}" git config --global user.email "${GIT_AUTHOR_EMAIL:-picoclaw@example.com}" # Configure credential helper for GitHub git config --global credential.helper store # Store credentials for GitHub echo "https://${GIT_AUTHOR_NAME}:${GITHUB_TOKEN}@github.com" > ~/.git-credentials } # Initial Clone/Pull initial_sync() { mkdir -p "$BACKUP_DIR" if [ ! -d "$BACKUP_DIR/.git" ]; then echo "Cloning dataset $DATASET_REPO..." git clone "$DATASET_REPO" "$BACKUP_DIR" else echo "Pulling latest changes from dataset..." cd "$BACKUP_DIR" && git pull origin main || echo "Pull failed, continuing..." fi # Restore to workspace if backup has data if [ -d "$BACKUP_DIR/workspace" ]; then echo "Restoring workspace from backup..." cp -r "$BACKUP_DIR/workspace/"* "$WORKSPACE_DIR/" 2>/dev/null || true fi if [ -f "$BACKUP_DIR/config.json" ]; then echo "Restoring config from backup..." cp "$BACKUP_DIR/config.json" "$CONFIG_FILE" 2>/dev/null || true fi } # Sync Loop sync_loop() { echo "Starting sync loop (interval: ${SYNC_INTERVAL}s)..." while true; do sleep "$SYNC_INTERVAL" echo "Syncing data to dataset..." # Copy current state to backup dir mkdir -p "$BACKUP_DIR/workspace" cp -r "$WORKSPACE_DIR/"* "$BACKUP_DIR/workspace/" 2>/dev/null || true cp "$CONFIG_FILE" "$BACKUP_DIR/config.json" 2>/dev/null || true # Commit and Push cd "$BACKUP_DIR" # Security check: Prevent pushing to upstream repository REMOTE_URL=$(git remote get-url origin 2>/dev/null || echo "") if echo "$REMOTE_URL" | grep -q "sipeed/picoclaw"; then echo "SECURITY WARNING: Detected attempt to push to upstream (sipeed/picoclaw). Aborting push." return fi if [[ -n $(git status -s) ]]; then git add . git commit -m "Auto-sync: $(date '+%Y-%m-%d %H:%M:%S')" git push origin main || echo "Push failed, will retry next time..." echo "Sync completed successfully." else echo "No changes to sync." fi done } # Main execution setup_git initial_sync sync_loop &