Spaces:
Sleeping
Sleeping
File size: 4,377 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | #!/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
# Determine home directory
if [ -z "$HOME" ]; then
echo "HOME not set, defaulting to /root"
export HOME="/root"
fi
PICOCLAW_HOME="${PICOCLAW_HOME:-$HOME/.picoclaw}"
WORKSPACE_DIR="$PICOCLAW_HOME/workspace"
CONFIG_FILE="$PICOCLAW_HOME/config.json"
BACKUP_DIR="$PICOCLAW_HOME/backup"
echo "Using PICOCLAW_HOME: $PICOCLAW_HOME"
echo "Backup Dir: $BACKUP_DIR"
echo "Workspace Dir: $WORKSPACE_DIR"
# 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
# Check for token or SSH key (implied by absence of error)
if [ -z "$GITHUB_TOKEN" ] && [ ! -f "$HOME/.ssh/id_rsa" ] && [ ! -f "$HOME/.ssh/id_ed25519" ]; then
echo "Warning: GITHUB_TOKEN not set and no SSH keys found. Sync might fail if repo is private or requires auth."
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 if token is present
if [ -n "$GITHUB_TOKEN" ]; then
git config --global credential.helper store
echo "https://${GIT_AUTHOR_NAME}:${GITHUB_TOKEN}@github.com" > ~/.git-credentials
fi
}
# 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" || echo "Clone failed, continuing..."
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..."
mkdir -p "$WORKSPACE_DIR"
# Use rsync for better synchronization if available, otherwise cp
if command -v rsync >/dev/null 2>&1; then
rsync -av --update "$BACKUP_DIR/workspace/" "$WORKSPACE_DIR/"
else
cp -r "$BACKUP_DIR/workspace/"* "$WORKSPACE_DIR/" 2>/dev/null || true
fi
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)..."
# Run sync immediately on start
sync_now
while true; do
sleep "$SYNC_INTERVAL"
sync_now
done
}
sync_now() {
echo "Syncing data to dataset..."
# Copy current state to backup dir
mkdir -p "$BACKUP_DIR/workspace"
if [ -d "$WORKSPACE_DIR" ]; then
if command -v rsync >/dev/null 2>&1; then
rsync -av --update --exclude='.git' "$WORKSPACE_DIR/" "$BACKUP_DIR/workspace/"
else
cp -r "$WORKSPACE_DIR/"* "$BACKUP_DIR/workspace/" 2>/dev/null || true
fi
fi
if [ -f "$CONFIG_FILE" ]; then
cp "$CONFIG_FILE" "$BACKUP_DIR/config.json" 2>/dev/null || true
fi
# Commit and Push
if [ -d "$BACKUP_DIR/.git" ]; then
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
else
echo "Backup directory is not a git repository. Attempting initial sync..."
initial_sync
fi
}
# Main execution
setup_git
initial_sync
sync_loop
|