-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup.sh
More file actions
247 lines (224 loc) · 8.34 KB
/
Copy pathbackup.sh
File metadata and controls
247 lines (224 loc) · 8.34 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env bash
# claude-code-backup — backup.sh
# Syncs ~/.claude and ~/.openclaude into two private GitHub repos.
#
# Usage:
# ./backup.sh # use config from ./.env
# ./backup.sh --dry-run # show what would happen, no commits/pushes
#
# Env (.env in same dir; .env takes precedence over shell env, like dotenv):
# CLAUDE_BACKUP_REPO e.g. dexteon/claude-config-backup
# OPENCLAUDE_BACKUP_REPO e.g. dexteon/openclaude-config-backup
# CLAUDE_HOME default: $HOME/.claude
# OPENCLAUDE_HOME default: $HOME/.openclaude
# BACKUP_WORKDIR default: $HOME/.claude-backup-work (clone roots)
set -euo pipefail
# --- locate .env -----------------------------------------------------------
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [[ -f "$SCRIPT_DIR/.env" ]]; then
set -a
# shellcheck disable=SC1091
source "$SCRIPT_DIR/.env"
set +a
fi
DRY_RUN=0
for arg in "$@"; do
case "$arg" in
--dry-run) DRY_RUN=1 ;;
-h|--help)
sed -n '2,14p' "${BASH_SOURCE[0]}" | sed 's/^# \{0,1\}//'
exit 0 ;;
*) echo "unknown arg: $arg (try --help)" >&2; exit 2 ;;
esac
done
# --- required config -------------------------------------------------------
: "${CLAUDE_BACKUP_REPO:?CLAUDE_BACKUP_REPO not set (run ./setup.sh first)}"
: "${OPENCLAUDE_BACKUP_REPO:?OPENCLAUDE_BACKUP_REPO not set (run ./setup.sh first)}"
CLAUDE_HOME="${CLAUDE_HOME:-$HOME/.claude}"
OPENCLAUDE_HOME="${OPENCLAUDE_HOME:-$HOME/.openclaude}"
BACKUP_WORKDIR="${BACKUP_WORKDIR:-$HOME/.claude-backup-work}"
# --- helpers ---------------------------------------------------------------
# Convert bash path (/c/Users/Dex/.claude) to Windows path (C:\Users\Dex\.claude)
to_win_path() {
local p="$1"
# drive letter: /c/... -> C:\...
if [[ "$p" =~ ^/([a-zA-Z])/(.*)$ ]]; then
local drive="${BASH_REMATCH[1]}"
local rest="${BASH_REMATCH[2]}"
rest="${rest//\//\\}"
printf '%s:\\%s\n' "${drive^}" "$rest"
else
p="${p//\//\\}"
printf '%s\n' "$p"
fi
}
log() { printf '[backup] %s\n' "$*"; }
fail() { printf '[backup][ERROR] %s\n' "$*" >&2; exit 1; }
run() {
if (( DRY_RUN )); then
printf '[dry-run] %s\n' "$*"
else
"$@"
fi
}
command -v git >/dev/null || fail "git not found on PATH"
command -v gh >/dev/null || fail "gh CLI not found on PATH"
gh auth status >/dev/null 2>&1 || fail "gh not authenticated (run: gh auth login)"
# --- rsync-replacement via robocopy (native, fast on Windows) --------------
# Falls back to cp -r when robocopy missing (WSL/Linux/macOS).
# Excludes split: directory names go to /XD, file globs to /XF.
# robocopy is buggy with mixed args, so we build /XD and /XF separately.
EXCLUDE_DIRS=(
".git" "node_modules" "cache" "logs" "Cache" "Code Cache" "GPUCache"
"Service Worker" "backups" "paste-cache" "sessions" "projects"
"ShaderCache" "GrShaderCache"
"tasks" "teams" "telemetry"
)
EXCLUDE_FILES=(
"*.log" "*.tmp" "*.output" "*.lock" ".last-cleanup"
)
sync_dir() {
local src="$1" dst="$2" label="$3"
log "sync $label: $src -> $dst"
if [[ ! -d "$src" ]]; then
log " source missing, skip ($label)"
return 0
fi
mkdir -p "$dst"
if command -v robocopy >/dev/null 2>&1; then
local win_src win_dst
win_src="$(to_win_path "$src")"
win_dst="$(to_win_path "$dst")"
local rob_args=( /MIR /R:1 /W:1 /NFL /NDL /NJH /NP )
rob_args+=( /XD "${EXCLUDE_DIRS[@]}" )
rob_args+=( /XF "${EXCLUDE_FILES[@]}" )
if (( DRY_RUN )); then rob_args+=( /L ); fi
# MSYS_NO_PATHCONV=1 stops Git Bash from mangling /MIR -> C:\...\MIR
# robocopy rc 0-7 = clean success; 8 = files FAILED (real error);
# 9-15 = copied with warnings (locked files, mismatches) — acceptable for backup
MSYS_NO_PATHCONV=1 MSYS2_ARG_CONV_EXCL='*' \
robocopy "$win_src" "$win_dst" "${rob_args[@]}" >/dev/null 2>&1 || {
local rc=$?
# robocopy rc 0-7 = clean success (1=copied, 2=extras, 3=both, ...).
# 8 = files FAILED; 9-15 = partial copy w/ failures (locked/mismatch);
# >=16 = fatal. Only 8+ is worth surfacing.
if (( rc >= 16 )); then
fail "robocopy serious error (rc=$rc) for $label"
elif (( rc == 8 )); then
fail "robocopy failed (rc=8) for $label — files could not be copied"
elif (( rc >= 9 )); then
log " robocopy rc=$rc ($label, copied with warnings — likely locked files)"
fi
}
elif command -v rsync >/dev/null 2>&1; then
local rs_args=( -a --delete )
local e
for e in "${EXCLUDE_DIRS[@]}" "${EXCLUDE_FILES[@]}"; do
rs_args+=( --exclude="$e" )
done
if (( DRY_RUN )); then rs_args+=( --dry-run ); fi
rsync "${rs_args[@]}" "$src/" "$dst/"
else
run cp -rf "$src/." "$dst/"
fi
}
# Back up project memory dirs. projects/ is excluded wholesale by sync_dir
# (per-project session transcripts are large and volatile), but the small
# projects/<p>/memory/ subdirs hold persistent knowledge worth keeping.
# /MIR on each memory dir individually keeps it in sync without pulling in
# the surrounding transcript files.
sync_memory() {
local src="$1" dst="$2" label="$3"
[[ -d "$src/projects" ]] || return 0
local md
for md in "$src"/projects/*/memory; do
[[ -d "$md" ]] || continue
local rel="${md#"$src"/}" # projects/<p>/memory
local dmd="$dst/$rel"
log "sync memory $label: $rel"
mkdir -p "$dmd"
if command -v robocopy >/dev/null 2>&1; then
local ws wd
ws="$(to_win_path "$md")"
wd="$(to_win_path "$dmd")"
local ra=( /MIR /R:1 /W:1 /NFL /NDL /NJH /NP )
(( DRY_RUN )) && ra+=( /L )
MSYS_NO_PATHCONV=1 MSYS2_ARG_CONV_EXCL='*' \
robocopy "$ws" "$wd" "${ra[@]}" >/dev/null 2>&1 || {
local rc=$?
if (( rc >= 16 )); then
fail "robocopy serious error (rc=$rc) for $label memory"
elif (( rc == 8 )); then
fail "robocopy failed (rc=8) for $label memory"
fi
}
elif command -v rsync >/dev/null 2>&1; then
local rsa=( -a --delete )
(( DRY_RUN )) && rsa+=( --dry-run )
rsync "${rsa[@]}" "$md/" "$dmd/"
else
run cp -rf "$md/." "$dmd/"
fi
done
}
push_repo() {
local workdir="$1" repo="$2" label="$3"
log "commit+push $label -> $repo"
(
cd "$workdir" || fail "workdir missing: $workdir"
run git add -A
# only commit if there are staged changes
if ! run git diff --cached --quiet; then
run git commit -m "backup $(date -u +%Y-%m-%dT%H:%M:%SZ)"
else
log " no changes ($label)"
fi
if (( DRY_RUN )); then
log " [dry-run] would push to $repo"
else
git push origin HEAD 2>&1 | sed 's/^/[backup] /' || fail "push failed for $label"
fi
)
}
clone_or_pull_repo() {
local repo="$1" dst="$2" label="$3"
if [[ -d "$dst/.git" ]]; then
log "existing clone: $label ($dst)"
( cd "$dst" && run git pull --rebase --ff-only 2>&1 | sed 's/^/[backup] /' ) || true
else
log "clone $label -> $dst"
mkdir -p "$dst"
rmdir "$dst" 2>/dev/null || true
if (( DRY_RUN )); then
log " [dry-run] would clone $repo"
else
git clone "https://github.com/$repo.git" "$dst" 2>&1 | sed 's/^/[backup] /' \
|| fail "clone failed for $label"
fi
fi
}
# Force-remove a workdir using PowerShell on Windows (rm -rf fails on locked .git/objects)
rm_workdir_force() {
local p="$1"
if [[ -d "$p" ]]; then
local win_p; win_p="$(to_win_path "$p")"
powershell.exe -NoProfile -Command \
"Remove-Item -LiteralPath '$win_p' -Recurse -Force -ErrorAction SilentlyContinue" \
2>/dev/null || true
# also try rm -rf as best effort
rm -rf "$p" 2>/dev/null || true
fi
}
# --- main ------------------------------------------------------------------
CLAUDE_WORKDIR="$BACKUP_WORKDIR/claude"
OPENCLAUDE_WORKDIR="$BACKUP_WORKDIR/openclaude"
mkdir -p "$BACKUP_WORKDIR"
clone_or_pull_repo "$CLAUDE_BACKUP_REPO" "$CLAUDE_WORKDIR" "claude"
clone_or_pull_repo "$OPENCLAUDE_BACKUP_REPO" "$OPENCLAUDE_WORKDIR" "openclaude"
sync_dir "$CLAUDE_HOME" "$CLAUDE_WORKDIR" "claude"
sync_dir "$OPENCLAUDE_HOME" "$OPENCLAUDE_WORKDIR" "openclaude"
sync_memory "$CLAUDE_HOME" "$CLAUDE_WORKDIR" "claude"
sync_memory "$OPENCLAUDE_HOME" "$OPENCLAUDE_WORKDIR" "openclaude"
push_repo "$CLAUDE_WORKDIR" "$CLAUDE_BACKUP_REPO" "claude"
push_repo "$OPENCLAUDE_WORKDIR" "$OPENCLAUDE_BACKUP_REPO" "openclaude"
log "done."