WEBTOOLIST.
STATIC WEB PLATFORM
NO ACCOUNTS / NOTHING TO INSTALL

$ webtoolist --script rsync-snapshot-backup

Rsync Snapshot Backup

Mirror a directory to a backup target with dry-run, delete protection, and a log.

category Backup and Recovery shell Bash 4+ updated 2026-09-15
backuprsyncsnapshotsync
Safety notes
  • Can delete files at the destination when --delete is explicitly enabled.
  • Always review the dry run; the script will not enable deletion by default.

Dependencies: rsync, date

Usage

  • Pass source and destination paths.
  • Add —delete only when the destination is a true mirror.
  • Set LOG_DIR to keep a per-run log.

Script

#!/usr/bin/env bash
set -euo pipefail

src="${1:-}"
dest="${2:-}"
[[ -n "$src" && -n "$dest" ]] || { echo "usage: $0 SRC DEST [--delete]" >&2; exit 1; }

extra=()
[[ ${3:-} == --delete ]] && extra+=(--delete)

log_dir="${LOG_DIR:-$HOME/.local/state/rsync-backups}"
mkdir -p "$log_dir"
log="$log_dir/$(date +%Y%m%d-%H%M%S).log"

rsync -aHAX --numeric-ids --info=progress2 \
  "${extra[@]}" \
  --exclude '.cache/' \
  --exclude 'node_modules/' \
  "$src"/ "$dest"/ | tee "$log"

echo "log: $log"

Example

./rsync-snapshot-backup ~/projects /mnt/backups/projects
./rsync-snapshot-backup ~/projects /mnt/backups/projects --delete

Notes

  • The trailing slash on the source matters: it copies contents, not the directory itself.
  • -H preserves hard links; -A and -X preserve ACLs and xattrs where supported.

Related scripts

Safe SQLite Backup

Use SQLite's online backup API to copy a live database consistently.

Backup and Recovery

Verify a Tar Backup

Check an archive's checksum, list its contents, and compare a sample safely.

Backup and Recovery