#!/usr/bin/env bash set -Eeuo pipefail umask 077 APP_NAME="nextcloud-borg-backup" ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" BIN_DIR="${HOME}/.local/bin" CONFIG_DIR="${XDG_CONFIG_HOME:-${HOME}/.config}/${APP_NAME}" STATE_DIR="${XDG_STATE_HOME:-${HOME}/.local/state}/${APP_NAME}" SYSTEMD_USER_DIR="${XDG_CONFIG_HOME:-${HOME}/.config}/systemd/user" mkdir -p "$BIN_DIR" "$CONFIG_DIR" "$STATE_DIR/logs" "$SYSTEMD_USER_DIR" install -m 0750 "$ROOT_DIR/scripts/nextcloud-borg-backup" "$BIN_DIR/nextcloud-borg-backup" if [[ ! -f "$CONFIG_DIR/config" ]]; then install -m 0600 "$ROOT_DIR/etc/config.example" "$CONFIG_DIR/config" printf 'Installed config: %s\n' "$CONFIG_DIR/config" else printf 'Config exists, not overwriting: %s\n' "$CONFIG_DIR/config" fi if [[ ! -f "$CONFIG_DIR/excludes" ]]; then install -m 0600 "$ROOT_DIR/etc/excludes.example" "$CONFIG_DIR/excludes" fi if [[ ! -f "$CONFIG_DIR/passphrase" ]]; then if command -v openssl >/dev/null 2>&1; then openssl rand -base64 48 > "$CONFIG_DIR/passphrase" else od -An -tx1 -N48 /dev/urandom | tr -d ' \n' > "$CONFIG_DIR/passphrase" printf '\n' >> "$CONFIG_DIR/passphrase" fi chmod 0600 "$CONFIG_DIR/passphrase" printf 'Created Borg passphrase file: %s\n' "$CONFIG_DIR/passphrase" printf 'Store a copy of this passphrase somewhere safe for disaster recovery.\n' fi install -m 0644 "$ROOT_DIR/systemd/user/nextcloud-borg-backup.service" "$SYSTEMD_USER_DIR/nextcloud-borg-backup.service" install -m 0644 "$ROOT_DIR/systemd/user/nextcloud-borg-backup.timer" "$SYSTEMD_USER_DIR/nextcloud-borg-backup.timer" systemctl --user daemon-reload systemctl --user enable --now nextcloud-borg-backup.timer printf '\nInstalled %s.\n' "$APP_NAME" printf 'Next steps:\n' printf ' 1. Check config: %s\n' "$CONFIG_DIR/config" printf ' 2. Run first backup: systemctl --user start nextcloud-borg-backup.service\n' printf ' 3. Watch logs: journalctl --user -u nextcloud-borg-backup.service -f\n'