dotfiles/root/usr/local/bin/incremental-backup

38 lines
1.1 KiB
Plaintext
Raw Permalink Normal View History

2021-05-03 13:31:15 +00:00
#!/bin/bash
# Script to perform incremental backups using rsync
# It is often ran as crontab rule for automated backup solution
#
# This script will respect .rsync-filter files, which can be used
# to define custom exclude rules for files/dirs in which it is present
2021-05-03 13:59:55 +00:00
if [ $# -lt 2 ]; then
2021-05-03 13:31:15 +00:00
echo "Invalid amount of arguments passed!"
echo "Arguments: [Source path] [Backup path]"
echo " Source path: directory to be backed up, usually '/'"
echo " Backup path: directory to back up to (destination), usually mounted drive"
exit
fi
SOURCE_DIR="$1"
BACKUP_DIR="$2"
DATETIME="$(date '+%Y-%m-%d_%H:%M:%S')"
BACKUP_PATH="${BACKUP_DIR}/${DATETIME}"
LATEST_LINK="${BACKUP_DIR}/latest"
mkdir -p "$BACKUP_DIR"
rsync -avHAXS \
--delete \
--filter='dir-merge /.rsync-filter' \
--link-dest "${LATEST_LINK}" \
"${@:3}" "${SOURCE_DIR}/" "${BACKUP_PATH}"
2021-05-03 13:31:15 +00:00
2021-05-03 13:59:55 +00:00
# Only attempt to override the symlink if we made new backup_path
# user might've passed --dry-run option in which case we wouldn't
# want to override latest symlink to non-existent location
if [ -d "${BACKUP_PATH}" ]; then
rm "${LATEST_LINK}" 2>/dev/null
ln -s "${BACKUP_PATH}" "${LATEST_LINK}"
fi