Automate incremental backups using rsync logic or mtime checking. Create hard links for unchanged files in new backup folders so each backup looks like a full snapshot but consumes minimal storage.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Incremental backups can be automated using rsync's --link-dest option, which creates new backup folders where unchanged files are hard linked to a previous snapshot, making each backup appear full but using minimal storage. A simple Bash script creates a dated backup directory, finds the last backup, and runs rsync with --link-dest to efficiently copy only changed files. Alternatively, using cp -al to hard-link the previous backup directory before syncing updates achieves a similar result. Both methods can be scheduled with cron, and pruning old backups frees space safely since unchanged files are hard linked. This approach preserves file attributes and efficiently tracks changes without duplicating data.
What you’re describing is the “snapshot” style of incremental backup popularised by tools like rsnapshot: each run produces a complete-looking tree, but unchanged files are hard‑linked back to the previous run so they don’t consume extra space.
You can build this either directly with rsync’s --link-dest option or by doing the hard‑linking yourself. Here’s a minimal, self‑contained approach in shell using rsync:
bash
#!/usr/bin/env bash
set -euo pipefail
SRC="/path/to/source"
DEST_ROOT="/path/to/backups" # parent directory that will contain dated snapshots
TS=$(date +%Y-%m-%d_%H-%M-%S)
DEST="$DEST_ROOT/$TS"
# Find the most recent backup to use as link‑dest, if any
PREV=$(ls -1t "$DEST_ROOT" | head -1 || true)
LINKDEST=""
if [ -n "$PREV" ] && [ -d "$DEST_ROOT/$PREV" ]; then
LINKDEST="--link-dest=$DEST_ROOT/$PREV"
fi
# Create the new snapshot; rsync will hard‑link unchanged files to $LINKDEST
rsync -a --delete $LINKDEST "$SRC/" "$DEST/"
# Optional: prune old backups
# find "$DEST_ROOT" -maxdepth 1 -type d -mtime +30 -exec rm -rf {} +
Key points:
The trailing slashes on "$SRC/" and "$DEST/" matter: they tell rsync to sync the contents of SRC into DEST.
--delete makes deletions in SRC propagate to the new snapshot; older snapshots are untouched.
--link-dest points to the previous snapshot. Unchanged files are hard‑linked instead of recopied.
Backups must reside on the same filesystem for hard links to work.
If you prefer to do the hard‑linking step yourself based on mtimes, an equivalent pattern is:
bash
#!/usr/bin/env bash
set -euo pipefail
SRC="/path/to/source"
DEST_ROOT="/path/to/backups"
TS=$(date +%Y-%m-%d_%H-%M-%S)
DEST="$DEST_ROOT/$TS"
PREV=$(ls -1t "$DEST_ROOT" | head -1 || true)
mkdir -p "$DEST"
# If there is a previous backup, prepopulate DEST with hard links to it
if [ -n "$PREV" ] && [ -d "$DEST_ROOT/$PREV" ]; then
cp -al "$DEST_ROOT/$PREV"/. "$DEST"/
fi
# Now overlay changes from SRC; modified/new files replace the hard links
rsync -a --delete "$SRC/" "$DEST/"
This works because cp -al makes a tree of hard links to the previous snapshot, then rsync overwrites any links that need to change.
You can schedule either script from cron or a systemd timer to run nightly, and add retention logic to delete snapshots older than N days. If you need to preserve permissions, ACLs, xattrs, etc., add the appropriate rsync flags (-A-X-H). Always test on non‑critical data first to verify that the snapshots look and behave as expected.