diff options
| author | Don Williams <don.e.williams@gmail.com> | 2026-01-11 02:17:07 -0500 |
|---|---|---|
| committer | Don Williams <don.e.williams@gmail.com> | 2026-01-11 02:17:07 -0500 |
| commit | 2f09b78e20c68c60e171f71979bae3551c51eb08 (patch) | |
| tree | 496fe99e7ed70f1a58721561d3598ab2313f9dc8 /scripts | |
| parent | bd736891d8c30ce8d42b6a3586567b9f826ec153 (diff) | |
Phase 2a of copy.sh modularization lib_backup functions
This module takes care of backing up objects with date-time
Removes the duplicate code in copy.sh
On branch development
Your branch is up to date with 'origin/development'.
Changes to be committed:
modified: copy.sh
new file: scripts/lib_backup.sh
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/lib_backup.sh | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/scripts/lib_backup.sh b/scripts/lib_backup.sh new file mode 100644 index 00000000..6867fb6d --- /dev/null +++ b/scripts/lib_backup.sh @@ -0,0 +1,72 @@ +#!/usr/bin/env bash +# Backup helper utilities shared by copy.sh (and future scripts). + +# Create a unique backup directory name with month, day, hours, and minutes. +get_backup_dirname() { + echo "back-up_$(date +"%m%d_%H%M")" +} + +# Move a directory to a timestamped backup alongside the original. +# Usage: backup_dir "/path/to/dir" [logfile] +backup_dir() { + local dir="$1" + local log="${2:-/dev/null}" + local backup_suffix + + [ -z "$dir" ] && return 1 + backup_suffix=$(get_backup_dirname) + mv "$dir" "${dir}-backup-${backup_suffix}" 2>&1 | tee -a "$log" +} + +# Cleanup old backups under ~/.config, keeping the newest for each base dir. +# mode: "auto" (no prompts) or "prompt" (asks before delete); log optional. +cleanup_backups() { + local mode="${1:-prompt}" + local log="${2:-/dev/null}" + local CONFIG_DIR="$HOME/.config" + local BACKUP_PREFIX="-backup" + + for DIR in "$CONFIG_DIR"/*; do + [ -d "$DIR" ] || continue + local BACKUP_DIRS=() + + for BACKUP in "$DIR"$BACKUP_PREFIX*; do + [ -d "$BACKUP" ] && BACKUP_DIRS+=("$BACKUP") + done + + [ ${#BACKUP_DIRS[@]} -gt 1 ] || continue + + # Determine latest backup by mtime + local latest_backup="${BACKUP_DIRS[0]}" + for BACKUP in "${BACKUP_DIRS[@]}"; do + [ "$BACKUP" -nt "$latest_backup" ] && latest_backup="$BACKUP" + done + + if [ "$mode" = "auto" ]; then + for BACKUP in "${BACKUP_DIRS[@]}"; do + if [ "$BACKUP" != "$latest_backup" ]; then + rm -rf "$BACKUP" + fi + done + echo "${INFO:-[INFO]} Express mode: trimmed backups for ${YELLOW:-}${DIR##*/}${RESET:-}, keeping ${MAGENTA:-}${latest_backup##*/}${RESET:-}." 2>&1 | tee -a "$log" + continue + fi + + printf "\n%s Found multiple backups for: %s\n" "${INFO:-[INFO]}" "${DIR##*/}" + echo "${YELLOW:-}Backups:${RESET:-}" + for BACKUP in "${BACKUP_DIRS[@]}"; do + echo " - ${BACKUP##*/}" + done + echo -n "${CAT:-[ACTION]} Delete older backups and keep only the latest? (y/N): " + read back_choice + if [[ "$back_choice" == [Yy]* ]]; then + for BACKUP in "${BACKUP_DIRS[@]}"; do + if [ "$BACKUP" != "$latest_backup" ]; then + rm -rf "$BACKUP" + echo "Deleted: ${BACKUP##*/}" + fi + done + echo "Kept: ${latest_backup##*/}" + fi + done +} |
