Repository files navigation

Backup Scripts

A robust collection of shell scripts for automated Git-based backups with enhanced reliability, notifications, and monitoring.

Features

  • 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

Documentation

📚 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

Quick Start

  1. Clone this repository
  2. Copy config.template.sh to config.sh:
    cp config.template.sh config.sh
  3. Edit config.sh with your paths and preferences
  4. Run a backup:
    ./backup-to-git.sh obsidian

macOS Setup Guide (Intel MacBook Pro)

Prerequisites

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 --version

If 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

Step-by-Step Setup

1. Clone the Repository

# Clone to your home directorycd~
git clone <your-repo-url> backup-scripts
cd backup-scripts

2. Prepare Your Git Repositories

Each directory you want to backup must be a git repository with a remote configured.

Example: Setting up Obsidian vault backup

# Navigate to your Obsidian vaultcd~/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 main

Repeat this for each directory you want to backup (personal docs, projects, etc.).

3. Configure the Backup Scripts

# Navigate to backup-scripts directorycd~/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 TextEdit

Edit config.sh with your settings:

#!/bin/bash# Path to this backup scripts directoryexport BACKUP_SCRIPTS_DIR="$HOME/backup-scripts"# Define your backup jobsdeclare -A BACKUP_JOBS
BACKUP_JOBS[obsidian]="$HOME/Documents/ObsidianVault"
BACKUP_JOBS[personal-docs]="$HOME/Documents/Personal"# Add more as needed# Git configurationexport GIT_USER_EMAIL="your-email@example.com"export GIT_USER_NAME="Your Name"# Enable macOS notificationsexport ENABLE_MACOS_NOTIFICATIONS=true
# Optional: Email notifications (requires mail command)export ENABLE_EMAIL_NOTIFICATIONS=false
export NOTIFICATION_EMAIL="your-email@example.com"# Optional: Webhook notificationsexport ENABLE_WEBHOOK_NOTIFICATIONS=false
export WEBHOOK_URL=""

4. Test Your Backups

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.sh

You 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

5. Set Up Automated Backups (Optional but Recommended)

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.sh

This 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.log

The 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.log

See TESTING.md for comprehensive restart testing guide.

6. View Launch Agent Logs

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

7. Manual Control of Launch Agents

# 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

Troubleshooting on macOS

"Permission denied" when running scripts:

chmod +x backup-to-git.sh backup-obsidian.sh backup-personal-docs.sh health-check.sh

Launch 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 main

Notifications 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

Best Practices for macOS

  1. Verify restart persistence after setup:

    ./verify-launchd.sh
  2. Keep your Mac awake during backups - Consider using Amphetamine (free on App Store) or caffeinate command

  3. Test with dry-run first:

    DRY_RUN=true ./backup-to-git.sh obsidian
  4. Run health checks regularly:

    ./health-check.sh
  5. Monitor logs periodically:

    grep ERROR logs/*.log
  6. 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

Quick Reference Commands

# 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.plist

Configuration

Backup Jobs

Define 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"

Notification Settings

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"

Backup Options

# Verify backups were pushed successfully
VERIFY_BACKUP=true
# Test mode - don't make actual changes
DRY_RUN=false

Scripts

Core Scripts

  • 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

Legacy Scripts (now using shared library)

  • backup-obsidian.sh: Backs up Obsidian vault
  • backup-personal-docs.sh: Backs up personal documents

Automated Backups (macOS)

Set up automated backups using launchd:

  1. Copy and edit the setup script:

    cp setup-launchd.template.sh setup-launchd.sh
    chmod +x setup-launchd.sh
  2. Run the setup script:

    ./setup-launchd.sh

The backups will run:

  • Daily at midnight
  • On system boot
  • Immediately after setup

Health Monitoring

Run the health check to verify all backups are working:

./health-check.sh

This 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

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.log

Advanced Features

Dry Run Mode

Test backups without making changes:

DRY_RUN=true ./backup-to-git.sh obsidian

Custom Branch

Backup to a specific branch:

./backup-to-git.sh my-project feature-branch

Branch 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 first

Retry Logic

Git operations automatically retry up to 4 times with exponential backoff (2s, 4s, 8s, 16s) on network failures.

Backup Verification

After pushing, the script verifies that local and remote are in sync. Disable with:

VERIFY_BACKUP=false

Troubleshooting

Common Issues

Git hangs or fails with "index.lock" errors in iCloud directories

Symptom: 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.

Bash version too old (associative array errors)

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

General Troubleshooting

  1. Check backup health:

    ./health-check.sh
  2. View logs:

    cat logs/obsidian_backup.log
  3. Test in dry run mode:

    DRY_RUN=true ./backup-to-git.sh obsidian
  4. Check launch agent status (macOS):

    launchctl list | grep backup

Architecture

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

Requirements

  • Git
  • Bash 4.0+ (for associative arrays)
  • macOS (for launchd automation and notifications)
  • Optional: mail command for email notifications
  • Optional: curl for webhook notifications

About

No description, website, or topics provided.

Resources

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

Repository files navigation

Backup Scripts

A robust collection of shell scripts for automated Git-based backups with enhanced reliability, notifications, and monitoring.

Features

  • 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

Documentation

📚 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

Quick Start

  1. Clone this repository
  2. Copy config.template.sh to config.sh:
    cp config.template.sh config.sh
  3. Edit config.sh with your paths and preferences
  4. Run a backup:
    ./backup-to-git.sh obsidian

macOS Setup Guide (Intel MacBook Pro)

Prerequisites

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 --version

If 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

Step-by-Step Setup

1. Clone the Repository

# Clone to your home directorycd~
git clone <your-repo-url> backup-scripts
cd backup-scripts

2. Prepare Your Git Repositories

Each directory you want to backup must be a git repository with a remote configured.

Example: Setting up Obsidian vault backup

# Navigate to your Obsidian vaultcd~/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 main

Repeat this for each directory you want to backup (personal docs, projects, etc.).

3. Configure the Backup Scripts

# Navigate to backup-scripts directorycd~/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 TextEdit

Edit config.sh with your settings:

#!/bin/bash# Path to this backup scripts directoryexport BACKUP_SCRIPTS_DIR="$HOME/backup-scripts"# Define your backup jobsdeclare -A BACKUP_JOBS
BACKUP_JOBS[obsidian]="$HOME/Documents/ObsidianVault"
BACKUP_JOBS[personal-docs]="$HOME/Documents/Personal"# Add more as needed# Git configurationexport GIT_USER_EMAIL="your-email@example.com"export GIT_USER_NAME="Your Name"# Enable macOS notificationsexport ENABLE_MACOS_NOTIFICATIONS=true
# Optional: Email notifications (requires mail command)export ENABLE_EMAIL_NOTIFICATIONS=false
export NOTIFICATION_EMAIL="your-email@example.com"# Optional: Webhook notificationsexport ENABLE_WEBHOOK_NOTIFICATIONS=false
export WEBHOOK_URL=""

4. Test Your Backups

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.sh

You 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

5. Set Up Automated Backups (Optional but Recommended)

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.sh

This 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.log

The 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.log

See TESTING.md for comprehensive restart testing guide.

6. View Launch Agent Logs

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

7. Manual Control of Launch Agents

# 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

Troubleshooting on macOS

"Permission denied" when running scripts:

chmod +x backup-to-git.sh backup-obsidian.sh backup-personal-docs.sh health-check.sh

Launch 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 main

Notifications 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

Best Practices for macOS

  1. Verify restart persistence after setup:

    ./verify-launchd.sh
  2. Keep your Mac awake during backups - Consider using Amphetamine (free on App Store) or caffeinate command

  3. Test with dry-run first:

    DRY_RUN=true ./backup-to-git.sh obsidian
  4. Run health checks regularly:

    ./health-check.sh
  5. Monitor logs periodically:

    grep ERROR logs/*.log
  6. 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

Quick Reference Commands

# 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.plist

Configuration

Backup Jobs

Define 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"

Notification Settings

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"

Backup Options

# Verify backups were pushed successfully
VERIFY_BACKUP=true
# Test mode - don't make actual changes
DRY_RUN=false

Scripts

Core Scripts

  • 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

Legacy Scripts (now using shared library)

  • backup-obsidian.sh: Backs up Obsidian vault
  • backup-personal-docs.sh: Backs up personal documents

Automated Backups (macOS)

Set up automated backups using launchd:

  1. Copy and edit the setup script:

    cp setup-launchd.template.sh setup-launchd.sh
    chmod +x setup-launchd.sh
  2. Run the setup script:

    ./setup-launchd.sh

The backups will run:

  • Daily at midnight
  • On system boot
  • Immediately after setup

Health Monitoring

Run the health check to verify all backups are working:

./health-check.sh

This 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

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.log

Advanced Features

Dry Run Mode

Test backups without making changes:

DRY_RUN=true ./backup-to-git.sh obsidian

Custom Branch

Backup to a specific branch:

./backup-to-git.sh my-project feature-branch

Branch 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 first

Retry Logic

Git operations automatically retry up to 4 times with exponential backoff (2s, 4s, 8s, 16s) on network failures.

Backup Verification

After pushing, the script verifies that local and remote are in sync. Disable with:

VERIFY_BACKUP=false

Troubleshooting

Common Issues

Git hangs or fails with "index.lock" errors in iCloud directories

Symptom: 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.

Bash version too old (associative array errors)

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

General Troubleshooting

  1. Check backup health:

    ./health-check.sh
  2. View logs:

    cat logs/obsidian_backup.log
  3. Test in dry run mode:

    DRY_RUN=true ./backup-to-git.sh obsidian
  4. Check launch agent status (macOS):

    launchctl list | grep backup

Architecture

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

Requirements

  • Git
  • Bash 4.0+ (for associative arrays)
  • macOS (for launchd automation and notifications)
  • Optional: mail command for email notifications
  • Optional: curl for webhook notifications

About

No description, website, or topics provided.

Resources

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

Backup Scripts

A robust collection of shell scripts for automated Git-based backups with enhanced reliability, notifications, and monitoring.

Features

  • 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

Documentation

📚 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

Quick Start

  1. Clone this repository
  2. Copy config.template.sh to config.sh:
    cp config.template.sh config.sh
  3. Edit config.sh with your paths and preferences
  4. Run a backup:
    ./backup-to-git.sh obsidian

macOS Setup Guide (Intel MacBook Pro)

Prerequisites

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 --version

If 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

Step-by-Step Setup

1. Clone the Repository

# Clone to your home directorycd~
git clone <your-repo-url> backup-scripts
cd backup-scripts

2. Prepare Your Git Repositories

Each directory you want to backup must be a git repository with a remote configured.

Example: Setting up Obsidian vault backup

# Navigate to your Obsidian vaultcd~/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 main

Repeat this for each directory you want to backup (personal docs, projects, etc.).

3. Configure the Backup Scripts

# Navigate to backup-scripts directorycd~/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 TextEdit

Edit config.sh with your settings:

#!/bin/bash# Path to this backup scripts directoryexport BACKUP_SCRIPTS_DIR="$HOME/backup-scripts"# Define your backup jobsdeclare -A BACKUP_JOBS
BACKUP_JOBS[obsidian]="$HOME/Documents/ObsidianVault"
BACKUP_JOBS[personal-docs]="$HOME/Documents/Personal"# Add more as needed# Git configurationexport GIT_USER_EMAIL="your-email@example.com"export GIT_USER_NAME="Your Name"# Enable macOS notificationsexport ENABLE_MACOS_NOTIFICATIONS=true
# Optional: Email notifications (requires mail command)export ENABLE_EMAIL_NOTIFICATIONS=false
export NOTIFICATION_EMAIL="your-email@example.com"# Optional: Webhook notificationsexport ENABLE_WEBHOOK_NOTIFICATIONS=false
export WEBHOOK_URL=""

4. Test Your Backups

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.sh

You 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

5. Set Up Automated Backups (Optional but Recommended)

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.sh

This 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.log

The 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.log

See TESTING.md for comprehensive restart testing guide.

6. View Launch Agent Logs

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

7. Manual Control of Launch Agents

# 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

Troubleshooting on macOS

"Permission denied" when running scripts:

chmod +x backup-to-git.sh backup-obsidian.sh backup-personal-docs.sh health-check.sh

Launch 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 main

Notifications 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

Best Practices for macOS

  1. Verify restart persistence after setup:

    ./verify-launchd.sh
  2. Keep your Mac awake during backups - Consider using Amphetamine (free on App Store) or caffeinate command

  3. Test with dry-run first:

    DRY_RUN=true ./backup-to-git.sh obsidian
  4. Run health checks regularly:

    ./health-check.sh
  5. Monitor logs periodically:

    grep ERROR logs/*.log
  6. 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

Quick Reference Commands

# 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.plist

Configuration

Backup Jobs

Define 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"

Notification Settings

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"

Backup Options

# Verify backups were pushed successfully
VERIFY_BACKUP=true
# Test mode - don't make actual changes
DRY_RUN=false

Scripts

Core Scripts

  • 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

Legacy Scripts (now using shared library)

  • backup-obsidian.sh: Backs up Obsidian vault
  • backup-personal-docs.sh: Backs up personal documents

Automated Backups (macOS)

Set up automated backups using launchd:

  1. Copy and edit the setup script:

    cp setup-launchd.template.sh setup-launchd.sh
    chmod +x setup-launchd.sh
  2. Run the setup script:

    ./setup-launchd.sh

The backups will run:

  • Daily at midnight
  • On system boot
  • Immediately after setup

Health Monitoring

Run the health check to verify all backups are working:

./health-check.sh

This 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

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.log

Advanced Features

Dry Run Mode

Test backups without making changes:

DRY_RUN=true ./backup-to-git.sh obsidian

Custom Branch

Backup to a specific branch:

./backup-to-git.sh my-project feature-branch

Branch 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 first

Retry Logic

Git operations automatically retry up to 4 times with exponential backoff (2s, 4s, 8s, 16s) on network failures.

Backup Verification

After pushing, the script verifies that local and remote are in sync. Disable with:

VERIFY_BACKUP=false

Troubleshooting

Common Issues

Git hangs or fails with "index.lock" errors in iCloud directories

Symptom: 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.

Bash version too old (associative array errors)

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

General Troubleshooting

  1. Check backup health:

    ./health-check.sh
  2. View logs:

    cat logs/obsidian_backup.log
  3. Test in dry run mode:

    DRY_RUN=true ./backup-to-git.sh obsidian
  4. Check launch agent status (macOS):

    launchctl list | grep backup

Architecture

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

Requirements

  • Git
  • Bash 4.0+ (for associative arrays)
  • macOS (for launchd automation and notifications)
  • Optional: mail command for email notifications
  • Optional: curl for webhook notifications

About

No description, website, or topics provided.

Resources

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

Backup Scripts

A robust collection of shell scripts for automated Git-based backups with enhanced reliability, notifications, and monitoring.

Features

  • 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

Documentation

📚 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

Quick Start

  1. Clone this repository
  2. Copy config.template.sh to config.sh:
    cp config.template.sh config.sh
  3. Edit config.sh with your paths and preferences
  4. Run a backup:
    ./backup-to-git.sh obsidian

macOS Setup Guide (Intel MacBook Pro)

Prerequisites

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 --version

If 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

Step-by-Step Setup

1. Clone the Repository

# Clone to your home directorycd~
git clone <your-repo-url> backup-scripts
cd backup-scripts

2. Prepare Your Git Repositories

Each directory you want to backup must be a git repository with a remote configured.

Example: Setting up Obsidian vault backup

# Navigate to your Obsidian vaultcd~/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 main

Repeat this for each directory you want to backup (personal docs, projects, etc.).

3. Configure the Backup Scripts

# Navigate to backup-scripts directorycd~/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 TextEdit

Edit config.sh with your settings:

#!/bin/bash# Path to this backup scripts directoryexport BACKUP_SCRIPTS_DIR="$HOME/backup-scripts"# Define your backup jobsdeclare -A BACKUP_JOBS
BACKUP_JOBS[obsidian]="$HOME/Documents/ObsidianVault"
BACKUP_JOBS[personal-docs]="$HOME/Documents/Personal"# Add more as needed# Git configurationexport GIT_USER_EMAIL="your-email@example.com"export GIT_USER_NAME="Your Name"# Enable macOS notificationsexport ENABLE_MACOS_NOTIFICATIONS=true
# Optional: Email notifications (requires mail command)export ENABLE_EMAIL_NOTIFICATIONS=false
export NOTIFICATION_EMAIL="your-email@example.com"# Optional: Webhook notificationsexport ENABLE_WEBHOOK_NOTIFICATIONS=false
export WEBHOOK_URL=""

4. Test Your Backups

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.sh

You 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

5. Set Up Automated Backups (Optional but Recommended)

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.sh

This 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.log

The 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.log

See TESTING.md for comprehensive restart testing guide.

6. View Launch Agent Logs

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

7. Manual Control of Launch Agents

# 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

Troubleshooting on macOS

"Permission denied" when running scripts:

chmod +x backup-to-git.sh backup-obsidian.sh backup-personal-docs.sh health-check.sh

Launch 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 main

Notifications 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

Best Practices for macOS

  1. Verify restart persistence after setup:

    ./verify-launchd.sh
  2. Keep your Mac awake during backups - Consider using Amphetamine (free on App Store) or caffeinate command

  3. Test with dry-run first:

    DRY_RUN=true ./backup-to-git.sh obsidian
  4. Run health checks regularly:

    ./health-check.sh
  5. Monitor logs periodically:

    grep ERROR logs/*.log
  6. 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

Quick Reference Commands

# 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.plist

Configuration

Backup Jobs

Define 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"

Notification Settings

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"

Backup Options

# Verify backups were pushed successfully
VERIFY_BACKUP=true
# Test mode - don't make actual changes
DRY_RUN=false

Scripts

Core Scripts

  • 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

Legacy Scripts (now using shared library)

  • backup-obsidian.sh: Backs up Obsidian vault
  • backup-personal-docs.sh: Backs up personal documents

Automated Backups (macOS)

Set up automated backups using launchd:

  1. Copy and edit the setup script:

    cp setup-launchd.template.sh setup-launchd.sh
    chmod +x setup-launchd.sh
  2. Run the setup script:

    ./setup-launchd.sh

The backups will run:

  • Daily at midnight
  • On system boot
  • Immediately after setup

Health Monitoring

Run the health check to verify all backups are working:

./health-check.sh

This 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

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.log

Advanced Features

Dry Run Mode

Test backups without making changes:

DRY_RUN=true ./backup-to-git.sh obsidian

Custom Branch

Backup to a specific branch:

./backup-to-git.sh my-project feature-branch

Branch 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 first

Retry Logic

Git operations automatically retry up to 4 times with exponential backoff (2s, 4s, 8s, 16s) on network failures.

Backup Verification

After pushing, the script verifies that local and remote are in sync. Disable with:

VERIFY_BACKUP=false

Troubleshooting

Common Issues

Git hangs or fails with "index.lock" errors in iCloud directories

Symptom: 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.

Bash version too old (associative array errors)

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

General Troubleshooting

  1. Check backup health:

    ./health-check.sh
  2. View logs:

    cat logs/obsidian_backup.log
  3. Test in dry run mode:

    DRY_RUN=true ./backup-to-git.sh obsidian
  4. Check launch agent status (macOS):

    launchctl list | grep backup

Architecture

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

Requirements

  • Git
  • Bash 4.0+ (for associative arrays)
  • macOS (for launchd automation and notifications)
  • Optional: mail command for email notifications
  • Optional: curl for webhook notifications

About

No description, website, or topics provided.

Resources

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

Repository files navigation

Backup Scripts

A robust collection of shell scripts for automated Git-based backups with enhanced reliability, notifications, and monitoring.

Features

  • 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

Documentation

📚 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

Quick Start

  1. Clone this repository
  2. Copy config.template.sh to config.sh:
    cp config.template.sh config.sh
  3. Edit config.sh with your paths and preferences
  4. Run a backup:
    ./backup-to-git.sh obsidian

macOS Setup Guide (Intel MacBook Pro)

Prerequisites

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 --version

If 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

Step-by-Step Setup

1. Clone the Repository

# Clone to your home directorycd~
git clone <your-repo-url> backup-scripts
cd backup-scripts

2. Prepare Your Git Repositories

Each directory you want to backup must be a git repository with a remote configured.

Example: Setting up Obsidian vault backup

# Navigate to your Obsidian vaultcd~/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 main

Repeat this for each directory you want to backup (personal docs, projects, etc.).

3. Configure the Backup Scripts

# Navigate to backup-scripts directorycd~/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 TextEdit

Edit config.sh with your settings:

#!/bin/bash# Path to this backup scripts directoryexport BACKUP_SCRIPTS_DIR="$HOME/backup-scripts"# Define your backup jobsdeclare -A BACKUP_JOBS
BACKUP_JOBS[obsidian]="$HOME/Documents/ObsidianVault"
BACKUP_JOBS[personal-docs]="$HOME/Documents/Personal"# Add more as needed# Git configurationexport GIT_USER_EMAIL="your-email@example.com"export GIT_USER_NAME="Your Name"# Enable macOS notificationsexport ENABLE_MACOS_NOTIFICATIONS=true
# Optional: Email notifications (requires mail command)export ENABLE_EMAIL_NOTIFICATIONS=false
export NOTIFICATION_EMAIL="your-email@example.com"# Optional: Webhook notificationsexport ENABLE_WEBHOOK_NOTIFICATIONS=false
export WEBHOOK_URL=""

4. Test Your Backups

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.sh

You 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

5. Set Up Automated Backups (Optional but Recommended)

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.sh

This 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.log

The 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.log

See TESTING.md for comprehensive restart testing guide.

6. View Launch Agent Logs

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

7. Manual Control of Launch Agents

# 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

Troubleshooting on macOS

"Permission denied" when running scripts:

chmod +x backup-to-git.sh backup-obsidian.sh backup-personal-docs.sh health-check.sh

Launch 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 main

Notifications 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

Best Practices for macOS

  1. Verify restart persistence after setup:

    ./verify-launchd.sh
  2. Keep your Mac awake during backups - Consider using Amphetamine (free on App Store) or caffeinate command

  3. Test with dry-run first:

    DRY_RUN=true ./backup-to-git.sh obsidian
  4. Run health checks regularly:

    ./health-check.sh
  5. Monitor logs periodically:

    grep ERROR logs/*.log
  6. 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

Quick Reference Commands

# 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.plist

Configuration

Backup Jobs

Define 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"

Notification Settings

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"

Backup Options

# Verify backups were pushed successfully
VERIFY_BACKUP=true
# Test mode - don't make actual changes
DRY_RUN=false

Scripts

Core Scripts

  • 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

Legacy Scripts (now using shared library)

  • backup-obsidian.sh: Backs up Obsidian vault
  • backup-personal-docs.sh: Backs up personal documents

Automated Backups (macOS)

Set up automated backups using launchd:

  1. Copy and edit the setup script:

    cp setup-launchd.template.sh setup-launchd.sh
    chmod +x setup-launchd.sh
  2. Run the setup script:

    ./setup-launchd.sh

The backups will run:

  • Daily at midnight
  • On system boot
  • Immediately after setup

Health Monitoring

Run the health check to verify all backups are working:

./health-check.sh

This 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

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.log

Advanced Features

Dry Run Mode

Test backups without making changes:

DRY_RUN=true ./backup-to-git.sh obsidian

Custom Branch

Backup to a specific branch:

./backup-to-git.sh my-project feature-branch

Branch 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 first

Retry Logic

Git operations automatically retry up to 4 times with exponential backoff (2s, 4s, 8s, 16s) on network failures.

Backup Verification

After pushing, the script verifies that local and remote are in sync. Disable with:

VERIFY_BACKUP=false

Troubleshooting

Common Issues

Git hangs or fails with "index.lock" errors in iCloud directories

Symptom: 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.

Bash version too old (associative array errors)

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

General Troubleshooting

  1. Check backup health:

    ./health-check.sh
  2. View logs:

    cat logs/obsidian_backup.log
  3. Test in dry run mode:

    DRY_RUN=true ./backup-to-git.sh obsidian
  4. Check launch agent status (macOS):

    launchctl list | grep backup

Architecture

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

Requirements

  • Git
  • Bash 4.0+ (for associative arrays)
  • macOS (for launchd automation and notifications)
  • Optional: mail command for email notifications
  • Optional: curl for webhook notifications

About

No description, website, or topics provided.

Resources

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

Backup Scripts

A robust collection of shell scripts for automated Git-based backups with enhanced reliability, notifications, and monitoring.

Features

  • 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

Documentation

📚 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

Quick Start

  1. Clone this repository
  2. Copy config.template.sh to config.sh:
    cp config.template.sh config.sh
  3. Edit config.sh with your paths and preferences
  4. Run a backup:
    ./backup-to-git.sh obsidian

macOS Setup Guide (Intel MacBook Pro)

Prerequisites

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 --version

If 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

Step-by-Step Setup

1. Clone the Repository

# Clone to your home directorycd~
git clone <your-repo-url> backup-scripts
cd backup-scripts

2. Prepare Your Git Repositories

Each directory you want to backup must be a git repository with a remote configured.

Example: Setting up Obsidian vault backup

# Navigate to your Obsidian vaultcd~/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 main

Repeat this for each directory you want to backup (personal docs, projects, etc.).

3. Configure the Backup Scripts

# Navigate to backup-scripts directorycd~/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 TextEdit

Edit config.sh with your settings:

#!/bin/bash# Path to this backup scripts directoryexport BACKUP_SCRIPTS_DIR="$HOME/backup-scripts"# Define your backup jobsdeclare -A BACKUP_JOBS
BACKUP_JOBS[obsidian]="$HOME/Documents/ObsidianVault"
BACKUP_JOBS[personal-docs]="$HOME/Documents/Personal"# Add more as needed# Git configurationexport GIT_USER_EMAIL="your-email@example.com"export GIT_USER_NAME="Your Name"# Enable macOS notificationsexport ENABLE_MACOS_NOTIFICATIONS=true
# Optional: Email notifications (requires mail command)export ENABLE_EMAIL_NOTIFICATIONS=false
export NOTIFICATION_EMAIL="your-email@example.com"# Optional: Webhook notificationsexport ENABLE_WEBHOOK_NOTIFICATIONS=false
export WEBHOOK_URL=""

4. Test Your Backups

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.sh

You 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

5. Set Up Automated Backups (Optional but Recommended)

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.sh

This 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.log

The 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.log

See TESTING.md for comprehensive restart testing guide.

6. View Launch Agent Logs

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

7. Manual Control of Launch Agents

# 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

Troubleshooting on macOS

"Permission denied" when running scripts:

chmod +x backup-to-git.sh backup-obsidian.sh backup-personal-docs.sh health-check.sh

Launch 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 main

Notifications 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

Best Practices for macOS

  1. Verify restart persistence after setup:

    ./verify-launchd.sh
  2. Keep your Mac awake during backups - Consider using Amphetamine (free on App Store) or caffeinate command

  3. Test with dry-run first:

    DRY_RUN=true ./backup-to-git.sh obsidian
  4. Run health checks regularly:

    ./health-check.sh
  5. Monitor logs periodically:

    grep ERROR logs/*.log
  6. 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

Quick Reference Commands

# 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.plist

Configuration

Backup Jobs

Define 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"

Notification Settings

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"

Backup Options

# Verify backups were pushed successfully
VERIFY_BACKUP=true
# Test mode - don't make actual changes
DRY_RUN=false

Scripts

Core Scripts

  • 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

Legacy Scripts (now using shared library)

  • backup-obsidian.sh: Backs up Obsidian vault
  • backup-personal-docs.sh: Backs up personal documents

Automated Backups (macOS)

Set up automated backups using launchd:

  1. Copy and edit the setup script:

    cp setup-launchd.template.sh setup-launchd.sh
    chmod +x setup-launchd.sh
  2. Run the setup script:

    ./setup-launchd.sh

The backups will run:

  • Daily at midnight
  • On system boot
  • Immediately after setup

Health Monitoring

Run the health check to verify all backups are working:

./health-check.sh

This 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

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.log

Advanced Features

Dry Run Mode

Test backups without making changes:

DRY_RUN=true ./backup-to-git.sh obsidian

Custom Branch

Backup to a specific branch:

./backup-to-git.sh my-project feature-branch

Branch 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 first

Retry Logic

Git operations automatically retry up to 4 times with exponential backoff (2s, 4s, 8s, 16s) on network failures.

Backup Verification

After pushing, the script verifies that local and remote are in sync. Disable with:

VERIFY_BACKUP=false

Troubleshooting

Common Issues

Git hangs or fails with "index.lock" errors in iCloud directories

Symptom: 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.

Bash version too old (associative array errors)

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

General Troubleshooting

  1. Check backup health:

    ./health-check.sh
  2. View logs:

    cat logs/obsidian_backup.log
  3. Test in dry run mode:

    DRY_RUN=true ./backup-to-git.sh obsidian
  4. Check launch agent status (macOS):

    launchctl list | grep backup

Architecture

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

Requirements

  • Git
  • Bash 4.0+ (for associative arrays)
  • macOS (for launchd automation and notifications)
  • Optional: mail command for email notifications
  • Optional: curl for webhook notifications

About

No description, website, or topics provided.

Resources

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

Repository files navigation

Backup Scripts

A robust collection of shell scripts for automated Git-based backups with enhanced reliability, notifications, and monitoring.

Features

  • 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

Documentation

📚 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

Quick Start

  1. Clone this repository
  2. Copy config.template.sh to config.sh:
    cp config.template.sh config.sh
  3. Edit config.sh with your paths and preferences
  4. Run a backup:
    ./backup-to-git.sh obsidian

macOS Setup Guide (Intel MacBook Pro)

Prerequisites

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 --version

If 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

Step-by-Step Setup

1. Clone the Repository

# Clone to your home directorycd~
git clone <your-repo-url> backup-scripts
cd backup-scripts

2. Prepare Your Git Repositories

Each directory you want to backup must be a git repository with a remote configured.

Example: Setting up Obsidian vault backup

# Navigate to your Obsidian vaultcd~/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 main

Repeat this for each directory you want to backup (personal docs, projects, etc.).

3. Configure the Backup Scripts

# Navigate to backup-scripts directorycd~/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 TextEdit

Edit config.sh with your settings:

#!/bin/bash# Path to this backup scripts directoryexport BACKUP_SCRIPTS_DIR="$HOME/backup-scripts"# Define your backup jobsdeclare -A BACKUP_JOBS
BACKUP_JOBS[obsidian]="$HOME/Documents/ObsidianVault"
BACKUP_JOBS[personal-docs]="$HOME/Documents/Personal"# Add more as needed# Git configurationexport GIT_USER_EMAIL="your-email@example.com"export GIT_USER_NAME="Your Name"# Enable macOS notificationsexport ENABLE_MACOS_NOTIFICATIONS=true
# Optional: Email notifications (requires mail command)export ENABLE_EMAIL_NOTIFICATIONS=false
export NOTIFICATION_EMAIL="your-email@example.com"# Optional: Webhook notificationsexport ENABLE_WEBHOOK_NOTIFICATIONS=false
export WEBHOOK_URL=""

4. Test Your Backups

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.sh

You 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

5. Set Up Automated Backups (Optional but Recommended)

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.sh

This 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.log

The 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.log

See TESTING.md for comprehensive restart testing guide.

6. View Launch Agent Logs

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

7. Manual Control of Launch Agents

# 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

Troubleshooting on macOS

"Permission denied" when running scripts:

chmod +x backup-to-git.sh backup-obsidian.sh backup-personal-docs.sh health-check.sh

Launch 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 main

Notifications 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

Best Practices for macOS

  1. Verify restart persistence after setup:

    ./verify-launchd.sh
  2. Keep your Mac awake during backups - Consider using Amphetamine (free on App Store) or caffeinate command

  3. Test with dry-run first:

    DRY_RUN=true ./backup-to-git.sh obsidian
  4. Run health checks regularly:

    ./health-check.sh
  5. Monitor logs periodically:

    grep ERROR logs/*.log
  6. 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

Quick Reference Commands

# 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.plist

Configuration

Backup Jobs

Define 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"

Notification Settings

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"

Backup Options

# Verify backups were pushed successfully
VERIFY_BACKUP=true
# Test mode - don't make actual changes
DRY_RUN=false

Scripts

Core Scripts

  • 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

Legacy Scripts (now using shared library)

  • backup-obsidian.sh: Backs up Obsidian vault
  • backup-personal-docs.sh: Backs up personal documents

Automated Backups (macOS)

Set up automated backups using launchd:

  1. Copy and edit the setup script:

    cp setup-launchd.template.sh setup-launchd.sh
    chmod +x setup-launchd.sh
  2. Run the setup script:

    ./setup-launchd.sh

The backups will run:

  • Daily at midnight
  • On system boot
  • Immediately after setup

Health Monitoring

Run the health check to verify all backups are working:

./health-check.sh

This 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

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.log

Advanced Features

Dry Run Mode

Test backups without making changes:

DRY_RUN=true ./backup-to-git.sh obsidian

Custom Branch

Backup to a specific branch:

./backup-to-git.sh my-project feature-branch

Branch 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 first

Retry Logic

Git operations automatically retry up to 4 times with exponential backoff (2s, 4s, 8s, 16s) on network failures.

Backup Verification

After pushing, the script verifies that local and remote are in sync. Disable with:

VERIFY_BACKUP=false

Troubleshooting

Common Issues

Git hangs or fails with "index.lock" errors in iCloud directories

Symptom: 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.

Bash version too old (associative array errors)

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

General Troubleshooting

  1. Check backup health:

    ./health-check.sh
  2. View logs:

    cat logs/obsidian_backup.log
  3. Test in dry run mode:

    DRY_RUN=true ./backup-to-git.sh obsidian
  4. Check launch agent status (macOS):

    launchctl list | grep backup

Architecture

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

Requirements

  • Git
  • Bash 4.0+ (for associative arrays)
  • macOS (for launchd automation and notifications)
  • Optional: mail command for email notifications
  • Optional: curl for webhook notifications

About

No description, website, or topics provided.

Resources

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

Repository files navigation

Backup Scripts

A robust collection of shell scripts for automated Git-based backups with enhanced reliability, notifications, and monitoring.

Features

  • 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

Documentation

📚 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

Quick Start

  1. Clone this repository
  2. Copy config.template.sh to config.sh:
    cp config.template.sh config.sh
  3. Edit config.sh with your paths and preferences
  4. Run a backup:
    ./backup-to-git.sh obsidian

macOS Setup Guide (Intel MacBook Pro)

Prerequisites

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 --version

If 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

Step-by-Step Setup

1. Clone the Repository

# Clone to your home directorycd~
git clone <your-repo-url> backup-scripts
cd backup-scripts

2. Prepare Your Git Repositories

Each directory you want to backup must be a git repository with a remote configured.

Example: Setting up Obsidian vault backup

# Navigate to your Obsidian vaultcd~/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 main

Repeat this for each directory you want to backup (personal docs, projects, etc.).

3. Configure the Backup Scripts

# Navigate to backup-scripts directorycd~/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 TextEdit

Edit config.sh with your settings:

#!/bin/bash# Path to this backup scripts directoryexport BACKUP_SCRIPTS_DIR="$HOME/backup-scripts"# Define your backup jobsdeclare -A BACKUP_JOBS
BACKUP_JOBS[obsidian]="$HOME/Documents/ObsidianVault"
BACKUP_JOBS[personal-docs]="$HOME/Documents/Personal"# Add more as needed# Git configurationexport GIT_USER_EMAIL="your-email@example.com"export GIT_USER_NAME="Your Name"# Enable macOS notificationsexport ENABLE_MACOS_NOTIFICATIONS=true
# Optional: Email notifications (requires mail command)export ENABLE_EMAIL_NOTIFICATIONS=false
export NOTIFICATION_EMAIL="your-email@example.com"# Optional: Webhook notificationsexport ENABLE_WEBHOOK_NOTIFICATIONS=false
export WEBHOOK_URL=""

4. Test Your Backups

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.sh

You 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

5. Set Up Automated Backups (Optional but Recommended)

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.sh

This 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.log

The 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.log

See TESTING.md for comprehensive restart testing guide.

6. View Launch Agent Logs

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

7. Manual Control of Launch Agents

# 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

Troubleshooting on macOS

"Permission denied" when running scripts:

chmod +x backup-to-git.sh backup-obsidian.sh backup-personal-docs.sh health-check.sh

Launch 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 main

Notifications 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

Best Practices for macOS

  1. Verify restart persistence after setup:

    ./verify-launchd.sh
  2. Keep your Mac awake during backups - Consider using Amphetamine (free on App Store) or caffeinate command

  3. Test with dry-run first:

    DRY_RUN=true ./backup-to-git.sh obsidian
  4. Run health checks regularly:

    ./health-check.sh
  5. Monitor logs periodically:

    grep ERROR logs/*.log
  6. 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

Quick Reference Commands

# 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.plist

Configuration

Backup Jobs

Define 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"

Notification Settings

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"

Backup Options

# Verify backups were pushed successfully
VERIFY_BACKUP=true
# Test mode - don't make actual changes
DRY_RUN=false

Scripts

Core Scripts

  • 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

Legacy Scripts (now using shared library)

  • backup-obsidian.sh: Backs up Obsidian vault
  • backup-personal-docs.sh: Backs up personal documents

Automated Backups (macOS)

Set up automated backups using launchd:

  1. Copy and edit the setup script:

    cp setup-launchd.template.sh setup-launchd.sh
    chmod +x setup-launchd.sh
  2. Run the setup script:

    ./setup-launchd.sh

The backups will run:

  • Daily at midnight
  • On system boot
  • Immediately after setup

Health Monitoring

Run the health check to verify all backups are working:

./health-check.sh

This 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

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.log

Advanced Features

Dry Run Mode

Test backups without making changes:

DRY_RUN=true ./backup-to-git.sh obsidian

Custom Branch

Backup to a specific branch:

./backup-to-git.sh my-project feature-branch

Branch 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 first

Retry Logic

Git operations automatically retry up to 4 times with exponential backoff (2s, 4s, 8s, 16s) on network failures.

Backup Verification

After pushing, the script verifies that local and remote are in sync. Disable with:

VERIFY_BACKUP=false

Troubleshooting

Common Issues

Git hangs or fails with "index.lock" errors in iCloud directories

Symptom: 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.

Bash version too old (associative array errors)

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

General Troubleshooting

  1. Check backup health:

    ./health-check.sh
  2. View logs:

    cat logs/obsidian_backup.log
  3. Test in dry run mode:

    DRY_RUN=true ./backup-to-git.sh obsidian
  4. Check launch agent status (macOS):

    launchctl list | grep backup

Architecture

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

Requirements

  • Git
  • Bash 4.0+ (for associative arrays)
  • macOS (for launchd automation and notifications)
  • Optional: mail command for email notifications
  • Optional: curl for webhook notifications

About

No description, website, or topics provided.

Resources

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages