Script cleanup

This commit is contained in:
David Tomaschik
2026-07-06 15:59:12 -07:00
parent 22b3f2e049
commit da1ee162c2
13 changed files with 225 additions and 111 deletions

View File

@@ -12,8 +12,8 @@ set -o pipefail
# For this script, we assume the user's home directory.
SOURCE_DIR="${HOME}"
# Exclude file location. We'll create a default one next to the script.
EXCLUDE_FILE="$HOME/.restic_exclude.darwin"
# Exclude file location.
EXCLUDE_FILE="${HOME}/.restic_exclude.darwin"
# --- Functions ---
usage() {
@@ -34,7 +34,7 @@ EOF
# --- Main Script ---
# Check if restic is installed
if ! command -v restic &> /dev/null; then
if ! command -v restic >/dev/null 2>&1; then
echo "Error: restic command not found." >&2
echo "Please install restic first: https://restic.net/" >&2
exit 1
@@ -57,7 +57,7 @@ while getopts ":l:bu:h" opt; do
;;
b)
BACKUP_MODE="b2"
if [[ ${OPTIND} -le $# && "${!OPTIND}" != -* ]]; then
if [[ ${OPTIND} -le $# && "${!OPTIND:-}" != -* ]]; then
REPO="b2:${!OPTIND}:"
OPTIND=$((OPTIND + 1))
fi
@@ -84,70 +84,86 @@ done
# --- Pre-run checks ---
if [[ -z "${BACKUP_MODE}" ]]; then
echo "Error: You must specify a backup mode (-l or -b)." >&2
usage
exit 1
echo "Error: You must specify a backup mode (-l or -b)." >&2
usage
exit 1
fi
if [[ "${BACKUP_MODE}" == "b2" ]]; then
if [[ -f "${HOME}/.resticb2" ]] ; then
. "${HOME}/.resticb2"
fi
export B2_ACCOUNT_ID
export B2_ACCOUNT_KEY
export B2_BUCKET_NAME
if [[ -z "${B2_ACCOUNT_ID:-}" || -z "${B2_ACCOUNT_KEY:-}" ]]; then
echo "Error: For Backblaze B2 backups, you must set the B2_ACCOUNT_ID and B2_ACCOUNT_KEY environment variables." >&2
exit 1
fi
if [[ -f "${HOME}/.resticb2" ]] ; then
# shellcheck disable=SC1090
. "${HOME}/.resticb2"
fi
export B2_ACCOUNT_ID="${B2_ACCOUNT_ID:-}"
export B2_ACCOUNT_KEY="${B2_ACCOUNT_KEY:-}"
export B2_BUCKET_NAME="${B2_BUCKET_NAME:-}"
if [[ -z "${B2_ACCOUNT_ID}" || -z "${B2_ACCOUNT_KEY}" ]]; then
echo "Error: For Backblaze B2 backups, you must set the B2_ACCOUNT_ID and B2_ACCOUNT_KEY environment variables." >&2
exit 1
fi
if [[ -z "${REPO:-}" ]]; then
if [[ -n "${B2_BUCKET_NAME:-}" ]]; then
REPO="b2:${B2_BUCKET_NAME}:"
else
echo "Error: Backup mode is B2 but no bucket name was provided and the B2_BUCKET_NAME environment variable is not set." >&2
usage
exit 1
fi
if [[ -z "${REPO}" ]]; then
if [[ -n "${B2_BUCKET_NAME}" ]]; then
REPO="b2:${B2_BUCKET_NAME}:"
else
echo "Error: Backup mode is B2 but no bucket name was provided and the B2_BUCKET_NAME environment variable is not set." >&2
usage
exit 1
fi
fi
fi
KEYCHAIN_ENTRY_NAME="restic_repo_password"
if security find-generic-password -a "$(whoami)" -s "${KEYCHAIN_ENTRY_NAME}" >/dev/null 2>&1 ; then
export RESTIC_PASSWORD_COMMAND="security find-generic-password -a \"$(whoami)\" -s \"${KEYCHAIN_ENTRY_NAME}\" -w"
local_user="$(id -un 2>/dev/null || whoami)"
if security find-generic-password -a "${local_user}" -s "${KEYCHAIN_ENTRY_NAME}" >/dev/null 2>&1 ; then
printf -v RESTIC_PASSWORD_COMMAND 'security find-generic-password -a %q -s %q -w' "${local_user}" "${KEYCHAIN_ENTRY_NAME}"
export RESTIC_PASSWORD_COMMAND
# Source file?
elif [[ -f "${HOME}/.resticpass" ]] ; then
export RESTIC_PASSWORD_FILE="${HOME}/.resticpass"
fi
# Ensure exclude file exists so restic does not fail when checking exclusions.
if [[ ! -f "${EXCLUDE_FILE}" ]]; then
echo "Exclude file '${EXCLUDE_FILE}' not found. Creating a default macOS exclude file..."
cat << 'EOF' > "${EXCLUDE_FILE}"
# Default restic exclusions for macOS
.Trash
Library/Caches
Library/Logs
.CFUserTextEncoding
EOF
fi
# If the repository does not exist, initialize it.
# The user will be prompted for a password, which will be required for all
# future interactions with the repository.
if ! restic -r "${REPO}" snapshots &> /dev/null; then
echo "Restic repository not found or not accessible. Initializing..."
restic init -r "${REPO}"
if ! restic -r "${REPO}" snapshots >/dev/null 2>&1; then
echo "Restic repository not found or not accessible. Initializing..."
restic init -r "${REPO}"
fi
# --- Run Backup ---
echo "Starting restic backup..."
echo "Source: ${SOURCE_DIR}"
echo "Repository: ${REPO}"
BACKUP_CMD="restic backup \
--verbose \
--repo \"${REPO}\" \
--exclude-file \"${EXCLUDE_FILE}\" \
--one-file-system \
--tag \"macbook-backup\""
BACKUP_ARGS=(
"restic" "backup"
"--verbose"
"--repo" "${REPO}"
"--exclude-file" "${EXCLUDE_FILE}"
"--one-file-system"
"--tag" "macbook-backup"
)
if [[ -n "${UPLOAD_LIMIT}" ]]; then
BACKUP_CMD="${BACKUP_CMD} --limit-upload ${UPLOAD_LIMIT}"
BACKUP_ARGS+=("--limit-upload" "${UPLOAD_LIMIT}")
fi
BACKUP_CMD="${BACKUP_CMD} \"${SOURCE_DIR}\""
BACKUP_ARGS+=("${SOURCE_DIR}")
eval "${BACKUP_CMD}"
"${BACKUP_ARGS[@]}"
echo "Backup complete."
@@ -155,11 +171,11 @@ echo "Backup complete."
# Keeps the last 7 daily, 4 weekly, and 6 monthly snapshots.
echo "Pruning old snapshots..."
restic forget \
--repo "${REPO}" \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 6 \
--prune
--repo "${REPO}" \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 6 \
--prune
echo "Pruning complete."
echo "Restic backup script finished."