From 4bb0de28c82edb747388a518e5a662b7e17cd666 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Wed, 12 Apr 2023 13:37:04 +0200 Subject: [PATCH] implemented backup-pull from static latest instead of relative --- .../files/pull-remote-backup.sh | 46 ++++++++++--------- .../templates/ssh-wrapper.sh.j2 | 20 +++++--- 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/roles/native-pull-primary-backups/files/pull-remote-backup.sh b/roles/native-pull-primary-backups/files/pull-remote-backup.sh index 17f41f3e..e82ef3d8 100644 --- a/roles/native-pull-primary-backups/files/pull-remote-backup.sh +++ b/roles/native-pull-primary-backups/files/pull-remote-backup.sh @@ -4,42 +4,46 @@ # error counter errors=0 -source_host="backup@$1" -source_machine_id="$( (ssh "$source_host" sha256sum /etc/machine-id) | head -c 64)" || exit 1 -source_path="/Backups/$source_machine_id/" -directories="$(ssh "$source_host" find "$source_path" -maxdepth 1 -type d)" || exit 1 +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_dir in $directories; do - if [ "$backup_type_dir" != "$source_path" ]; then +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/" # folder which contains versions - versions_dir="$backup_type_dir/versions/" + general_versions_dir="$general_backup_type_dir""versions/" # link name of last backup - latest_version_link="$backup_type_dir/latest" + general_latest_version_link="$general_backup_type_dir""latest" + # this folder contains the last backup - latest_version_dir="$versions_dir$(date '+%Y%m%d%H%M%S')/" + local_latest_version_dir="$general_versions_dir$(date '+%Y%m%d%H%M%S')/" # this is the link name of the previous version - previous_version_link="$backup_type_dir/previous" - # source path of the backup files: - remote_source_path="$source_host:$latest_version_link/" + local_previous_version_link="$general_backup_type_dir""previous" #identifiy previous version - versions=( $(basename -a "$versions_dir"*/ | sort) )|| exit 1 - last_version="${versions[-1]}" || exit 1 - previous_version_dir="$versions_dir$last_version/" + 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/" # create working folders: - mkdir -vp "$latest_version_dir" + mkdir -vp "$local_latest_version_dir" # delete links - rm -v "$latest_version_link" - rm -v "$previous_version_link" + rm -v "$general_latest_version_link" + rm -v "$local_previous_version_link" # create links - ln -vs "$latest_version_dir" "$latest_version_link" || exit 1 - ln -vs "$previous_version_dir" "$previous_version_link"|| exit 1 + ln -vs "$local_latest_version_dir" "$general_latest_version_link" || exit 1 + ln -vs "$local_previous_version_dir" "$local_previous_version_link"|| exit 1 # do backup: - rsync -abP --delete --delete-excluded --rsync-path="sudo rsync" --link-dest="$previous_version_link" "$remote_source_path" "$latest_version_link" || ((errors+=1)); + rsync -abP --delete --delete-excluded --rsync-path="sudo rsync" --link-dest="$local_previous_version_link" "$remote_source_path" "$general_latest_version_link" || ((errors+=1)); fi done exit $errors; diff --git a/roles/native-user-backup/templates/ssh-wrapper.sh.j2 b/roles/native-user-backup/templates/ssh-wrapper.sh.j2 index a22b8635..61a1171b 100644 --- a/roles/native-user-backup/templates/ssh-wrapper.sh.j2 +++ b/roles/native-user-backup/templates/ssh-wrapper.sh.j2 @@ -7,17 +7,23 @@ then fi # define executable commands -hashed_machine_id_command="sha256sum /etc/machine-id"; -find_command="find /Backups/{{hashed_machine_id.stdout}}/ -maxdepth 1 -type d"; -rsync_command="sudo rsync --server --sender -blogDtpre.iLsfxCIvu . /Backups/{{hashed_machine_id.stdout}}/docker-volume-backup/latest/" +get_hashed_machine_id="sha256sum /etc/machine-id"; +get_backup_types="find /Backups/{{hashed_machine_id.stdout}}/ -maxdepth 1 -type d -execdir basename {} ';'"; + +# @todo This configuration is not scalable yet. If other backup services then docker-volume-backup are integrated, this logic needs to be optimized +get_static_last_version_dir="readlink -f /Backups/{{hashed_machine_id.stdout}}/docker-volume-backup/latest/" +rsync_command="sudo rsync --server --sender -blogDtpre.iLsfxCIvu . $($get_static_last_version_dir)/" # filter commands case "$SSH_ORIGINAL_COMMAND" in - "$hashed_machine_id_command") - $hashed_machine_id_command + "$get_hashed_machine_id") + $get_hashed_machine_id ;; - "$find_command") - $find_command + "$get_static_last_version_dir") + $get_static_last_version_dir + ;; + "$get_backup_types") + $get_backup_types ;; "$rsync_command") $rsync_command