Files
connector-macos/configure.d/20-backup.sh
T
davidandClaude Opus 5.5 08e6b2ee4f Set up a Mac for a Federated Core, like CoreDesktop's configure
A macOS counterpart of CoreDesktop's configure, with the same drop-in
items in configure.d/:

- core: Mail, Calendar and Contacts through a configuration profile,
  Superpanel as an app, Nextcloud files through the Nextcloud app, and
  Element; with test-and-fix, password change and removal
- tailscale: join the Core's network through headscale
- backup: restic to Nextcloud through rclone, run by launchd through an
  app that can be given Full Disk Access
- storage: SMB shares mounted the way Finder does, again at login and
  on network changes
- agents, claude, opencode, hermes, sandbox: install the agents and run
  them in Anthropic's sandbox runtime (Seatbelt on macOS)
- ai-servers: LLM servers on this Mac or the network, for the agents

Apps and tools macOS lacks are offered for download, checked against
their makers' Team IDs and notarization, or published SHA-256 sums.
Everything runs on macOS' bash 3.2 with only built-in tools.

smoketest/ runs scenarios in fresh macOS VMs with Tart.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 08:34:42 -07:00

309 lines
11 KiB
Bash

#!/bin/bash
# configure reads TITLE and DESCRIPTION
# shellcheck disable=SC2034
TITLE="Backup"
# shellcheck disable=SC2034
DESCRIPTION="Back up your home folder daily to your Nextcloud files"
CORE_CONF="$CONF_DIR/core.conf"
BACKUP_CONF="$CONF_DIR/backup.conf"
ACCESS_CHECK="$CONF_DIR/backup-access-check"
NOW_REQUEST="$CONF_DIR/backup-now"
BACKUP_LOG="$HOME/Library/Logs/Federated Connector/backup.log"
BACKUP_KEYCHAIN="federated-connector-backup"
BACKUP_APP="$HOME/Applications/Federated Backup.app"
BACKUP_SCRIPT="$CONNECTOR/system/backup"
LABEL="computer.federated.connector.backup"
PLIST="$HOME/Library/LaunchAgents/$LABEL.plist"
FULL_DISK_ACCESS_PANE="x-apple.systempreferences:com.apple.preference.security?Privacy_AllFiles"
source "$CONNECTOR/configure.d/lib/json.sh"
source "$CONNECTOR/configure.d/lib/keychain.sh"
source "$CONNECTOR/configure.d/lib/nextcloud.sh"
source "$CONNECTOR/configure.d/lib/tools.sh"
[ -f "$CORE_CONF" ] && source "$CORE_CONF"
job_loaded() {
launchctl print "gui/$(id -u)/$LABEL" > /dev/null 2>&1
}
backups_on() {
[ -f "$BACKUP_CONF" ] || return 1
source "$CONNECTOR/system/backup-env.sh" > /dev/null || return 1
[ -n "$RESTIC_PASSWORD_COMMAND" ] && job_loaded
}
# Done when backing up to the main Core
is_configured() {
backups_on && [ "$BACKUP_WEBDAV" = "https://nextcloud.$CORE_DOMAIN/remote.php/dav/files/${CORE_EMAIL%@*}" ]
}
save_secret() {
keychain_save "$BACKUP_KEYCHAIN" "$1" "$2"
}
# The backup gets its own app password, so revoking it stops only backups
nextcloud_login() {
[ -n "$(backup_secret "nextcloud:$WEBDAV")" ] \
&& rclone lsf --max-depth 1 ":webdav:" > /dev/null 2>&1 && return 0
APP_PASSWORD=""
for _ in 1 2 3; do
read -rsp "Your Core password: " PASSWORD
echo
nextcloud_app_password "Federated Backup on $COMPUTER"
case $? in
0) break ;;
2) echo "Nextcloud says that password is wrong." ;;
*) echo "Couldn't reach Nextcloud at $NEXTCLOUD."; unset PASSWORD; return 1 ;;
esac
done
unset PASSWORD
[ -n "$APP_PASSWORD" ] || return 1
save_secret "nextcloud:$WEBDAV" "$APP_PASSWORD" || return 1
unset APP_PASSWORD
RCLONE_WEBDAV_PASS="$(backup_secret "nextcloud:$WEBDAV" | rclone obscure -)"
export RCLONE_WEBDAV_PASS
}
new_passphrase() {
local pass
pass="$(openssl rand -base64 24)"
echo "Your backup is encrypted with this passphrase:"
echo
echo " $pass"
echo
echo "Save it somewhere other than this Mac, such as Bitwarden. Without it, the backup"
echo "can't be read if this Mac is lost. It's kept in your keychain so the daily"
echo "backup can run."
until confirm "Have you saved it?"; do :; done
save_secret "passphrase:$WEBDAV/$BACKUP_DIR" "$pass"
}
# The app launchd starts, which runs system/backup. Full Disk Access belongs
# to this build of the app, so it's only made again if it runs another script
# (e.g. this folder moved), and then needs Full Disk Access again.
backup_app_current() {
[ "$(plutil -extract FederatedBackupScript raw -o - "$BACKUP_APP/Contents/Info.plist" 2> /dev/null)" \
= "$BACKUP_SCRIPT" ]
}
make_backup_app() {
local script
backup_app_current && return 0
# Quoted for an AppleScript string
script="${BACKUP_SCRIPT//\\/\\\\}"
script="${script//\"/\\\"}"
mkdir -p "$HOME/Applications"
rm -rf "$BACKUP_APP"
osacompile -o "$BACKUP_APP" \
-e 'on run' \
-e ' try' \
-e " do shell script \"/bin/bash \" & quoted form of \"$script\" & \" > /dev/null 2>&1\"" \
-e ' end try' \
-e 'end run' || return 1
# No Dock icon while it runs; a name of its own in Privacy & Security
plutil -replace LSUIElement -bool true "$BACKUP_APP/Contents/Info.plist" \
&& plutil -replace CFBundleIdentifier -string "$LABEL" "$BACKUP_APP/Contents/Info.plist" \
&& plutil -replace FederatedBackupScript -string "$BACKUP_SCRIPT" "$BACKUP_APP/Contents/Info.plist" \
&& codesign --force --sign - "$BACKUP_APP" > /dev/null 2>&1
}
# Start the app the way launchd does (as itself, not as part of Terminal),
# and have system/backup say whether it can read what Full Disk Access guards
full_disk_access() {
local answer
echo "?" > "$ACCESS_CHECK"
open -g -W "$BACKUP_APP" 2> /dev/null
answer="$(cat "$ACCESS_CHECK" 2> /dev/null)"
rm -f "$ACCESS_CHECK"
[ "$answer" = yes ]
}
grant_access() {
echo "Checking that Federated Backup can read all your files..."
full_disk_access && { echo "It can."; return 0; }
echo
echo "macOS keeps apps out of Desktop, Documents, Mail, Photos and other folders until"
echo "you let them in. To back them up, give Federated Backup Full Disk Access:"
echo "System Settings opens at Full Disk Access, and Finder shows the app. Drag it into"
echo "the list (or click + and choose it), and make sure it's turned on."
open "$FULL_DISK_ACCESS_PANE"
open -R "$BACKUP_APP"
if wait_until 600 full_disk_access; then
echo "Federated Backup can read all your files."
else
echo "Without Full Disk Access, backups leave out the folders macOS guards, and"
echo "you get a notification each time. To give it later: configure backup"
fi
}
# Hourly and at login; system/backup backs up once a day. Background
# priority keeps it out of the way of what you're doing.
install_job() {
mkdir -p "$(dirname "$PLIST")"
cat > "$PLIST" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key><string>$LABEL</string>
<key>ProgramArguments</key>
<array>
<string>$(xml_escape "$BACKUP_APP/Contents/MacOS/applet")</string>
</array>
<key>RunAtLoad</key><true/>
<key>StartInterval</key><integer>3600</integer>
<key>ProcessType</key><string>Background</string>
<key>LowPriorityIO</key><true/>
<key>Nice</key><integer>19</integer>
</dict>
</plist>
EOF
launchctl bootout "gui/$(id -u)/$LABEL" 2> /dev/null
launchctl bootstrap "gui/$(id -u)" "$PLIST"
}
xml_escape() {
local s="$1"
s="${s//&/\&amp;}"
s="${s//</\&lt;}"
s="${s//>/\&gt;}"
printf '%s' "$s"
}
# Stop the daily backup and remove the app and the job
stop_backups() {
launchctl bootout "gui/$(id -u)/$LABEL" 2> /dev/null
rm -f "$PLIST"
rm -rf "$BACKUP_APP"
rm -f "$CONF_DIR/backup-last"
}
# Delete the backup from Nextcloud, then what's on this Mac for it: without
# the backup, the passphrase and app password are no use
delete_backups() {
local app_password
nextcloud_login || { echo "Couldn't sign in to Nextcloud."; return 1; }
echo "Deleting $BACKUP_DIR from your Nextcloud files..."
rclone purge ":webdav:$BACKUP_DIR" \
|| { echo "rclone couldn't delete $BACKUP_DIR."; return 1; }
echo "Deleted. Nextcloud keeps it in its trash bin, where it still uses space, until you empty it."
app_password="$(backup_secret "nextcloud:$WEBDAV")"
[ -n "$app_password" ] && nextcloud_revoke_app_password "$app_password"
keychain_delete "$BACKUP_KEYCHAIN" "nextcloud:$WEBDAV"
keychain_delete "$BACKUP_KEYCHAIN" "passphrase:$WEBDAV/$BACKUP_DIR"
rm -f "$BACKUP_CONF"
}
turn_off() {
local answer
WEBDAV="$BACKUP_WEBDAV"
CORE_USER="$BACKUP_USER"
NEXTCLOUD="${BACKUP_WEBDAV%%/remote.php/*}"
stop_backups
echo "Backups are off."
echo
echo "The backups made so far are still in $BACKUP_DIR in your Nextcloud files."
if ! confirm "Delete them? They can't be restored from after that."; then
echo "Kept them. To turn backups back on: configure backup"
return 0
fi
read -rp "To delete them, type the folder name ($BACKUP_DIR): " answer
if [ "$answer" != "$BACKUP_DIR" ]; then
echo "That's not the folder name, so the backups were kept."
return 0
fi
delete_backups
}
run_configure() {
local pass choice host
if backups_on &> /dev/null; then
if is_configured &> /dev/null; then
echo "Your home folder is backed up daily to $BACKUP_DIR in your Nextcloud files."
echo "The last backups are in: $BACKUP_LOG"
echo
echo " 1) Set it up again"
else
echo "Your home folder is backed up daily to $BACKUP_DIR on ${BACKUP_WEBDAV%%/remote.php/*},"
echo "which isn't your main Core."
echo
echo " 1) Back up to your main Core ($CORE_DOMAIN) instead; the backups so far stay where they are"
fi
echo " 2) Turn backups off"
echo " 3) Back up now"
echo " q) Leave it as it is"
echo
read -rp "Choose: " choice
case "$choice" in
1) ;;
2) turn_off; return ;;
3) touch "$NOW_REQUEST"
launchctl kickstart "gui/$(id -u)/$LABEL"
echo "Started. To follow it: tail -f \"$BACKUP_LOG\""
return 0 ;;
*) return 0 ;;
esac
fi
if [ -z "$CORE_DOMAIN" ] || [ -z "$CORE_EMAIL" ]; then
echo "Backups go to your Nextcloud files, so set up your Core first: configure core"
return 1
fi
tool_ensure restic rclone || return 1
CORE_USER="${CORE_EMAIL%@*}"
NEXTCLOUD="https://nextcloud.$CORE_DOMAIN"
WEBDAV="$NEXTCLOUD/remote.php/dav/files/$CORE_USER"
host="$(scutil --get LocalHostName 2> /dev/null || hostname -s)"
BACKUP_DIR=".backup-$host"
mkdir -p "$(dirname "$BACKUP_CONF")"
{
printf 'BACKUP_WEBDAV=%q\n' "$WEBDAV"
printf 'BACKUP_USER=%q\n' "$CORE_USER"
printf 'BACKUP_DIR=%q\n' "$BACKUP_DIR"
printf 'BACKUP_HOST=%q\n' "$host"
} > "$BACKUP_CONF"
source "$CONNECTOR/system/backup-env.sh"
nextcloud_login || { echo "Couldn't sign in to Nextcloud."; return 1; }
if rclone lsf --files-only ":webdav:$BACKUP_DIR" 2> /dev/null | grep -qx config; then
# Already there, e.g. after reinstalling macOS: it needs the passphrase from then
if [ -z "$RESTIC_PASSWORD_COMMAND" ] || ! restic cat config &> /dev/null; then
echo "There's already a backup in $BACKUP_DIR in your Nextcloud files."
read -rsp "Its passphrase: " pass
echo
RESTIC_PASSWORD_COMMAND='' RESTIC_PASSWORD="$pass" restic cat config > /dev/null \
|| { echo "restic couldn't open it with that passphrase."; return 1; }
save_secret "passphrase:$WEBDAV/$BACKUP_DIR" "$pass" || return 1
fi
echo "Using the backup in $BACKUP_DIR."
else
new_passphrase || return 1
source "$CONNECTOR/system/backup-env.sh"
echo "Creating the backup in $BACKUP_DIR in your Nextcloud files..."
restic init || return 1
fi
make_backup_app || { echo "Couldn't make the Federated Backup app."; return 1; }
grant_access
# launchd starts the app as soon as the job is added; the first backup
# waits for a day unless you start it below
[ -f "$CONF_DIR/backup-last" ] || touch "$CONF_DIR/backup-last"
install_job || return 1
echo "Your home folder will be backed up daily to $BACKUP_DIR in your Nextcloud files."
echo "(macOS may say a background item was added: that's this. If it's turned off in"
echo "System Settings > General > Login Items, backups don't run.)"
if confirm_yes "Start the first backup now? It runs in the background and can take hours."; then
touch "$NOW_REQUEST"
launchctl kickstart "gui/$(id -u)/$LABEL"
echo "Started. To follow it: tail -f \"$BACKUP_LOG\""
fi
}