File size: 3,129 Bytes
da590a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/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 &