aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/lib_backup.sh
blob: b6f54f6ae92f15dddcc1ff8e9d3cb23f10b4647a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env bash
# ==================================================
#  KoolDots (2026)
#  Project URL: https://github.com/LinuxBeginnings
#  License: GNU GPLv3
#  SPDX-License-Identifier: GPL-3.0-or-later
# ==================================================
# 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() {
  if [ -n "${BACKUP_DIR:-}" ]; then
    echo "$BACKUP_DIR"
    return
  fi
  BACKUP_DIR="back-up_$(date +"%m%d_%H%M")"
  export BACKUP_DIR
  echo "$BACKUP_DIR"
}

# 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
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage