Incremental Backup using rsync

Category: SW linux tools

Posted at 2021-02-13.



No Backup No Mercy





Here is a little script that can be found in a similar form on the internet. Since it essentially only calls rsync with the --link-dest option, that's actually no wonder.

The --link-dest option ensures that we get an incremental backup. It creates hard links for unchanged files. A separate directory is created for each backup. After copying, a link "latest" is set pointing to the directory. During the next backup run, this directory will be "--link-dest". The link is set to the newly created directory after the backup run.

The script is started on the computer on which the target directory is located. (Otherwise change the last two lines and execute the commands remotely - e.g. using ssh-.)

I named the script „rsync_backup.sh" and call it like so:

rsync_backup.sh isis:VM-exports /extra/backups/VBox_exports 'g*'



#!/bin/bash

# incremental backups using rsync
set -e

if [ "$#" -lt 2 ]; then
  echo usage: $(basename $BASH_SOURCE) [remote_host:]source_dir target_dir_must_be_local \[exclude_pattern\]
  exit 1
fi
SOURCE_DIR="$1"
BACKUP_DIR="$2"
if [ "$#" -ge 3 ]; then
  EXCLUDE_PATTERN="$3"
  exclude="--exclude"
else
  exclude=""
  EXCLUDE_PATTERN=""
fi

BACKUP_PATH="${BACKUP_DIR}/$(date '+%Y-%m-%d_%H:%M:%S')"
link2latest="${BACKUP_DIR}/latest"

mkdir -p "${BACKUP_DIR}"
rsync -av --delete $exclude $EXCLUDE_PATTERN  --link-dest "${link2latest}" "${SOURCE_DIR}/" "${BACKUP_PATH}"

rm -f "${link2latest}"
ln -s "${BACKUP_PATH}" "${link2latest}"


Also worth mentioning is rsync's option --backup (with --suffix and --backup-dir).


Send us your Comment

... because software matters