diff --git a/ShellScripts/rsync-incremental-backups.sh b/ShellScripts/rsync-incremental-backups.sh new file mode 100644 index 0000000..1d67bf4 --- /dev/null +++ b/ShellScripts/rsync-incremental-backups.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +# Script for performing incremental backups using rsync. + +# Set the script to exit if any command returns a non-zero status. +set -o errexit +# Set the script to treat unset variables as errors. +set -o nounset +# Set the script to exit if a pipeline returns a non-zero status. +set -o pipefail + +# Define the source directory you want to back up. +readonly SOURCE_DIR="" + +# Define the directory where the backups will be stored. +readonly BACKUP_DIR="" + +# Get the current date and time in the format 'YYYY-MM-DD_HH:MM:SS'. +readonly DATETIME="$(date '+%Y-%m-%d_%H:%M:%S')" + +# Create a path for the current backup using the datetime. +readonly BACKUP_PATH="${BACKUP_DIR}/${DATETIME}" + +# Create a symbolic link called 'latest' that points to the most recent backup. +readonly LATEST_LINK="${BACKUP_DIR}/latest" + +# Ensure that the backup directory exists. +mkdir -p "${BACKUP_DIR}" + +# Use rsync to perform the backup: +# - 'ahv' preserves permissions, owner, group, and provides verbose output. +# - '--progress' shows progress during transfer. +# - '--stats' shows statistics at the end of the backup. +# - '--delete' removes files from the backup that are no longer present in the source. +# - '--log-file' specifies a log file for recording backup details. +# - '--exclude' excludes the '.cache' directory from being backed up. +# - '--link-dest' specifies a directory with previous backups, enabling hard-linking of unchanged files. +# - "${SOURCE_DIR}/" is the source directory to be backed up. +# - "${BACKUP_PATH}" is the destination directory for the current backup. +rsync -ahv --progress --stats --delete \ + --log-file="${BACKUP_DIR}/${DATETIME}.log" \ + --exclude=".cache" \ + --link-dest "${LATEST_LINK}" \ + "${SOURCE_DIR}/" \ + "${BACKUP_PATH}" + +# Remove the symbolic link 'latest' to the old backup. +rm -rfv "${LATEST_LINK}" + +# Create a new symbolic link 'latest' pointing to the most recent backup. +ln -s "${BACKUP_PATH}" "${LATEST_LINK}"