#!/bin/sh

# Perform incremental backups using rsync and hardlinks.
#
# Thanks to http://www.sanitarium.net/golug/rsync_backups_2010.html for the
# idea.

# Copyright (C) 2011-2017  Simon Ruderich
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


set -eu

if test "$#" -lt 2; then
    echo "Usage: $0 <backups-directory> <arguments to rsync>" >&2
    echo
    echo "Note: The target directory is the _first_ argument!" >&2
    exit 2
fi


cd "$1"
shift

# Get path to last backup directory.
dest=./
for x in backup-*; do
    test -d "$x" || continue
    dest="../$x" # relative to destination directory
done

target="backup-$(date '+%Y-%m-%d-%H-%M-%S')"
target_tmp="partial-$target"

mkdir "$target_tmp"
rsync \
    --verbose --itemize-changes --human-readable \
    --archive --acls --xattrs --hard-links --sparse --numeric-ids \
    --one-file-system \
    --link-dest="$dest" \
    "$@" "$target_tmp" \
|| {
    # Try to remove the target directory without changing the exit code. In
    # case the connection failed without transferring any files, we want to
    # remove the empty directory.
    code=$?
    rmdir "$target_tmp" 2>/dev/null || true
    exit $code
}
# --dry-run (-n) creates an empty directory. Remove it to prevent using it for
# further incremental backups (which would do a full backup).
rmdir "$target_tmp" 2>/dev/null && exit 0 || true

mv "$target_tmp" "$target"
