#!/bin/bash set -euo pipefail exec 9>/var/lock/hetznerSync.lock if ! flock -n 9 then echo "[$(date --rfc-3339=seconds)] offsite: another sync is still running, skipping this run" exit 0 fi source /opt/backup/metrics.sh metric_init offsite trap metric_guard EXIT if ! mountpoint -q {{ hetzner_raid_mount }} then log "{{ hetzner_raid_mount }} not mounted, refusing to sync" exit 1 fi sync_path() { local src=$1 local dest=$2 if [ -z "$(ls -A ${src} 2>/dev/null)" ] then log "${src} empty, refusing to sync" exit 1 fi log "syncing ${src} to ${dest}" sudo -u {{ user }} rsync -a --delete --partial \ -e "ssh -p {{ hetzner_port }} -i {{ user_home }}/.ssh/id_ed25519 -o UserKnownHostsFile={{ user_home }}/.ssh/known_hosts -o StrictHostKeyChecking=yes" \ ${src}/ \ {{ hetzner_user }}@{{ hetzner_host }}:${dest}/ } {% for path in hetzner_paths %} sync_path {{ path.src }} {{ path.dest }} {% endfor %}