Files
David YoungandClaude Opus 5.5 ad97e68db1 Let sandboxed agents use mktemp, and keep backups from waiting on prompts
macOS' mktemp uses the per-user temp folder in /var/folders, not TMPDIR,
so the sandbox now allows writing there. The sandbox's settings move out
of it, to Application Support, which the agent can't read or write.

Without Full Disk Access, reading Desktop, Documents, Downloads, other
apps' containers or Photos makes macOS ask, and restic waited for an
answer no one gives at night. The backup now leaves those out until it
has Full Disk Access, as configure backup already said it did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 16:19:21 -07:00

94 lines
3.4 KiB
Bash

# 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"
# Outside Documents, which a backup without Full Disk Access leaves out
SMOKE_FILE="$HOME/Projects/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
}