+ die "error: no btrbk binary found"
+fi
+# if our mountpoints are from stale snapshots,
+# it doesn't make sense to do a backup.
+check-subvol-stale ${mountpoints[@]} || die "found stale mountpoints in ${mountpoints[*]}"
+
+# for an initial run, btrbk requires the dir to exist.
+mkdir -p /mnt/root/btrbk
+local_zone=$(date +%z)
+
+if [[ $source ]]; then
+ if ! zone=$(ssh root@$source date +%z); then
+ die failed to ssh to root@$source
+ fi
+ if [[ $zone != $local_zone ]]; then
+ die "error: dont confuse yourself with multiple time zones. $h has different timezone than localhost"
+ fi
+
+else
+
+ sshable=()
+ sshfail=()
+ min_idle_ms=$((1000 * 60 * 15))
+ for h in ${targets[@]}; do
+ if zone=$(ssh root@$h "mkdir -p /mnt/root/btrbk && date +%z"); then
+ # This is a separate ssh because xprintidle can fail and thats ok.
+ if $cron && idle_ms=$(ssh $h DISPLAY=:0 xprintidle); then
+ if (( idle_ms < min_idle_ms )); then
+
+ # Ignore this host. i sometimes use a non-main machine for
+ # testing or web browsing, knowing that everything will be wiped
+ # by the next backup, but I dont want it to happen as Im using
+ # it from cronjob.
+ e "warning: $h: active X session in the last 15 minutes, skipping for now"
+ continue
+ fi
+ fi
+ sshable+=($h)
+ if [[ $zone != $local_zone ]]; then
+ die "error: dont confuse yourself with multiple time zones. $h has different timezone than localhost"
+ fi
+ else
+ sshfail+=($h)
+ fi
+ done
+ if [[ ! ${sshable[@]} ]] || { ! $cron && [[ ${sshfail[@]} ]]; }; then
+ die "failed to ssh to hosts: ${sshfail[*]}"
+ else
+ if [[ ${sshfail[@]} ]]; then
+ ret=1
+ e "error: failed to ssh to ${sshfail[*]} but continuing with other hosts"
+ fi
+ targets=(${sshable[@]})
+ fi