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
2021-01-10 20:35:37 +01:00
for folder in $directories; do
if [ "$folder" != "$source_path" ]; then
2021-01-11 14:14:36 +01:00
# this folder is neccessary to make restricted rsync possible:
diff_current="$folder/diffs/current/"
# this is the folder where the diffs will be copied to:
diff_store="$folder/diffs/$(date '+%Y%m%d%H%M%S')/"
# this is the folder which contains the actual backup:
2021-01-10 20:35:37 +01:00
latest_path="$folder/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:
2021-01-10 20:35:37 +01:00
log_path="$folder/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"
rm -vr "$diff_current"
mkdir -vp "$diff_current"
# do backup:
rsync -abP --delete --delete-excluded --rsync-path="sudo rsync" --log-file="$log_path" --backup-dir="$diff_current" "$remote_source_path" "$latest_path" || ((errors+=1));
mv -v "$diff_current"* "$diff_store"
2021-01-10 20:35:37 +01:00
fi
done
2021-01-11 14:14:36 +01:00
exit $errors;