-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.sh
More file actions
executable file
·247 lines (220 loc) · 7.81 KB
/
sync.sh
File metadata and controls
executable file
·247 lines (220 loc) · 7.81 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
#
# dotfiles-sync: Auto-sync dotfiles, app configs, and auxiliary repos
# This script is designed to be run manually or via LaunchAgent
#
# Syncs:
# 1. ~/.dotfiles (this repo)
# 2. All git repos in ~/.sync/ (mackup, themes, etc.)
# 3. Brewfile (auto-dumps current packages)
# 4. App configs via mackup (copy mode)
#
set -euo pipefail
DOTFILES_DIR="${DOTFILES_DIR:-$HOME/.dotfiles}"
SYNC_DIR="${SYNC_DIR:-$HOME/.sync}"
LOG_DIR="${DOTFILES_DIR}/logs"
LOG_FILE="${LOG_DIR}/sync.log"
MAX_LOG_SIZE=1048576 # 1MB
# Ensure directories exist
mkdir -p "$LOG_DIR" "$SYNC_DIR"
# Rotate log if too large
if [[ -f "$LOG_FILE" ]] && [[ $(stat -f%z "$LOG_FILE" 2>/dev/null || stat --printf="%s" "$LOG_FILE" 2>/dev/null) -gt $MAX_LOG_SIZE ]]; then
mv "$LOG_FILE" "${LOG_FILE}.old"
fi
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE"
}
# Check if we're online
check_connectivity() {
if ! ping -c 1 -W 2 github.com &>/dev/null; then
log "WARN: No network connectivity, skipping sync"
return 1
fi
return 0
}
#=============================================================================
# BREWFILE SYNC
#=============================================================================
dump_brewfile() {
if command -v brew &>/dev/null; then
log "INFO: Dumping Brewfile..."
local brewfile="${DOTFILES_DIR}/Brewfile"
if brew bundle dump --force --file="$brewfile" 2>&1 | tee -a "$LOG_FILE"; then
log "INFO: Brewfile updated"
else
log "WARN: Brewfile dump failed"
fi
else
log "INFO: brew not installed, skipping Brewfile dump"
fi
}
#=============================================================================
# MACKUP BACKUP (copy mode)
#=============================================================================
run_mackup_backup() {
if command -v mackup &>/dev/null; then
log "INFO: Running mackup backup (copy mode)..."
if mackup backup --force 2>&1 | tee -a "$LOG_FILE"; then
log "INFO: Mackup backup complete"
else
log "WARN: Mackup backup failed"
fi
else
log "INFO: mackup not installed, skipping app config backup"
fi
}
#=============================================================================
# GIT REPO SYNC (generic)
#=============================================================================
sync_git_repo() {
local repo_dir="$1"
local repo_name
repo_name=$(basename "$repo_dir")
cd "$repo_dir" || {
log "ERROR: Cannot cd to $repo_dir"
return 1
}
# Ensure we're in a git repo
if [[ ! -d ".git" ]]; then
log "WARN: $repo_dir is not a git repository, skipping"
return 0
fi
log "INFO: Syncing $repo_name..."
# Check if remote exists
if ! git remote get-url origin &>/dev/null; then
log "INFO: $repo_name has no remote, committing locally only"
# Just commit local changes
if ! git diff-index --quiet HEAD -- 2>/dev/null || [[ -n "$(git ls-files --others --exclude-standard)" ]]; then
git add -A
git commit -m "auto: sync $(date '+%Y-%m-%d %H:%M') from $(hostname -s)" 2>&1 | tee -a "$LOG_FILE" || true
fi
return 0
fi
# Fetch latest changes
git fetch origin 2>&1 | tee -a "$LOG_FILE" || log "WARN: Failed to fetch $repo_name"
# Check for local changes (staged, unstaged, and untracked)
if ! git diff-index --quiet HEAD -- 2>/dev/null || [[ -n "$(git ls-files --others --exclude-standard)" ]]; then
git add -A
local commit_msg="auto: sync $(date '+%Y-%m-%d %H:%M') from $(hostname -s)"
git commit -m "$commit_msg" 2>&1 | tee -a "$LOG_FILE" || true
log "INFO: Committed local changes in $repo_name"
fi
# Get current branch
local branch
branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "main")
# Pull with rebase (stash if needed)
local stashed=false
if ! git diff-index --quiet HEAD -- 2>/dev/null; then
git stash push -m "auto-stash for sync" 2>&1 | tee -a "$LOG_FILE"
stashed=true
fi
if git pull --rebase origin "$branch" 2>&1 | tee -a "$LOG_FILE"; then
log "INFO: Pulled latest changes for $repo_name"
else
log "WARN: Pull failed for $repo_name, aborting rebase"
git rebase --abort 2>/dev/null || true
fi
if [[ "$stashed" == "true" ]]; then
git stash pop 2>&1 | tee -a "$LOG_FILE" || log "WARN: Failed to pop stash"
fi
# Push changes
if git push origin "$branch" 2>&1 | tee -a "$LOG_FILE"; then
log "INFO: Pushed $repo_name to origin"
else
log "WARN: Push failed for $repo_name, will retry next sync"
fi
}
#=============================================================================
# SYNC ALL REPOS IN ~/.sync/
#=============================================================================
sync_auxiliary_repos() {
log "INFO: Scanning $SYNC_DIR for git repos..."
for dir in "$SYNC_DIR"/*/; do
[[ -d "$dir" ]] || continue
if [[ -d "${dir}.git" ]]; then
sync_git_repo "$dir"
fi
done
}
#=============================================================================
# SYNC DOTFILES REPO
#=============================================================================
sync_dotfiles() {
sync_git_repo "$DOTFILES_DIR"
}
#=============================================================================
# STOW SYMLINKS
#=============================================================================
run_stow() {
if command -v stow &>/dev/null; then
log "INFO: Running stow to update symlinks..."
cd "$DOTFILES_DIR/stow" || return
for package in */; do
package="${package%/}"
if [[ -d "$package" ]]; then
stow -v -R -t "$HOME" "$package" 2>&1 | tee -a "$LOG_FILE" || {
log "WARN: stow failed for package $package"
}
fi
done
else
log "INFO: stow not installed, skipping symlink update"
fi
}
#=============================================================================
# MAIN
#=============================================================================
main() {
case "${1:-sync}" in
sync)
check_connectivity || exit 0
log "========== Starting full sync =========="
dump_brewfile
run_mackup_backup
sync_dotfiles
sync_auxiliary_repos
log "========== Sync complete =========="
;;
dotfiles)
check_connectivity || exit 0
sync_dotfiles
;;
repos)
check_connectivity || exit 0
sync_auxiliary_repos
;;
stow)
run_stow
;;
brewfile)
dump_brewfile
;;
mackup)
run_mackup_backup
;;
all)
check_connectivity || exit 0
log "========== Starting full sync + stow =========="
dump_brewfile
run_mackup_backup
sync_dotfiles
sync_auxiliary_repos
run_stow
log "========== Sync complete =========="
;;
*)
echo "Usage: $0 {sync|dotfiles|repos|stow|brewfile|mackup|all}"
echo ""
echo "Commands:"
echo " sync - Full sync: brewfile + mackup + dotfiles + ~/.sync repos"
echo " dotfiles - Sync only ~/.dotfiles"
echo " repos - Sync only repos in ~/.sync/"
echo " stow - Run stow to update symlinks"
echo " brewfile - Dump current brew packages to Brewfile"
echo " mackup - Run mackup backup"
echo " all - Full sync + stow"
exit 1
;;
esac
}
main "$@"