LinuxDojo

Optimized Snapshot Backups with rsync

This guide details an optimized method for creating snapshot-style backups using rsync. We'll build a reusable script that's both powerful and efficient, using advanced flags for cleaner output and better performance while keeping your backup a perfect mirror of your source.

Step 1: Create a Comprehensive Exclude File

A good exclude file is the secret to fast and efficient backups. It prevents rsync from wasting time on files that are temporary, can be regenerated, or simply don't need a backup. Instead of using many --exclude flags, list them in a single text file.

Here is a robust example for a file you could name rsync-exclude.txt. It covers system caches, log files, virtual machine disks, and other common exclusions.

rsync-exclude.txt
# --- Cache & Temp Files ---
# Generic cache directory used by many applications
.cache/

# User-specific trash folders
.Trash-*/
/home/*/.local/share/Trash/*

# --- Cloud Storage & Sync Folders ---
# These are already synced to the cloud, no need to back them up again.
pCloudDrive/
Dropbox/

# --- Development Folders ---
# Huge dependency folders that can be reinstalled easily.
node_modules/

# --- Application Specific Caches ---
# Examples for common browsers and apps.
/home/*/.mozilla/firefox/*/cache2/
/home/*/.config/google-chrome/Default/Cache/

# --- System & Virtualization ---
# Never back up virtual system directories.
/proc/*
/sys/*
/dev/*
/run/*
/tmp/*

# Virtual Machine disk files are huge and change often.
# It's better to back up the VM from the host machine's interface.
*.vdi
*.vmdk

# --- Log & Temporary Files ---
# Wildcard patterns to exclude common log and temp files anywhere.
*.log
*.tmp
*.bak
*~

Using a dedicated file makes it trivial to add or remove exclusions later without modifying your main script.

Step 2: The Complete Backup Script

Now, let's create the final rsync_backup.sh script. This script incorporates our optimized flags and points to the exclude file for a robust and manageable solution.

bash
#!/bin/sh

# An optimized script to perform a snapshot-style backup using rsync.

# --- CONFIGURATION ---
# The source directory you want to back up.
SOURCE="/home/joe"

# The top-level destination for your backup.
DESTINATION="/mnt/backups/mercury_backup"

# The full, absolute path to your exclude file.
EXCLUDE_FILE="/home/joe/scripts/rsync-exclude.txt"


# --- SCRIPT LOGIC ---
echo "🚀 Starting optimized rsync backup..."
echo "Source:      $SOURCE"
echo "Destination: $DESTINATION"

# Check if the exclude file exists
if [ ! -f "$EXCLUDE_FILE" ]; then
    echo "Error: Exclude file not found at $EXCLUDE_FILE"
    exit 1
fi

# Run the rsync command!
rsync -a \
      --info=progress2 \
      --no-inc-recursive \
      --delete \
      --exclude-from="$EXCLUDE_FILE" \
      "$SOURCE/" "$DESTINATION/"

echo "✅ Backup complete!"

Remember: You must update the EXCLUDE_FILE variable to the correct absolute path on your system.