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
|
|
|
|
2023-04-19 13:36:19 +02:00
|
|
|
echo "pulling backups from: $1" &&
|
2023-04-12 20:32:52 +02:00
|
|
|
|
2021-01-11 14:14:36 +01:00
|
|
|
# error counter
|
2023-04-19 13:36:19 +02:00
|
|
|
errors=0 &&
|
2021-01-11 14:14:36 +01:00
|
|
|
|
2023-04-19 13:36:19 +02:00
|
|
|
echo "loading meta data..." &&
|
2023-04-12 20:32:52 +02:00
|
|
|
|
2023-04-19 13:36:19 +02:00
|
|
|
remote_host="backup@$1" &&
|
|
|
|
echo "host address: $remote_host" &&
|
2023-04-12 20:32:52 +02:00
|
|
|
|
2023-04-19 13:36:19 +02:00
|
|
|
remote_machine_id="$( (ssh "$remote_host" sha256sum /etc/machine-id) | head -c 64 )" &&
|
|
|
|
echo "remote machine id: $remote_machine_id" &&
|
2023-04-12 20:32:52 +02:00
|
|
|
|
2023-04-19 13:36:19 +02:00
|
|
|
general_backup_machine_dir="/Backups/$remote_machine_id/" &&
|
|
|
|
echo "backup dir: $general_backup_machine_dir" &&
|
2023-04-12 20:32:52 +02:00
|
|
|
|
2023-04-19 13:36:19 +02:00
|
|
|
remote_backup_types="$(ssh "$remote_host" "find $general_backup_machine_dir -maxdepth 1 -type d -execdir basename {} ;")" &&
|
|
|
|
echo "backup types: $remote_backup_types" || exit 1
|
2023-04-12 13:37:04 +02:00
|
|
|
|
|
|
|
for backup_type in $remote_backup_types; do
|
|
|
|
if [ "$backup_type" != "$remote_machine_id" ]; then
|
2023-04-19 13:36:19 +02:00
|
|
|
echo "backup type: $backup_type" &&
|
2023-04-12 13:37:04 +02:00
|
|
|
|
2023-04-19 13:36:19 +02:00
|
|
|
general_backup_type_dir="$general_backup_machine_dir""$backup_type/" &&
|
|
|
|
general_versions_dir="$general_backup_type_dir" &&
|
|
|
|
local_previous_version_dir="$(ls -d $general_versions_dir* | tail -1)" &&
|
|
|
|
echo "last local backup: $local_previous_version_dir" &&
|
2022-03-29 21:20:51 +02:00
|
|
|
|
2023-04-19 13:36:19 +02:00
|
|
|
remote_backup_versions="$(ssh "$remote_host" ls -d "$general_backup_type_dir"\*)" &&
|
|
|
|
echo "remote backup versions: $remote_backup_versions" &&
|
2023-04-12 13:37:04 +02:00
|
|
|
|
2021-01-11 14:14:36 +01:00
|
|
|
|
2023-04-19 13:36:19 +02:00
|
|
|
remote_last_backup_dir=$(echo "$remote_backup_versions" | tail -1) &&
|
|
|
|
echo "last remote backup: $remote_last_backup_dir" &&
|
2022-03-29 21:20:51 +02:00
|
|
|
|
2023-04-19 13:36:19 +02:00
|
|
|
remote_source_path="$remote_host:$remote_last_backup_dir/" &&
|
|
|
|
echo "source path: $remote_source_path" &&
|
2022-03-29 21:20:51 +02:00
|
|
|
|
2023-04-19 13:36:19 +02:00
|
|
|
local_backup_destination_path=$remote_last_backup_dir &&
|
|
|
|
echo "backup destination: $local_backup_destination_path" &&
|
2021-01-11 14:14:36 +01:00
|
|
|
|
2023-04-19 13:36:19 +02:00
|
|
|
echo "creating local backup destination folder..." &&
|
|
|
|
mkdir -vp "$local_backup_destination_path" &&
|
|
|
|
|
2023-04-25 21:39:44 +02:00
|
|
|
status_pulling_file="$local_backup_destination_path/.pulling" &&
|
|
|
|
echo "creating: $status_pulling_file" &&
|
|
|
|
echo "pulling backup since $(date)" > $status_pulling_file &&
|
|
|
|
|
2023-04-19 13:36:19 +02:00
|
|
|
echo "starting backup..." &&
|
|
|
|
rsync_command='rsync -abP --delete --delete-excluded --rsync-path="sudo rsync" --link-dest="'$local_previous_version_dir'" "'$remote_source_path'" "'$local_backup_destination_path'"' &&
|
|
|
|
echo "executing: $rsync_command" &&
|
2023-04-25 21:39:44 +02:00
|
|
|
eval "$rsync_command" &&
|
|
|
|
|
|
|
|
echo "removing: $status_pulling_file" &&
|
|
|
|
rm -vf $status_pulling_file
|
|
|
|
|
|
|
|
|| ((errors+=1));
|
2021-01-10 20:35:37 +01:00
|
|
|
fi
|
|
|
|
done
|
2021-01-11 14:14:36 +01:00
|
|
|
exit $errors;
|