Linux – Rsync Backup with Rotation (Bash)

Below is a ready-to-use script. Review and adapt variables to your environment.

#!/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
  

← Back to SQL