brave-backup/brave_debug.sh
2025-10-19 00:58:37 +02:00

112 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Brave Browser configuration backup & diff tool (jq-enhanced)
#
# Usage:
# brave_backup.sh backup [prefix]
# brave_backup.sh diff <prefix>
#
set -euo pipefail
# --- CONFIG ---
BRAVE_DIR="$HOME/Library/Application Support/BraveSoftware/Brave-Browser"
FILES=(
"Local State"
"Default/Preferences"
"Default/Secure Preferences"
)
BACKUP_DIR="/tmp/brave_backups"
# --- FUNCTIONS ---
jq_pretty() {
local src="$1"
local dest="$2"
if jq -e . "$src" >/dev/null 2>&1; then
jq --sort-keys . "$src" >"$dest"
echo " (jq formatted)"
return 0
else
cp "$src" "$dest"
echo " (raw copy - not valid JSON)"
return 1
fi
}
backup_files() {
local prefix="${1:-$(date +%Y%m%d_%H%M%S)}"
local target_dir="${BACKUP_DIR}/${prefix}"
mkdir -p "$target_dir"
echo "🔒 Backing up Brave files to: $target_dir"
for f in "${FILES[@]}"; do
local src="${BRAVE_DIR}/${f}"
local dest="${target_dir}/$(basename "$f")"
if [[ -f "$src" ]]; then
jq_pretty "$src" "$dest"
echo "✅ Backed up (pretty JSON): $f$dest"
else
echo "⚠️ Missing: $src"
fi
done
echo "✅ Backup complete: $target_dir"
}
diff_files() {
local prefix="${1:-}"
if [[ -z "$prefix" ]]; then
echo "❌ Please provide a prefix for diff comparison."
exit 1
fi
local backup_dir="${BACKUP_DIR}/${prefix}"
if [[ ! -d "$backup_dir" ]]; then
echo "❌ Backup not found: $backup_dir"
exit 1
fi
echo "🔍 Comparing current files with backup: $backup_dir"
for f in "${FILES[@]}"; do
local current="${BRAVE_DIR}/${f}"
local backup="${backup_dir}/$(basename "$f")"
if [[ -f "$current" && -f "$backup" ]]; then
echo
echo "=== DIFF for $(basename "$f") ==="
local tmp_current=$(mktemp)
# Try pretty-print; if fails, fall back to raw copy
if ! jq_pretty "$current" "$tmp_current" >/dev/null; then
cp "$current" "$tmp_current"
fi
diff -u "$backup" "$tmp_current" || true
rm -f "$tmp_current"
else
echo "⚠️ Missing file in current or backup for: $(basename "$f")"
fi
done
}
# --- MAIN ---
case "${1:-}" in
backup)
backup_files "${2:-}"
;;
diff)
diff_files "${2:-}"
;;
*)
echo "Usage:"
echo " $0 backup [prefix] # Backup Brave config files (prettified JSON)"
echo " $0 diff <prefix> # Compare with a specific backup"
exit 1
;;
esac