#!/usr/bin/env bash set -euo pipefail ######################################## # CONFIG — EDIT THESE 4 LINES ######################################## VAULT_PATH="/Users/davidglidden/Library/Mobile Documents/iCloud~md~obsidian/Documents/David, root-and-branch" MIRROR_PATH="/Users/davidglidden/_Dev/david-root-and-branch-vault-git" GIT_REMOTE="https://git.skemantix.com/David/david-root-and-branch-vault-git.git" GIT_BRANCH="main" # Optional: set your git identity here (comment out if already global) # GIT_NAME="David Glidden" # GIT_EMAIL="you@example.com" # Excludes file lives alongside this script EXCLUDES_FILE="$(dirname "$0")/obsidian_rsync_excludes.txt" LOG_FILE="${HOME}/Library/Logs/vaultsync.log" ######################################## timestamp() { date "+%Y-%m-%d %H:%M:%S"; } log() { printf "[%s] %s\n" "$(timestamp)" "$*" | tee -a "$LOG_FILE"; } ensure_paths() { mkdir -p "$(dirname "$LOG_FILE")" mkdir -p "$MIRROR_PATH" } # Guard: skip if iCloud still downloading placeholders (*.icloud) icloud_busy() { # Any placeholder files inside vault? # NOTE: adjust -prune if you want to skip very large folders from scanning if find "$VAULT_PATH" -type f -name "*.icloud" -print -quit | grep -q . ; then return 0 else return 1 fi } init_git_repo() { if [ ! -d "$MIRROR_PATH/.git" ]; then log "Initializing git repo in $MIRROR_PATH" git init "$MIRROR_PATH" git -C "$MIRROR_PATH" checkout -b "$GIT_BRANCH" || true git -C "$MIRROR_PATH" remote add origin "$GIT_REMOTE" || true git -C "$MIRROR_PATH" config user.name "$GIT_NAME" || true git -C "$MIRROR_PATH" config user.email "$GIT_EMAIL" || true fi } do_rsync() { log "Syncing from iCloud vault → mirror" rsync -av --delete \ --exclude-from="$EXCLUDES_FILE" \ "$VAULT_PATH"/ "$MIRROR_PATH"/ | tee -a "$LOG_FILE" } do_commit_push() { log "Checking for changes to commit" if ! git -C "$MIRROR_PATH" diff --quiet || \ ! git -C "$MIRROR_PATH" diff --cached --quiet || \ [ -n "$(git -C "$MIRROR_PATH" ls-files --others --exclude-standard)" ]; then git -C "$MIRROR_PATH" add -A git -C "$MIRROR_PATH" commit -m "Mirror sync $(date +%Y-%m-%d_%H-%M-%S)" --no-verify log "Committed changes" # Push; if first push, set upstream if git -C "$MIRROR_PATH" rev-parse --verify "$GIT_BRANCH" >/dev/null 2>&1; then git -C "$MIRROR_PATH" push origin "$GIT_BRANCH" || log "Push failed (network?). Will retry next run." else git -C "$MIRROR_PATH" push -u origin "$GIT_BRANCH" || log "First push failed. Check remote." fi else log "No changes to commit" fi } main() { ensure_paths if icloud_busy; then log "iCloud still downloading placeholders (*.icloud). Skipping this run." exit 0 fi init_git_repo do_rsync do_commit_push log "Done." } main "$@"