feat: add automated konsolerc geometry sanitization and git backup integration
- Implement [MainWindow] section stripping in backup and restore scripts to prevent screen geometry/monitor layout noise across devices - Add robust hostname detection and customizable archive naming in backup.sh - Add optional interactive git commit and push prompt upon successful backup - Update restore.sh diff preview and extraction to respect sanitized konsolerc - Add AGENTS.md documentation detailing script architecture and conventions
This commit is contained in:
+60
-12
@@ -4,6 +4,20 @@ set -euo pipefail
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
BACKUP_DIR="${SCRIPT_DIR}/backups"
|
||||
|
||||
# Function to sanitize a konsolerc file by stripping the [MainWindow] section
|
||||
sanitize_konsolerc() {
|
||||
local target_file="$1"
|
||||
if [ -f "${target_file}" ]; then
|
||||
local cleaned_file="${target_file}.clean"
|
||||
awk '
|
||||
/^\[MainWindow\]/ { skip=1; next }
|
||||
/^\[.*\]/ { skip=0 }
|
||||
!skip { print }
|
||||
' "${target_file}" > "${cleaned_file}"
|
||||
mv "${cleaned_file}" "${target_file}"
|
||||
fi
|
||||
}
|
||||
|
||||
show_diff() {
|
||||
local source_dir="$1"
|
||||
echo ""
|
||||
@@ -22,13 +36,36 @@ show_diff() {
|
||||
changes_found=1
|
||||
echo -e "\n\033[1;32m[NEW FILE]\033[0m ~/${rel_path}"
|
||||
else
|
||||
if ! cmp -s "${item}" "${dest_item}"; then
|
||||
changes_found=1
|
||||
echo -e "\n\033[1;33m[MODIFIED]\033[0m ~/${rel_path}"
|
||||
if command -v git &>/dev/null; then
|
||||
git diff --no-index --color "${dest_item}" "${item}" || true
|
||||
else
|
||||
diff -u --color=always "${dest_item}" "${item}" || true
|
||||
if [ "${rel_path}" = ".config/konsolerc" ]; then
|
||||
# Compare against sanitized version of local konsolerc so [MainWindow] geometry noise is ignored
|
||||
local temp_local_clean
|
||||
temp_local_clean="$(mktemp)"
|
||||
sanitize_konsolerc "${dest_item}" 2>/dev/null || true
|
||||
awk '
|
||||
/^\[MainWindow\]/ { skip=1; next }
|
||||
/^\[.*\]/ { skip=0 }
|
||||
!skip { print }
|
||||
' "${dest_item}" > "${temp_local_clean}"
|
||||
|
||||
if ! cmp -s "${item}" "${temp_local_clean}"; then
|
||||
changes_found=1
|
||||
echo -e "\n\033[1;33m[MODIFIED]\033[0m ~/${rel_path}"
|
||||
if command -v git &>/dev/null; then
|
||||
git diff --no-index --color "${temp_local_clean}" "${item}" || true
|
||||
else
|
||||
diff -u --color=always "${temp_local_clean}" "${item}" || true
|
||||
fi
|
||||
fi
|
||||
rm -f "${temp_local_clean}"
|
||||
else
|
||||
if ! cmp -s "${item}" "${dest_item}"; then
|
||||
changes_found=1
|
||||
echo -e "\n\033[1;33m[MODIFIED]\033[0m ~/${rel_path}"
|
||||
if command -v git &>/dev/null; then
|
||||
git diff --no-index --color "${dest_item}" "${item}" || true
|
||||
else
|
||||
diff -u --color=always "${dest_item}" "${item}" || true
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
@@ -71,7 +108,13 @@ if [ -z "${ARCHIVE_PATH}" ]; then
|
||||
done
|
||||
echo " [q] Quit"
|
||||
|
||||
read -r -p "Select a backup to restore [1-${#AVAILABLE_BACKUPS[@]}]: " choice < /dev/tty
|
||||
if [ -t 0 ]; then
|
||||
read -r -p "Select a backup to restore [1-${#AVAILABLE_BACKUPS[@]}]: " choice || true
|
||||
elif [ -c /dev/tty ] && [ -r /dev/tty ] && { : < /dev/tty; } 2>/dev/null; then
|
||||
read -r -p "Select a backup to restore [1-${#AVAILABLE_BACKUPS[@]}]: " choice < /dev/tty || true
|
||||
else
|
||||
read -r -p "Select a backup to restore [1-${#AVAILABLE_BACKUPS[@]}]: " choice || true
|
||||
fi
|
||||
if [[ "${choice}" =~ ^[0-9]+$ ]] && [ "${choice}" -ge 1 ] && [ "${choice}" -le "${#AVAILABLE_BACKUPS[@]}" ]; then
|
||||
ARCHIVE_PATH="${AVAILABLE_BACKUPS[$((choice - 1))]}"
|
||||
else
|
||||
@@ -102,6 +145,11 @@ trap 'rm -rf "${TEMP_DIR}"' EXIT
|
||||
|
||||
unzip -q -o "${ARCHIVE_PATH}" -d "${TEMP_DIR}"
|
||||
|
||||
# Extra safety: Sanitize extracted konsolerc in case restoring an older archive
|
||||
if [ -f "${TEMP_DIR}/.config/konsolerc" ]; then
|
||||
sanitize_konsolerc "${TEMP_DIR}/.config/konsolerc"
|
||||
fi
|
||||
|
||||
# Interactive confirmation loop (allows viewing diffs)
|
||||
while true; do
|
||||
echo ""
|
||||
@@ -113,13 +161,13 @@ while true; do
|
||||
echo ""
|
||||
|
||||
if [ -t 0 ]; then
|
||||
INPUT_SRC="/dev/stdin"
|
||||
read -r -p "Choose action: [r] Restore / [d] View Diff / [c] Cancel: " action || true
|
||||
elif [ -c /dev/tty ] && [ -r /dev/tty ] && { : < /dev/tty; } 2>/dev/null; then
|
||||
read -r -p "Choose action: [r] Restore / [d] View Diff / [c] Cancel: " action < /dev/tty || true
|
||||
else
|
||||
INPUT_SRC="/dev/tty"
|
||||
read -r -p "Choose action: [r] Restore / [d] View Diff / [c] Cancel: " action || true
|
||||
fi
|
||||
|
||||
read -r -p "Choose action: [r] Restore / [d] View Diff / [c] Cancel: " action < "${INPUT_SRC}"
|
||||
|
||||
case "${action,,}" in
|
||||
r|y|yes)
|
||||
echo "Restoring configuration files..."
|
||||
|
||||
Reference in New Issue
Block a user