computer-playbook/roles/native-pull-primary-backups/files/pull-remote-backup.sh

37 lines
1.3 KiB
Bash
Raw Normal View History

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
2021-01-10 20:35:37 +01:00
source_host="backup@$1"
2021-01-11 14:14:36 +01:00
source_machine_id="$( (ssh "$source_host" sha256sum /etc/machine-id) | head -c 64)" || exit 1
2021-01-10 20:35:37 +01:00
source_path="/Backups/$source_machine_id/"
2021-01-11 14:14:36 +01:00
directories="$(ssh "$source_host" find "$source_path" -maxdepth 1 -type d)" || exit 1
2022-03-29 19:56:41 +02:00
for backup_type_dir in $directories; do
if [ "$backup_type_dir" != "$source_path" ]; then
2021-01-11 14:14:36 +01:00
# this folder is neccessary to make restricted rsync possible:
2022-03-29 19:56:41 +02:00
current_version="$backup_type_dir/versions/current/"
# this is the folder where the versions will be copied to:
diff_store="$backup_type_dir/versions/$(date '+%Y%m%d%H%M%S')/"
2021-01-11 14:14:36 +01:00
# this is the folder which contains the actual backup:
2022-03-29 19:56:41 +02:00
latest_path="$backup_type_dir/latest/"
2021-01-11 14:14:36 +01:00
# source path of the backup files:
2021-01-10 20:35:37 +01:00
remote_source_path="$source_host:$latest_path"
2021-01-11 14:14:36 +01:00
# file in which the logs will be saved:
2022-03-29 19:56:41 +02:00
log_path="$backup_type_dir/log.txt"
2021-01-11 14:14:36 +01:00
# create working folders:
2021-01-10 20:35:37 +01:00
mkdir -vp "$latest_path"
2021-01-11 14:14:36 +01:00
mkdir -vp "$diff_store"
2022-03-29 19:56:41 +02:00
rm -vr "$current_version"
mkdir -vp "$current_version"
2021-01-11 14:14:36 +01:00
# do backup:
2022-03-29 19:56:41 +02:00
rsync -abP --delete --delete-excluded --rsync-path="sudo rsync" --log-file="$log_path" --backup-dir="$current_version" "$remote_source_path" "$latest_path" || ((errors+=1));
mv -v "$current_version"* "$diff_store"
2021-01-10 20:35:37 +01:00
fi
done
2021-01-11 14:14:36 +01:00
exit $errors;