-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate
More file actions
executable file
·95 lines (80 loc) · 3.94 KB
/
update
File metadata and controls
executable file
·95 lines (80 loc) · 3.94 KB
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
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env bash
#
# _ _
# _ _ _ __ __| | __ _| |_ ___
# | | | | '_ \ / _` |/ _` | __/ _ \
# | |_| | |_) | (_| | (_| | || __/
# \__,_| .__/ \__,_|\__,_|\__\___|
# |_|
#
# Script : update
# Location : /usr/local/bin/update
# Author : Jens Ochmann
# Date : 2025‑04‑20
# License : MIT
# Description : System update & clean‑up with optional reboot.
# Requires root : Elevates via sudo if needed
#
set -euo pipefail
#–––––––––––– Farben (Fallback) ––––––––––––––––––––––––––––––––––––––––––––––––
RED=${RED:-"\033[0;31m"} ; GREEN=${GREEN:-"\033[0;32m"}
YELLOW=${YELLOW:-"\033[0;33m"} ; NC=${NC:-"\033[0m"}
info() { printf "%b%s%b\n" "${YELLOW}" "$*" "${NC}"; }
ok() { printf "%b%s%b\n" "${GREEN}" "$*" "${NC}"; }
die() { printf "%b%s%b\n" "${RED}" "$*" "${NC}" >&2; exit 1; }
#–––––––––––– Root‑Handling ––––––––––––––––––––––––––––––––––––––––––––––––––––
if (( EUID != 0 )); then
exec sudo --preserve-env=RED,GREEN,YELLOW,NC "$0" "$@"
fi
#–––––––––––– Zielbenutzer ermitteln –––––––––––––––––––––––––––––––––––––––––––
TARGET_USER=${SUDO_USER:-}
if [[ -z "${TARGET_USER}" ]]; then
read -rp "$(printf 'Enter the username to operate on: ')" TARGET_USER
fi
HOME_DIR="/home/${TARGET_USER}"
[[ -d "${HOME_DIR}" ]] || die "User ${TARGET_USER} not found."
#–––––––––––– Konfiguration laden ––––––––––––––––––––––––––––––––––––––––––––––
CONFIG_FILE="${HOME_DIR}/.config/sc.conf"
[[ -f "${CONFIG_FILE}" ]] && source "${CONFIG_FILE}" || info "sc.conf not found – proceeding with defaults."
#–––––––––––– Terminalbreite für zentrierte Warnung ––––––––––––––––––––––––––––
cols=$(tput cols 2>/dev/null || echo 80)
warn() {
local msg="$1"
local len=${#msg}
local pad=$(( (cols - len) / 2 < 0 ? 0 : (cols - len) / 2 ))
printf "%*s%b%s%b\n" "$pad" "" "$RED" "$msg" "$NC"
}
echo
warn "Reboot may be required after updates."
warn "Save your work before continuing."
echo
#–––––––––––– Aktualisierungen ––––––––––––––––––––––––––––––––––––––––––––––––
info "Updating APT packages …"
DEBIAN_FRONTEND=noninteractive apt-get -qq update
DEBIAN_FRONTEND=noninteractive apt-get -y full-upgrade
ok "APT packages updated."
if command -v snap &>/dev/null; then
info "Refreshing Snap packages …"
snap refresh || info "Snap refresh returned non‑zero (ignored)."
fi
#–––––––––––– Aufräumen –––––––––––––––––––––––––––––––––––––––––––––––––––––––
info "Cleaning temporary directories …"
find /tmp /var/tmp -mindepth 1 -delete
if command -v trash-empty &>/dev/null; then
info "Emptying trash for ${TARGET_USER} …"
sudo -u "${TARGET_USER}" trash-empty || true
fi
info "Cleaning APT cache …"
apt-get -y autoremove
apt-get -y autoclean
ok "System clean‑up done."
#–––––––––––– Abschluss & Reboot –––––––––––––––––––––––––––––––––––––––––––––––
echo
ok "System maintenance completed."
if [[ -t 0 ]]; then # interaktives Terminal?
read -rp "$(printf '%bReboot now? (y/N): %b' "${YELLOW}" "${NC}")" ans
if [[ "${ans}" =~ ^[Yy]$ ]]; then
info "Rebooting …"
reboot
fi
fi