From ac1720f70075f353d0dbbf17c955423515d9cc47 Mon Sep 17 00:00:00 2001 From: Aroy-Art Date: Fri, 8 Sep 2023 10:15:14 +0200 Subject: [PATCH 1/2] Add: rsync script for incremental backups --- ShellScripts/rsync-incremental-backups.sh | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 ShellScripts/rsync-incremental-backups.sh diff --git a/ShellScripts/rsync-incremental-backups.sh b/ShellScripts/rsync-incremental-backups.sh new file mode 100644 index 0000000..58b3abe --- /dev/null +++ b/ShellScripts/rsync-incremental-backups.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# A script to perform incremental backups using rsync + +set -o errexit +set -o nounset +set -o pipefail + +#readonly SOURCE_DIR="${HOME}" +readonly SOURCE_DIR="" +readonly BACKUP_DIR="" +readonly DATETIME="$(date '+%Y-%m-%d_%H:%M:%S')" +readonly BACKUP_PATH="${BACKUP_DIR}/${DATETIME}" +readonly LATEST_LINK="${BACKUP_DIR}/latest" + +mkdir -p "${BACKUP_DIR}" + +rsync -ahv --progress --stats --delete \ + --log-file="${BACKUP_DIR}/${DATETIME}.log" \ + --exclude=".cache" \ + --link-dest "${LATEST_LINK}" \ + "${SOURCE_DIR}/" \ + "${BACKUP_PATH}" + +rm -rfv "${LATEST_LINK}" +ln -s "${BACKUP_PATH}" "${LATEST_LINK}" From cbca198ef4df7057ccd7553505a4626e10437365 Mon Sep 17 00:00:00 2001 From: Aroy-Art Date: Fri, 8 Sep 2023 10:20:11 +0200 Subject: [PATCH 2/2] Add: documentation --- ShellScripts/rsync-incremental-backups.sh | 29 +++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/ShellScripts/rsync-incremental-backups.sh b/ShellScripts/rsync-incremental-backups.sh index 58b3abe..1d67bf4 100644 --- a/ShellScripts/rsync-incremental-backups.sh +++ b/ShellScripts/rsync-incremental-backups.sh @@ -1,20 +1,42 @@ #!/bin/bash -# A script to perform incremental backups using rsync +# 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 -#readonly SOURCE_DIR="${HOME}" +# 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" \ @@ -22,5 +44,8 @@ rsync -ahv --progress --stats --delete \ "${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}"