# shellcheck shell=bash DESCRIPTION="Backup end to end: restic and rclone, the app, launchd, the keychain, a snapshot and a restore" TIER=quick source "$CONNECTOR/configure.d/20-backup.sh" DAV="$HOME/davroot" SMOKE_FILE="$HOME/Documents/smoke-file.txt" ran() { grep -qE 'Backup (finished|failed)' "$BACKUP_LOG" 2> /dev/null } finished() { grep -q 'Backup finished' "$BACKUP_LOG" || { tail -n 30 "$BACKUP_LOG"; return 1; } } has_snapshot() { [ "$(restic snapshots --host smoketest --json | json_get 'd.length')" -ge 1 ] } restores() { local target="$TMPDIR/restored" rm -rf "$target" restic restore latest --host smoketest --target "$target" --include "$SMOKE_FILE" > /dev/null \ && cmp "$SMOKE_FILE" "$target$SMOKE_FILE" } access_answer() { echo "?" > "$ACCESS_CHECK" open -g -W "$BACKUP_APP" cat "$ACCESS_CHECK" rm -f "$ACCESS_CHECK" } scenario() { local pass answer lines DAV_SERVER step "Installing restic and rclone" check "tool_ensure restic rclone" tool_ensure restic rclone export PATH="$TOOLS_PATH:$PATH" # A plain WebDAV server stands in for Nextcloud. Without a password, so the # backup's check that Nextcloud is up (status.php) answers. mkdir -p "$DAV" "$(dirname "$SMOKE_FILE")" echo ok > "$DAV/status.php" rclone serve webdav "$DAV" --addr 127.0.0.1:8081 > /dev/null 2>&1 & DAV_SERVER=$! sleep 3 date > "$SMOKE_FILE" WEBDAV=http://127.0.0.1:8081 BACKUP_DIR=.backup-smoketest mkdir -p "$CONF_DIR" { printf 'BACKUP_WEBDAV=%q\n' "$WEBDAV" printf 'BACKUP_USER=tester\nBACKUP_DIR=%q\nBACKUP_HOST=smoketest\nBACKUP_VENDOR=other\n' "$BACKUP_DIR" } > "$BACKUP_CONF" echo "$DAV" > "$CONF_DIR/backup-exclude" pass="$(openssl rand -base64 24)" check "the app password goes in the keychain" keychain_save "$BACKUP_KEYCHAIN" "nextcloud:$WEBDAV" smokepass check "the passphrase goes in the keychain" keychain_save "$BACKUP_KEYCHAIN" "passphrase:$WEBDAV/$BACKUP_DIR" "$pass" source "$CONNECTOR/system/backup-env.sh" check "backup-env.sh finds the passphrase" test -n "$RESTIC_PASSWORD_COMMAND" check "restic init, through rclone" restic init step "The Federated Backup app and its launchd job" check "make_backup_app" make_backup_app check "the app knows which script it runs" backup_app_current check "its signature checks out" codesign --verify --deep --strict "$BACKUP_APP" answer="$(access_answer)" check "started as itself, it says whether it has Full Disk Access" test "$answer" = yes -o "$answer" = no [ "$answer" = no ] && skip "Full Disk Access needs a click in System Settings; this backup leaves protected folders out" touch "$CONF_DIR/backup-last" check "install_job" install_job check "launchd has the job" job_loaded step "A backup, started the way configure does" touch "$NOW_REQUEST" launchctl kickstart "gui/$(id -u)/$LABEL" wait_until 900 ran > /dev/null check "it finished" finished check "restic has a snapshot from this Mac" has_snapshot check "a file comes back from it" restores lines="$(wc -l < "$BACKUP_LOG")" launchctl kickstart "gui/$(id -u)/$LABEL" sleep 15 check_eq "started again within the day, it does nothing" "$lines" "$(wc -l < "$BACKUP_LOG")" step "Turning backups off" stop_backups check_not "launchd no longer has the job" job_loaded check_not "the app is gone" test -e "$BACKUP_APP" kill "$DAV_SERVER" 2> /dev/null }