From 0a9fa669f02e79c58b38d44765c510a908c5acca Mon Sep 17 00:00:00 2001 From: Yan Lin Date: Mon, 28 Jul 2025 17:26:39 +0200 Subject: [PATCH] Add comprehensive rsync configuration and management MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add rsync package to home.packages for Nix-managed installation - Create modules/rsync.nix with comprehensive configuration: * ~/.rsync-exclude with macOS and development exclusion patterns * ~/.rsync-backup.conf with standard backup options and safety features * ~/.local/bin/rsync-backup executable wrapper script for easy backups * ~/.rsync-aliases with convenient shell aliases for common operations - Add detailed rsync documentation to README with usage examples - Switch from macOS built-in rsync to declarative Nix management Features: - Comprehensive exclude patterns for macOS metadata and temp files - Progress indication, compression, and network optimization - Safety options including dry-run capability and partial transfers - Extended attributes and ACL preservation for macOS - Multiple usage patterns: wrapper script, aliases, and manual configuration 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- README.md | 47 +++++++++++++++++++ flake.nix | 2 + modules/rsync.nix | 117 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 166 insertions(+) create mode 100644 modules/rsync.nix diff --git a/README.md b/README.md index 67a5022..7feedd0 100644 --- a/README.md +++ b/README.md @@ -297,6 +297,52 @@ Launch with `lazygit` in any git repository for: - File tree navigation with git status - Intuitive keyboard shortcuts and help system +### 🌟 File Synchronization & Backup + +**Tool**: rsync +**Purpose**: Declarative file synchronization and backup management + +Configured with comprehensive exclude patterns and backup presets: + +#### Configuration Files: +- `~/.rsync-exclude` - Common exclude patterns (macOS metadata, temp files) +- `~/.rsync-backup.conf` - Standard backup options with safety features +- `~/.local/bin/rsync-backup` - Convenient backup wrapper script +- `~/.rsync-aliases` - Shell aliases for common operations + +#### Usage Examples: + +**Using the backup wrapper:** +```bash +# Quick backup with progress and safety +rsync-backup ~/Documents/ /backup/documents/ + +# The script automatically applies exclude patterns and safety options +``` + +**Using shell aliases (source ~/.rsync-aliases first):** +```bash +rsync-quick source/ dest/ # Basic backup with progress +rsync-dry source/ dest/ # Dry run for testing (safe) +rsync-sync source/ dest/ # Sync without deleting files +rsync-mirror source/ dest/ # Mirror with delete (exact copy) +``` + +**Manual rsync with config:** +```bash +# Use the backup configuration file +rsync $(cat ~/.rsync-backup.conf | grep -v '^#' | tr '\n' ' ') source/ dest/ + +# Or with custom exclude patterns +rsync -avh --progress --exclude-from=~/.rsync-exclude source/ dest/ +``` + +**Features:** +- Automatic exclusion of temporary files and macOS metadata +- Progress indication and compression for network transfers +- Safety options including partial transfers and dry-run capability +- Preserves extended attributes and ACLs on macOS + ## 📦 Included Packages ### Development Tools @@ -316,6 +362,7 @@ Launch with `lazygit` in any git repository for: - **sqlite3**: Official SQLite command-line interface - **lftp**: Scriptable FTP client for automation - **termscp**: Comprehensive TUI file transfer client (FTP/SFTP/SCP/S3) +- **rsync**: Fast file synchronization and backup with comprehensive configuration - **zoxide**: Smart cd with frecency algorithm #### fd Usage Examples diff --git a/flake.nix b/flake.nix index fe690fa..a0a83c6 100644 --- a/flake.nix +++ b/flake.nix @@ -46,6 +46,7 @@ ./modules/ssh.nix ./modules/git.nix ./modules/termscp.nix + ./modules/rsync.nix ]; home.username = "yanlin"; @@ -69,6 +70,7 @@ lazygit ncdu git-credential-oauth + rsync ]; fonts.fontconfig.enable = true; diff --git a/modules/rsync.nix b/modules/rsync.nix new file mode 100644 index 0000000..5f99c12 --- /dev/null +++ b/modules/rsync.nix @@ -0,0 +1,117 @@ +{ config, pkgs, ... }: + +{ + # Rsync exclude patterns for common files and directories + home.file.".rsync-exclude".text = '' + # macOS specific + .DS_Store + .AppleDouble + .LSOverride + Icon? + ._* + .DocumentRevisions-V100 + .fseventsd + .Spotlight-V100 + .TemporaryItems + .Trashes + .VolumeIcon.icns + .com.apple.timemachine.donotpresent + + # OS generated files + Thumbs.db + ehthumbs.db + Desktop.ini + $RECYCLE.BIN/ + ''; + + # Rsync configuration for common backup scenarios + home.file.".rsync-backup.conf".text = '' + # Common rsync options for backups + # Usage: rsync @backup-options source/ destination/ + + # Standard backup options + --archive + --verbose + --progress + --human-readable + --exclude-from=~/.rsync-exclude + --delete + --delete-excluded + --partial + --partial-dir=.rsync-partial + + # Preserve extended attributes and ACLs (macOS) + --extended-attributes + --acls + + # Network optimization + --compress + --compress-level=6 + + # Safety options + --dry-run # Remove this line when you're ready to run for real + ''; + + # Create a convenient rsync wrapper script + home.file.".local/bin/rsync-backup".text = '' + #!/bin/bash + # + # Convenient rsync backup wrapper + # Usage: rsync-backup source/ destination/ + # + + if [ $# -ne 2 ]; then + echo "Usage: $0 " + echo "Example: $0 ~/Documents/ /backup/documents/" + exit 1 + fi + + SOURCE="$1" + DEST="$2" + + # Ensure source ends with slash for proper rsync behavior + if [[ "$SOURCE" != */ ]]; then + SOURCE="$SOURCE/" + fi + + echo "=== Rsync Backup ===" + echo "Source: $SOURCE" + echo "Destination: $DEST" + echo "===================" + + # Use the configuration file options + rsync $(cat ~/.rsync-backup.conf | grep -v '^#' | grep -v '^$' | tr '\n' ' ') "$SOURCE" "$DEST" + + if [ $? -eq 0 ]; then + echo "Backup completed successfully!" + else + echo "Backup failed with exit code $?" + exit 1 + fi + ''; + + # Make the backup script executable + home.file.".local/bin/rsync-backup".executable = true; + + # Optional: Add rsync aliases to shell configuration + # This can be integrated with your existing zsh module + home.file.".rsync-aliases".text = '' + # Rsync aliases for common operations + # Source this file in your shell configuration + + # Quick backup with progress + alias rsync-quick='rsync -avh --progress --exclude-from=~/.rsync-exclude' + + # Dry run backup (safe testing) + alias rsync-dry='rsync -avh --progress --exclude-from=~/.rsync-exclude --dry-run' + + # Full backup with all safety options + alias rsync-full='rsync-backup' + + # Sync directories (no delete) + alias rsync-sync='rsync -avh --progress --exclude-from=~/.rsync-exclude' + + # Mirror directories (with delete) + alias rsync-mirror='rsync -avh --progress --exclude-from=~/.rsync-exclude --delete' + ''; +}