diff --git a/examples/block-until-remote-reachable.sh b/examples/block-until-remote-reachable.sh new file mode 100644 index 0000000..7245314 --- /dev/null +++ b/examples/block-until-remote-reachable.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash +set -eu -o pipefail + +# Wait until the host yodaNas is reachable with SSH. +while :; do + result="$(ssh yodaNas 'hostname')" && e=0 || e=$? + if [ "${e}" = 0 ] && [ "${result}" = 'yodaNas' ]; then + break + fi + printf '%s\n' 'Delaying due to SSH connectivity problems.' + sleep 10s +done diff --git a/examples/block-while-remote-file-exists.sh b/examples/block-while-remote-file-exists.sh index a39fab8..16dff62 100644 --- a/examples/block-while-remote-file-exists.sh +++ b/examples/block-while-remote-file-exists.sh @@ -1,9 +1,10 @@ #!/usr/bin/env bash +set -eu -o pipefail # As long as there is the file `stay-alive` at host `yodaNas`, # this loop blocks/waits. while :; do - result="$(ssh yodaNas 'ls stay-alive 2>&1')" + result="$(ssh yodaNas 'ls stay-alive 2>&1')" || : case "${result}" in *"No such file or directory") break diff --git a/hosts/yodaHedgehog/host-specific.nix b/hosts/yodaHedgehog/host-specific.nix index 35604bc..44cae89 100644 --- a/hosts/yodaHedgehog/host-specific.nix +++ b/hosts/yodaHedgehog/host-specific.nix @@ -37,16 +37,12 @@ in timerConfig = { OnCalendar = [ # Daily - "*-*-* 00:00:05" + "*-*-* 00:05:00" ]; WakeSystem = true; }; }; systemd.services."daily-backup-and-suspend" = { - # If the system wakes up at 00:05, it is not directly connected to the Internet. - # Thus, `btrbk` might fail with the following error while connecting to `rootNas`: - # ssh: Could not resolve hostname p1st.de: Name or service not known - # To avoid this, we depend on the `network-online` systemd target. after = [ "network-online.target" ]; # Packages required for this script. # For `ssh` and `journalwatch`, there are assertions above. @@ -70,6 +66,23 @@ in set -eu -o pipefail #printf '%s\n' 'Starting backup script.' + # Wait until ${backup-source} is reachable. + # + # This test is necessary because of the following: + # If the system wakes up at 00:05, it is not directly connected to the Internet. + # The config option `after = [ "network-online.target" ];` does not help in this regard. + # Thus, `btrbk` might fail with the following error while connecting to ${backup-source}: + # ssh: Could not resolve hostname p1st.de: Name or service not known + # + while :; do + result="$(ssh ${backup-source} 'echo ${backup-source}')" && e=0 || e=$? + if [ "''${e}" = 0 ] && [ "''${result}" = '${backup-source}' ]; then + break + fi + printf '%s\n' 'Delaying backup due to SSH connectivity problems.' + sleep 10s + done + # Pull BTRFS snapshots from ${backup-source}. btrbk -c /etc/btrbk/remote-backup-ssd.conf run btrbk -c /etc/btrbk/remote-backup-hdd.conf run @@ -88,7 +101,7 @@ in printf '%s\n' 'Delaying suspend due to SSH connectivity problems.' ;; esac - sleep 10s + sleep 60s done # Wait until no BTRFS scrub service is running.