2023-09-08 10:15:14 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2023-09-08 10:20:11 +02:00
|
|
|
# Script for performing incremental backups using rsync.
|
2023-09-08 10:15:14 +02:00
|
|
|
|
2023-09-08 10:20:11 +02:00
|
|
|
# Set the script to exit if any command returns a non-zero status.
|
2023-09-08 10:15:14 +02:00
|
|
|
set -o errexit
|
2023-09-08 10:20:11 +02:00
|
|
|
# Set the script to treat unset variables as errors.
|
2023-09-08 10:15:14 +02:00
|
|
|
set -o nounset
|
2023-09-08 10:20:11 +02:00
|
|
|
# Set the script to exit if a pipeline returns a non-zero status.
|
2023-09-08 10:15:14 +02:00
|
|
|
set -o pipefail
|
|
|
|
|
2023-09-08 10:20:11 +02:00
|
|
|
# Define the source directory you want to back up.
|
2023-09-08 10:15:14 +02:00
|
|
|
readonly SOURCE_DIR="<input folder>"
|
2023-09-08 10:20:11 +02:00
|
|
|
|
|
|
|
# Define the directory where the backups will be stored.
|
2023-09-08 10:15:14 +02:00
|
|
|
readonly BACKUP_DIR="<output folder>"
|
2023-09-08 10:20:11 +02:00
|
|
|
|
|
|
|
# Get the current date and time in the format 'YYYY-MM-DD_HH:MM:SS'.
|
2023-09-08 10:15:14 +02:00
|
|
|
readonly DATETIME="$(date '+%Y-%m-%d_%H:%M:%S')"
|
2023-09-08 10:20:11 +02:00
|
|
|
|
|
|
|
# Create a path for the current backup using the datetime.
|
2023-09-08 10:15:14 +02:00
|
|
|
readonly BACKUP_PATH="${BACKUP_DIR}/${DATETIME}"
|
2023-09-08 10:20:11 +02:00
|
|
|
|
|
|
|
# Create a symbolic link called 'latest' that points to the most recent backup.
|
2023-09-08 10:15:14 +02:00
|
|
|
readonly LATEST_LINK="${BACKUP_DIR}/latest"
|
|
|
|
|
2023-09-08 10:20:11 +02:00
|
|
|
# Ensure that the backup directory exists.
|
2023-09-08 10:15:14 +02:00
|
|
|
mkdir -p "${BACKUP_DIR}"
|
|
|
|
|
2023-09-08 10:20:11 +02:00
|
|
|
# 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.
|
2023-09-08 10:15:14 +02:00
|
|
|
rsync -ahv --progress --stats --delete \
|
|
|
|
--log-file="${BACKUP_DIR}/${DATETIME}.log" \
|
|
|
|
--exclude=".cache" \
|
|
|
|
--link-dest "${LATEST_LINK}" \
|
|
|
|
"${SOURCE_DIR}/" \
|
|
|
|
"${BACKUP_PATH}"
|
|
|
|
|
2023-09-08 10:20:11 +02:00
|
|
|
# Remove the symbolic link 'latest' to the old backup.
|
2023-09-08 10:15:14 +02:00
|
|
|
rm -rfv "${LATEST_LINK}"
|
2023-09-08 10:20:11 +02:00
|
|
|
|
|
|
|
# Create a new symbolic link 'latest' pointing to the most recent backup.
|
2023-09-08 10:15:14 +02:00
|
|
|
ln -s "${BACKUP_PATH}" "${LATEST_LINK}"
|