BASH
Timestamped Directory Backup with Rsync
A robust bash script for creating efficient, timestamped backups of important directories using rsync, ensuring data integrity and versioning.
#!/bin/bash
SOURCE_DIR="/var/www/mywebapp" # Directory to back up
BACKUP_ROOT_DIR="/mnt/backups/webapp" # Root directory for backups
DATE_SUFFIX=$(date +"%Y%m%d_%H%M%S")
BACKUP_TARGET_DIR="$BACKUP_ROOT_DIR/backup_$DATE_SUFFIX"
LATEST_LINK="$BACKUP_ROOT_DIR/latest" # Symlink to the latest backup
# Create backup root directory if it doesn't exist
mkdir -p "$BACKUP_ROOT_DIR"
echo "--- Starting backup of '$SOURCE_DIR' to '$BACKUP_TARGET_DIR' ---"
# Use rsync for efficient backup (e.g., --link-dest for incremental backups)
if [ -e "$LATEST_LINK" ]; then
echo "Performing incremental backup..."
# --archive: recursive, copy symlinks, preserve permissions, modification times, group, owner
# --human-readable: output numbers in a human-readable format
# --delete-excluded: delete excluded files on the receiving side
# --exclude: exclude specific files/directories (e.g., node_modules, .git)
# --link-dest: hardlink files that are unchanged from the previous backup
rsync -avh --delete-excluded \
--exclude='node_modules/' \
--exclude='.git/' \
--exclude='vendor/' \
--link-dest="$LATEST_LINK" "$SOURCE_DIR/" "$BACKUP_TARGET_DIR/"
else
echo "Performing full backup (first backup)..."
rsync -avh --delete-excluded \
--exclude='node_modules/' \
--exclude='.git/' \
--exclude='vendor/' \
"$SOURCE_DIR/" "$BACKUP_TARGET_DIR/"
fi
# Update the 'latest' symlink
rm -f "$LATEST_LINK"
ln -s "$BACKUP_TARGET_DIR" "$LATEST_LINK"
echo "Backup completed successfully."
echo "Latest backup available at: $LATEST_LINK"
echo "--------------------------------------------------"
How it works: This script automates creating timestamped backups of a source directory using `rsync`. It leverages `rsync`'s `--link-dest` option to perform incremental backups, where unchanged files are hard-linked from the previous backup, saving significant disk space and time. The script also handles initial full backups, excludes specified directories like `node_modules` and `.git`, and maintains a `latest` symlink for easy access to the most recent backup. This ensures efficient, versioned, and space-saving data protection.