2021-01-10 20:35:37 +01:00
|
|
|
#!/bin/bash
|
|
|
|
# @param $1 hostname from which backup should be pulled
|
2021-01-11 14:14:36 +01:00
|
|
|
|
|
|
|
# error counter
|
|
|
|
errors=0
|
|
|
|
|
2023-04-12 13:37:04 +02:00
|
|
|
remote_host="backup@$1"
|
|
|
|
remote_machine_id="$( (ssh "$remote_host" sha256sum /etc/machine-id) | head -c 64 )" || exit 1
|
|
|
|
general_backup_machine_dir="/Backups/$remote_machine_id/"
|
|
|
|
remote_backup_types="$(ssh "$remote_host" find "$general_backup_machine_dir" -maxdepth 1 -type d -execdir basename {} ';')" || exit 1
|
|
|
|
|
|
|
|
for backup_type in $remote_backup_types; do
|
|
|
|
if [ "$backup_type" != "$remote_machine_id" ]; then
|
|
|
|
general_backup_type_dir="$general_backup_machine_dir""$backup_type/"
|
2022-03-29 21:20:51 +02:00
|
|
|
# folder which contains versions
|
2023-04-12 13:37:04 +02:00
|
|
|
general_versions_dir="$general_backup_type_dir""versions/"
|
2022-03-29 21:20:51 +02:00
|
|
|
# link name of last backup
|
2023-04-12 13:37:04 +02:00
|
|
|
general_latest_version_link="$general_backup_type_dir""latest"
|
|
|
|
|
2022-03-29 21:20:51 +02:00
|
|
|
# this folder contains the last backup
|
2023-04-12 13:37:04 +02:00
|
|
|
local_latest_version_dir="$general_versions_dir$(date '+%Y%m%d%H%M%S')/"
|
2022-03-29 21:20:51 +02:00
|
|
|
# this is the link name of the previous version
|
2023-04-12 13:37:04 +02:00
|
|
|
local_previous_version_link="$general_backup_type_dir""previous"
|
2022-03-29 21:20:51 +02:00
|
|
|
|
|
|
|
#identifiy previous version
|
2023-04-12 13:37:04 +02:00
|
|
|
local_versions=( $(basename -a "$general_versions_dir"*/ | sort) )|| exit 1
|
|
|
|
local_last_version="${local_versions[-1]}" || exit 1
|
|
|
|
local_previous_version_dir="$general_versions_dir""$last_version/"
|
|
|
|
|
|
|
|
# source path of the backup files:
|
|
|
|
remote_last_version_dir="$(readlink -f $general_latest_version_link)"
|
|
|
|
remote_source_path="$remote_host:$remote_last_version_dir/"
|
2021-01-11 14:14:36 +01:00
|
|
|
|
|
|
|
# create working folders:
|
2023-04-12 13:37:04 +02:00
|
|
|
mkdir -vp "$local_latest_version_dir"
|
2022-03-29 21:20:51 +02:00
|
|
|
|
|
|
|
# delete links
|
2023-04-12 13:37:04 +02:00
|
|
|
rm -v "$general_latest_version_link"
|
|
|
|
rm -v "$local_previous_version_link"
|
2022-03-29 21:20:51 +02:00
|
|
|
|
|
|
|
# create links
|
2023-04-12 13:37:04 +02:00
|
|
|
ln -vs "$local_latest_version_dir" "$general_latest_version_link" || exit 1
|
|
|
|
ln -vs "$local_previous_version_dir" "$local_previous_version_link"|| exit 1
|
2021-01-11 14:14:36 +01:00
|
|
|
|
|
|
|
# do backup:
|
2023-04-12 13:37:04 +02:00
|
|
|
rsync -abP --delete --delete-excluded --rsync-path="sudo rsync" --link-dest="$local_previous_version_link" "$remote_source_path" "$general_latest_version_link" || ((errors+=1));
|
2021-01-10 20:35:37 +01:00
|
|
|
fi
|
|
|
|
done
|
2021-01-11 14:14:36 +01:00
|
|
|
exit $errors;
|