Other toolsBash

Linux – Rsync Backup with Rotation (Bash)

Create rsync backups and rotate retained copies.

Bash
#!/usr/bin/env bash
set -euo pipefail
SRC="/data"
DEST="/backup/data"
RETENTION=7
STAMP=$(date +%F_%H%M)
mkdir -p "$DEST"
rsync -aHAX --delete --numeric-ids "$SRC"/ "$DEST/current"/
cp -al "$DEST/current" "$DEST/$STAMP"
ls -1dt "$DEST"/* | tail -n +$((RETENTION+1)) | xargs -r rm -rf
  

From Joseph Nakhle's automation workbench.

More Other tools scripts