forked from AlexeyKrasnoperov/openclaw-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
217 lines (187 loc) · 6.53 KB
/
setup.sh
File metadata and controls
217 lines (187 loc) · 6.53 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env bash
set -euo pipefail
# OpenClaw backup setup — run once to provision backup infrastructure
# Prerequisites: git, rsync, crontab, SSH key added to GitHub
# ============================================================
# Config
# ============================================================
REMOTE_URL="${1:-}"
CRON_FREQUENCY="${2:-60}" # minutes, default 60
OPENCLAW_DIR="${HOME}/.openclaw"
BACKUP_SCRIPT_DIR="${HOME}/.openclaw/backup"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# ============================================================
# Input validation
# ============================================================
if [ -z "$REMOTE_URL" ]; then
echo "Usage: ./setup.sh <git-remote-url> [frequency-minutes]"
echo ""
echo "Examples:"
echo " ./setup.sh openclaw-backup:user/repo.git # SSH host alias"
echo " ./setup.sh git@github.com:user/repo.git # direct SSH"
echo " ./setup.sh openclaw-backup:user/repo.git 30 # every 30 min"
exit 1
fi
if ! [[ "$CRON_FREQUENCY" =~ ^[0-9]+$ ]] || [ "$CRON_FREQUENCY" -lt 1 ]; then
echo "ERROR: frequency must be a positive integer (minutes). Got: '$CRON_FREQUENCY'"
exit 1
fi
# ============================================================
# Pre-flight checks
# ============================================================
echo "==> Checking prerequisites..."
for cmd in git rsync crontab; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "ERROR: '$cmd' is not installed. Install it first."
exit 1
fi
done
# Test SSH connectivity if using SSH remote
if [[ "$REMOTE_URL" == *:* ]] && [[ "$REMOTE_URL" != https://* ]]; then
SSH_HOST="${REMOTE_URL%%:*}"
ssh_output=$(ssh -T "$SSH_HOST" 2>&1 || true)
if ! echo "$ssh_output" | grep -qi "successfully authenticated"; then
echo "ERROR: SSH authentication to '$SSH_HOST' failed."
echo " Check your SSH key and ~/.ssh/config"
exit 1
fi
fi
echo " Remote: $REMOTE_URL"
echo " Directory: $OPENCLAW_DIR"
echo " Frequency: every ${CRON_FREQUENCY} min"
echo ""
read -rp "Proceed? [y/N] " confirm
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
echo "Aborted."
exit 0
fi
# ============================================================
# 1. Initialize git in OpenClaw directory
# ============================================================
echo "==> Initializing backup repo..."
mkdir -p "$OPENCLAW_DIR"
if [ ! -d "$OPENCLAW_DIR" ]; then
echo "ERROR: failed to create $OPENCLAW_DIR"
exit 1
fi
cd "$OPENCLAW_DIR"
if [ ! -d .git ]; then
git init -b main
fi
git config user.name "openclaw-backup-bot"
git config user.email "openclaw-backup-bot@noreply"
# Set remote
if git remote get-url origin >/dev/null 2>&1; then
git remote set-url origin "$REMOTE_URL"
else
git remote add origin "$REMOTE_URL"
fi
# ============================================================
# 2. Copy .gitignore
# ============================================================
echo "==> Installing .gitignore..."
GITIGNORE_TEMPLATE="$SCRIPT_DIR/workspace.gitignore"
if [ -f "$GITIGNORE_TEMPLATE" ]; then
cp "$GITIGNORE_TEMPLATE" "$OPENCLAW_DIR/.gitignore"
echo " Copied workspace.gitignore → .gitignore"
else
echo " WARNING: workspace.gitignore not found next to setup.sh — create .gitignore manually!"
fi
# ============================================================
# 3. Install backup script
# ============================================================
echo "==> Installing backup script..."
mkdir -p "$BACKUP_SCRIPT_DIR"
if [ -f "$SCRIPT_DIR/backup.sh" ]; then
cp "$SCRIPT_DIR/backup.sh" "$BACKUP_SCRIPT_DIR/backup.sh"
chmod +x "$BACKUP_SCRIPT_DIR/backup.sh"
echo " Installed to $BACKUP_SCRIPT_DIR/backup.sh"
else
echo "ERROR: backup.sh not found next to setup.sh"
exit 1
fi
# ============================================================
# 4. Create initial workspace snapshot & commit
# ============================================================
echo "==> Initial commit..."
# Fetch remote state first (only if remote has a main branch)
if git ls-remote origin main 2>/dev/null | grep -q main; then
echo " Remote has existing commits, syncing..."
git fetch origin main
if ! git merge --ff-only origin/main; then
echo "ERROR: cannot fast-forward merge remote. Resolve manually."
exit 1
fi
fi
# Snapshot workspace without .git
SNAPSHOT_DIR="${OPENCLAW_DIR}/workspace-snapshot"
rm -rf "$SNAPSHOT_DIR"
if [ -d workspace ]; then
rsync -a --exclude=.git workspace/ "$SNAPSHOT_DIR/"
echo " Snapshotted workspace → workspace-snapshot"
fi
git add -A
NEED_PUSH=false
if git diff --cached --quiet; then
echo " Nothing new to commit."
if git rev-parse --verify origin/main >/dev/null 2>&1; then
if git log origin/main..HEAD --oneline 2>/dev/null | grep -q .; then
NEED_PUSH=true
fi
elif git log --oneline -1 >/dev/null 2>&1; then
# origin/main doesn't exist but we have local commits
NEED_PUSH=true
fi
else
git commit -m "initial backup"
NEED_PUSH=true
fi
if [ "$NEED_PUSH" = true ]; then
if ! git push -u origin main; then
echo "ERROR: initial push failed. Fix the issue before cron is installed."
rm -rf "$SNAPSHOT_DIR"
exit 1
fi
echo " Pushed to $REMOTE_URL"
else
echo " Already in sync with remote."
fi
rm -rf "$SNAPSHOT_DIR"
# ============================================================
# 5. Install cron job
# ============================================================
echo "==> Setting up cron..."
CRON_CMD="$BACKUP_SCRIPT_DIR/backup.sh"
# Build cron expression from minutes
if [ "$CRON_FREQUENCY" -le 59 ]; then
CRON_EXPR="*/$CRON_FREQUENCY * * * *"
else
hours=$(( CRON_FREQUENCY / 60 ))
[ "$hours" -lt 1 ] && hours=1
CRON_EXPR="0 */$hours * * *"
fi
CRON_LINE="$CRON_EXPR $CRON_CMD"
EXISTING_LINE=$(crontab -l 2>/dev/null | grep -F "$CRON_CMD" || true)
if [ -n "$EXISTING_LINE" ]; then
if [ "$EXISTING_LINE" = "$CRON_LINE" ]; then
echo " Cron job already exists with correct schedule, skipping."
else
echo " Updating cron schedule..."
{ crontab -l 2>/dev/null | { grep -vF "$CRON_CMD" || true; }; echo "$CRON_LINE"; } | crontab -
echo " Updated: $CRON_LINE"
fi
else
(crontab -l 2>/dev/null; echo "$CRON_LINE") | crontab -
echo " Installed: $CRON_LINE"
fi
# ============================================================
# Done
# ============================================================
echo ""
echo "Setup complete."
echo " Remote: $REMOTE_URL"
echo " Cron: $CRON_LINE"
echo " Script: $BACKUP_SCRIPT_DIR/backup.sh"
echo " Log: $BACKUP_SCRIPT_DIR/backup.log"
echo ""
echo "Test manually: $CRON_CMD"