A robust collection of shell scripts for automated Git-based backups with enhanced reliability, notifications, and monitoring.
- Modular Design: Shared library for common backup functions
- Multiple Backup Jobs: Configure unlimited backup jobs in one place
- Smart Notifications: macOS notifications, email, and webhook support (Slack, Discord)
- Retry Logic: Automatic retries with exponential backoff for network failures
- Log Rotation: Automatic log rotation (keeps last 10 logs, max 10MB each)
- Backup Verification: Verifies commits were successfully pushed to remote
- Health Check: Script to monitor backup job status and health
- Dry Run Mode: Test backups without making actual changes
- Conflict Detection: Better handling of merge conflicts
- Branch Safety: Automatic branch validation and switching to prevent data corruption
- Automated Daily Backups: LaunchAgent integration for macOS
- Restart Persistence: Backups automatically resume after system restart
- iCloud Compatibility: Works with iCloud Drive-synced directories
- Git Timeout Protection: Prevents hanging on slow operations
📚 New to this project? Start with these guides:
- SETUP_GUIDE.md - Complete setup instructions from scratch
- TESTING_CHECKLIST.md - Comprehensive testing guide (18 tests)
- README.md (this file) - Feature overview and quick reference
- Clone this repository
- Copy
config.template.shtoconfig.sh:cp config.template.sh config.sh
- Edit
config.shwith your paths and preferences - Run a backup:
./backup-to-git.sh obsidian
Your Intel MacBook Pro should have the following installed:
# Check if git is installed
git --version
# Should show: git version 2.x.x or higher
# Check bash version
bash --version
# macOS comes with bash 3.2, which works fine for these scripts
# Check if you have curl (for webhooks, optional)
curl --versionIf git is not installed, install it:
# Install via Homebrew (recommended)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install git
# Or install Xcode Command Line Tools
xcode-select --install# Clone to your home directory
cd ~
git clone <your-repo-url> backup-scripts
cd backup-scriptsEach directory you want to backup must be a git repository with a remote configured.
Example: Setting up Obsidian vault backup
# Navigate to your Obsidian vault
cd ~/Documents/ObsidianVault
# Initialize git if not already done
git init
# Create a .gitignore (optional, to exclude certain files)
cat > .gitignore << 'EOF'
.DS_Store
.obsidian/workspace*
.trash/
EOF
# Add remote repository (create this on GitHub/GitLab first)
git remote add origin https://github.com/yourusername/obsidian-vault.git
# Make initial commit
git add .
git commit -m "Initial commit"
git push -u origin mainRepeat this for each directory you want to backup (personal docs, projects, etc.).
# Navigate to backup-scripts directory
cd ~/backup-scripts
# Copy the template configuration
cp config.template.sh config.sh
# Edit configuration with your favorite editor
nano config.sh
# or
vim config.sh
# or
open -e config.sh # Opens in TextEditEdit config.sh with your settings:
#!/bin/bash
# Path to this backup scripts directory
export BACKUP_SCRIPTS_DIR="$HOME/backup-scripts"
# Define your backup jobs
declare -A BACKUP_JOBS
BACKUP_JOBS[obsidian]="$HOME/Documents/ObsidianVault"
BACKUP_JOBS[personal-docs]="$HOME/Documents/Personal"
# Add more as needed
# Git configuration
export GIT_USER_EMAIL="your-email@example.com"
export GIT_USER_NAME="Your Name"
# Enable macOS notifications
export ENABLE_MACOS_NOTIFICATIONS=true
# Optional: Email notifications (requires mail command)
export ENABLE_EMAIL_NOTIFICATIONS=false
export NOTIFICATION_EMAIL="your-email@example.com"
# Optional: Webhook notifications
export ENABLE_WEBHOOK_NOTIFICATIONS=false
export WEBHOOK_URL=""Before setting up automation, test each backup manually:
# Test in dry-run mode first (doesn't make changes)
DRY_RUN=true ./backup-to-git.sh obsidian
# If dry-run looks good, run actual backup
./backup-to-git.sh obsidian
# Check the logs
cat logs/obsidian_backup.log
# Run health check to verify everything is configured correctly
./health-check.shYou should see output like:
================================================
Starting backup: obsidian
Directory: /Users/yourusername/Documents/ObsidianVault
Branch: main
================================================
Already on branch 'main'
Pulling latest changes from origin/main
Successfully pulled latest changes
Changes committed successfully
Backup completed successfully
Using launchd for automatic backups:
# Copy the setup script template
cp setup-launchd.template.sh setup-launchd.sh
chmod +x setup-launchd.sh
# Edit the template if needed (usually not necessary)
# Then run the setup
./setup-launchd.shThis creates and loads launch agents that will:
- Run backups every day at midnight
- Run backups when your Mac boots/logs in (RunAtLoad=true)
- Run backups immediately after setup
- Persist across restarts - agents automatically reload on login
Verify launch agents are loaded and will survive restart:
# Check if agents are loaded
launchctl list | grep dlukianenko
# You should see entries like:
# - 0 com.dlukianenko.backup-obsidian
# - 0 com.dlukianenko.backup-personal-docs
# Check detailed agent status
launchctl print gui/$(id -u)/com.dlukianenko.backup-obsidian | grep -E "state|program|runatload"
# Check agent status and health
./health-check.sh
# View recent backup logs
tail -20 logs/obsidian_backup.logThe verification script checks:
- Launch agent files exist
- Agents are loaded in launchd
- RunAtLoad is enabled (ensures restart persistence)
- Backup scripts are executable
- Schedule configuration is correct
Test restart persistence:
# Option 1: Test without restarting
launchctl unload ~/Library/LaunchAgents/com.example.backup-obsidian.plist
launchctl load ~/Library/LaunchAgents/com.example.backup-obsidian.plist
# Check logs to verify it ran on load
# Option 2: Full restart test
# 1. Note current time: date
# 2. Restart your Mac
# 3. After login, run: ./verify-launchd.sh
# 4. Check logs: grep "$(date +%Y-%m-%d)" logs/obsidian_backup.logSee TESTING.md for comprehensive restart testing guide.
Logs are stored in the logs directory:
# View recent backup activity
tail -f logs/obsidian_backup.log
# View all logs
ls -lh logs/
# Check for errors
grep ERROR logs/*.log# Unload an agent (stop automatic backups)
launchctl unload ~/Library/LaunchAgents/com.example.backup-obsidian.plist
# Reload an agent (resume automatic backups)
launchctl load ~/Library/LaunchAgents/com.example.backup-obsidian.plist
# Trigger a backup immediately
launchctl start com.example.backup-obsidian"Permission denied" when running scripts:
chmod +x backup-to-git.sh backup-obsidian.sh backup-personal-docs.sh health-check.shLaunch agent not running:
# Check system logs
log show --predicate 'subsystem == "com.apple.launchd"' --last 1h | grep backup
# Verify plist file syntax
plutil -lint ~/Library/LaunchAgents/com.example.backup-obsidian.plist
# Reload the agent
launchctl unload ~/Library/LaunchAgents/com.example.backup-obsidian.plist
launchctl load ~/Library/LaunchAgents/com.example.backup-obsidian.plist"bash: local: can only be used in a function" error: This was a bug in earlier versions. Make sure you're using the latest version:
git pull origin mainNotifications not appearing:
- Check System Preferences → Notifications → Script Editor
- Make sure notifications are enabled
- Test with:
osascript -e 'display notification "Test" with title "Backup Test"'
Git authentication issues:
# Use SSH instead of HTTPS for easier authentication
git remote set-url origin git@github.com:yourusername/repo.git
# Or configure git credential helper for HTTPS
git config --global credential.helper osxkeychain-
Verify restart persistence after setup:
./verify-launchd.sh
-
Keep your Mac awake during backups - Consider using Amphetamine (free on App Store) or
caffeinatecommand -
Test with dry-run first:
DRY_RUN=true ./backup-to-git.sh obsidian
-
Run health checks regularly:
./health-check.sh
-
Monitor logs periodically:
grep ERROR logs/*.log -
Use SSH keys for git - Set up SSH keys to avoid password prompts:
ssh-keygen -t ed25519 -C "your-email@example.com" cat ~/.ssh/id_ed25519.pub # Add this to GitHub/GitLab SSH keys
# Run manual backup
./backup-to-git.sh obsidian
# Run backup to specific branch
./backup-to-git.sh my-project dev
# Test without making changes
DRY_RUN=true ./backup-to-git.sh obsidian
# Check all backup jobs health
./health-check.sh
# Verify launch agents and restart persistence
./verify-launchd.sh
# View logs
tail -f logs/obsidian_backup.log
# List launch agents
launchctl list | grep backup
# Trigger immediate backup via launch agent
launchctl start com.example.backup-obsidian
# Test restart behavior without restarting
launchctl unload ~/Library/LaunchAgents/com.example.backup-obsidian.plist
launchctl load ~/Library/LaunchAgents/com.example.backup-obsidian.plistDefine your backup jobs in config.sh using the BACKUP_JOBS associative array:
declare -A BACKUP_JOBS
BACKUP_JOBS[obsidian]="$HOME/Documents/ObsidianVault"
BACKUP_JOBS[personal-docs]="$HOME/Documents/Personal"
BACKUP_JOBS[my-project]="$HOME/Projects/MyProject"Enable notifications in config.sh:
# macOS Notifications
ENABLE_MACOS_NOTIFICATIONS=true
# Email Notifications
ENABLE_EMAIL_NOTIFICATIONS=true
NOTIFICATION_EMAIL="you@example.com"
# Webhook Notifications (Slack, Discord, etc.)
ENABLE_WEBHOOK_NOTIFICATIONS=true
WEBHOOK_URL="https://hooks.slack.com/services/YOUR/WEBHOOK/URL"# Verify backups were pushed successfully
VERIFY_BACKUP=true
# Test mode - don't make actual changes
DRY_RUN=false-
backup-to-git.sh <job_name> [branch]: Generic backup script for any job./backup-to-git.sh obsidian # Backup to main branch ./backup-to-git.sh my-project dev # Backup to dev branch
-
health-check.sh: Check health of all backup jobs./health-check.sh
backup-obsidian.sh: Backs up Obsidian vaultbackup-personal-docs.sh: Backs up personal documents
Set up automated backups using launchd:
-
Copy and edit the setup script:
cp setup-launchd.template.sh setup-launchd.sh chmod +x setup-launchd.sh
-
Run the setup script:
./setup-launchd.sh
The backups will run:
- Daily at midnight
- On system boot
- Immediately after setup
Run the health check to verify all backups are working:
./health-check.shThis checks:
- Directory existence
- Git repository status
- Remote configuration
- Uncommitted changes
- Sync status with remote
- Last commit time
- Log files and errors
- Launch agent status (macOS)
Logs are stored in the logs directory with automatic rotation:
- Maximum 10 rotated logs per job
- Maximum 10MB per log file
- Logs are excluded from Git
View logs:
tail -f logs/obsidian_backup.logTest backups without making changes:
DRY_RUN=true ./backup-to-git.sh obsidianBackup to a specific branch:
./backup-to-git.sh my-project feature-branchBranch Safety: The script automatically ensures you're on the correct branch before pulling/pushing:
- If already on the target branch, continues with backup
- If on a different branch with no uncommitted changes, automatically switches
- If the target branch doesn't exist locally, creates it from
origin/<branch> - If there are uncommitted changes, fails with a clear error message
This prevents accidentally merging or pushing to the wrong branch.
Example scenarios:
# Currently on 'main', no uncommitted changes
./backup-to-git.sh my-project dev
# → Switches to 'dev', then backs up
# Currently on 'main', with uncommitted changes
./backup-to-git.sh my-project dev
# → Fails with error: "Cannot switch branches - uncommitted changes detected"
# → You must commit or stash changes firstGit operations automatically retry up to 4 times with exponential backoff (2s, 4s, 8s, 16s) on network failures.
After pushing, the script verifies that local and remote are in sync. Disable with:
VERIFY_BACKUP=falseSymptom: Backup scripts hang indefinitely or fail with "Unable to create .git/index.lock: File exists"
Cause: iCloud Drive tries to sync the .git directory while Git operations are running, causing conflicts.
Solution: Exclude the .git directory from iCloud sync:
# For Obsidian vault in iCloud
xattr -w com.apple.fileprovider.ignore_sync 1 "$HOME/Library/Mobile Documents/iCloud~md~obsidian/Documents/MyBrain/.git"
# Verify the attribute is set
xattr "$HOME/Library/Mobile Documents/iCloud~md~obsidian/Documents/MyBrain/.git"
# Should show: com.apple.fileprovider.ignore_sync
# Clean up any existing lock files
rm -f "$HOME/Library/Mobile Documents/iCloud~md~obsidian/Documents/MyBrain/.git/index.lock"Note: This only excludes the .git directory from iCloud sync. Your actual files will still sync normally.
Symptom: declare: -A: invalid option
Cause: macOS ships with bash 3.2.57, but scripts require bash 4.0+ for associative arrays.
Solution: Install modern bash and update shebang:
# Install bash 5.x via Homebrew
brew install bash
# Verify installation
/usr/local/bin/bash --version # Intel Mac
/opt/homebrew/bin/bash --version # Apple Silicon
# Scripts in this repo are already configured to use /usr/local/bin/bash-
Check backup health:
./health-check.sh
-
View logs:
cat logs/obsidian_backup.log
-
Test in dry run mode:
DRY_RUN=true ./backup-to-git.sh obsidian
-
Check launch agent status (macOS):
launchctl list | grep backup
backup-scripts/
├── lib/
│ └── backup-functions.sh # Shared library with all core functions
├── logs/ # Auto-rotated log files
├── backup-to-git.sh # Generic backup script
├── backup-obsidian.sh # Legacy Obsidian backup
├── backup-personal-docs.sh # Legacy personal docs backup
├── health-check.sh # Health monitoring script
├── setup-launchd.sh # macOS automation setup
└── config.sh # Your configuration
- Git
- Bash 4.0+ (for associative arrays)
- macOS (for launchd automation and notifications)
- Optional:
mailcommand for email notifications - Optional:
curlfor webhook notifications