-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·94 lines (77 loc) · 2.62 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·94 lines (77 loc) · 2.62 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
#!/bin/bash
# Post-clone setup: check tool dependencies, render plist templates, install launchd schedule.
#
# Idempotent — safe to re-run after upgrades.
#
# Renders templates into $HOME/Library/LaunchAgents/ with the current user's
# $HOME / username / script path baked in. Templates themselves stay generic
# in the repo. Logs are written to $HOME/Library/Logs/claude-backup/.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
LAUNCH_AGENTS="$HOME/Library/LaunchAgents"
LOG_DIR="$HOME/Library/Logs/claude-backup"
USERNAME="$(id -un)"
UID_NUM="$(id -u)"
echo "=== claude-backup setup ==="
echo " Repo: $SCRIPT_DIR"
echo " User: $USERNAME (uid $UID_NUM)"
echo " Logs: $LOG_DIR"
echo " LaunchAgents: $LAUNCH_AGENTS"
echo
# -- 1. Tool dependency check --
echo "[1/3] Checking tool dependencies..."
missing=0
check_cmd() {
if command -v "$1" &>/dev/null; then
echo " ok $1"
else
echo " -- $1 (install: $2)"
missing=1
fi
}
check_cmd rclone "brew install rclone"
check_cmd gh "brew install gh"
check_cmd git "xcode-select --install"
if [ "$missing" -eq 1 ]; then
echo
echo " Some tools are missing. Install them above and re-run, or continue;"
echo " the launchd jobs will SKIP gracefully when their dependency is absent."
fi
echo
# -- 2. Prepare directories --
echo "[2/3] Preparing directories..."
mkdir -p "$LAUNCH_AGENTS" "$LOG_DIR"
echo " ok $LAUNCH_AGENTS"
echo " ok $LOG_DIR"
echo
# -- 3. Render templates and (re)load launchd jobs --
echo "[3/3] Installing launchd schedules..."
render_plist() {
local name="$1"
local cadence="$2"
local src="$SCRIPT_DIR/templates/${name}.plist.template"
local label="com.${USERNAME}.${name}"
local dst="$LAUNCH_AGENTS/${label}.plist"
if [ ! -f "$src" ]; then
echo " ERR template missing: $src" >&2
return 1
fi
# Render with sed; use | as delimiter since paths contain /
sed \
-e "s|__HOME__|$HOME|g" \
-e "s|__USERNAME__|$USERNAME|g" \
-e "s|__SCRIPT_DIR__|$SCRIPT_DIR|g" \
-e "s|__LOG_DIR__|$LOG_DIR|g" \
"$src" > "$dst"
# Idempotent reload: bootout if present, then bootstrap.
launchctl bootout "gui/${UID_NUM}/${label}" 2>/dev/null || true
launchctl bootstrap "gui/${UID_NUM}" "$dst"
echo " ok $label ($cadence)"
}
render_plist "claude-backup" "rclone -> Google Drive, every 2h"
render_plist "claude-git-snapshot" "git snapshot -> backup/auto, every 2h"
echo
echo "Loaded jobs:"
launchctl list | grep "com\.${USERNAME}\.claude-" | sed 's/^/ /' || echo " (none — bootstrap may have failed)"
echo
echo "Done. To verify health: $SCRIPT_DIR/claude-backup.sh status"