-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-system.sh
More file actions
265 lines (223 loc) · 7.01 KB
/
update-system.sh
File metadata and controls
265 lines (223 loc) · 7.01 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/bin/bash
#
# update-system.sh
# -----------------
# A modular, extensible system maintenance script for Debian/Ubuntu.
#
# Features:
# - Update, upgrade, dist-upgrade, cleanup
# - System info summary (OS, kernel, disk, memory)
# - Package health check
# - Verbose/quiet modes with color-coded output
# - Per-run log files in /var/log/update-system/
# - Log rotation: keeps last 10 runs
# - Summary report (packages upgraded, warnings, errors)
#
# Usage:
# sudo ./update-system.sh --full --verbose
#
# Author: Vikram (https://github.com/vikram2327)
# License: MIT
# ---------------------------------------------------------------------
set -euo pipefail
# ---------------------------------------------------------------------
# CONFIGURATION
# ---------------------------------------------------------------------
LOGDIR="/var/log/update-system"
mkdir -p "$LOGDIR"
# Unique log file per run
LOGFILE="$LOGDIR/update-system-$(date '+%Y-%m-%d-%H-%M-%S').log"
# Keep last 10 logs
MAX_LOGS=10
VERBOSE=false
# Counters for summary
ERROR_COUNT=0
WARNING_COUNT=0
UPGRADE_COUNT=0
# Colors for screen output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
WHITE='\033[0;37m'
NC='\033[0m' # No Color
# Track run time
START_TIME=$(date +%s)
# ---------------------------------------------------------------------
# LOGGING FUNCTIONS
# ---------------------------------------------------------------------
rotate_logs() {
# Keep only the latest MAX_LOGS files
local FILES
FILES=($(ls -t "$LOGDIR"/update-system-*.log 2>/dev/null || true))
local COUNT=${#FILES[@]}
if (( COUNT > MAX_LOGS )); then
for ((i=MAX_LOGS; i<COUNT; i++)); do
rm -f "${FILES[$i]}"
done
fi
}
log() {
local LEVEL="$1"
local MSG="$2"
local TIMESTAMP="[$(date '+%Y-%m-%d %H:%M:%S')]"
# Count warnings and errors
case "$LEVEL" in
WARNING) ((WARNING_COUNT++)) ;;
ERROR) ((ERROR_COUNT++)) ;;
esac
# Pick color based on level
case "$LEVEL" in
INFO) COLOR=$BLUE ;;
SUCCESS) COLOR=$GREEN ;;
WARNING) COLOR=$YELLOW ;;
ERROR) COLOR=$RED ;;
*) COLOR=$NC ;;
esac
# Print to screen (colored)
echo -e "${COLOR}${TIMESTAMP} [$LEVEL] ${MSG}${NC}"
# Save plain text to log file
echo "${TIMESTAMP} [$LEVEL] ${MSG}" >> "$LOGFILE"
}
run_cmd() {
# Execute a command with color-coded output
local CMD="$1"
if [ "$VERBOSE" = true ]; then
{
eval "$CMD" 2> >(while read -r line; do
echo -e "${RED}[stderr] $line${NC}" | tee -a "$LOGFILE"
done) | while read -r line; do
echo -e "${WHITE}$line${NC}" | tee -a "$LOGFILE"
done
}
else
eval "$CMD" >> "$LOGFILE" 2>&1
fi
}
# ---------------------------------------------------------------------
# CORE FUNCTIONS
# ---------------------------------------------------------------------
system_info() {
log INFO "Collecting system information..."
{
echo "===== SYSTEM INFO ====="
uname -a
lsb_release -a 2>/dev/null || true
echo "Memory Usage:"
free -h
echo "Disk Usage:"
df -h /
echo "======================="
} | tee -a "$LOGFILE"
}
check_broken() {
log INFO "Checking for broken dependencies..."
run_cmd "sudo apt-get check"
}
update() {
log INFO "Updating package index..."
run_cmd "sudo apt-get update -y"
}
upgrade() {
log INFO "Upgrading installed packages..."
local OUTPUT
OUTPUT=$(sudo apt-get upgrade -y --fix-missing 2>&1)
echo "$OUTPUT" >> "$LOGFILE"
[ "$VERBOSE" = true ] && echo -e "${WHITE}$OUTPUT${NC}"
local COUNT=$(echo "$OUTPUT" | grep -oP '^\d+ upgraded' | awk '{print $1}' || echo 0)
UPGRADE_COUNT=$((UPGRADE_COUNT + COUNT))
}
dist_upgrade() {
log INFO "Performing full dist-upgrade..."
local OUTPUT
OUTPUT=$(sudo apt-get dist-upgrade -y 2>&1)
echo "$OUTPUT" >> "$LOGFILE"
[ "$VERBOSE" = true ] && echo -e "${WHITE}$OUTPUT${NC}"
local COUNT=$(echo "$OUTPUT" | grep -oP '^\d+ upgraded' | awk '{print $1}' || echo 0)
UPGRADE_COUNT=$((UPGRADE_COUNT + COUNT))
}
cleanup() {
log INFO "Removing unused packages..."
run_cmd "sudo apt-get autoremove -y"
log INFO "Cleaning package cache..."
run_cmd "sudo apt-get autoclean -y"
run_cmd "sudo apt-get clean -y"
}
full() {
system_info
check_broken
update
upgrade
dist_upgrade
cleanup
log SUCCESS "System fully updated and cleaned."
}
# ---------------------------------------------------------------------
# USAGE & HELP
# ---------------------------------------------------------------------
usage() {
echo "Usage: $0 [OPTIONS]"
echo
echo "Options:"
echo " --update Refresh package index"
echo " --upgrade Upgrade installed packages"
echo " --dist-upgrade Perform full upgrade (kernel, dependencies)"
echo " --cleanup Remove old/unused packages and cache"
echo " --system-info Show system info summary"
echo " --check Check for broken dependencies"
echo " --full Run all steps (system-info, check, update, upgrade, dist-upgrade, cleanup)"
echo " --verbose Show full command output in terminal"
echo " --quiet Only log to file (default)"
exit 1
}
# ---------------------------------------------------------------------
# SCRIPT START
# ---------------------------------------------------------------------
rotate_logs
log INFO "===== Update Script Started ====="
log INFO "User : $(whoami)"
log INFO "Hostname : $(hostname)"
log INFO "Date : $(date '+%Y-%m-%d %H:%M:%S')"
log INFO "Flags : $*"
# If no arguments, show usage
if [ $# -eq 0 ]; then
usage
fi
# Parse flags
for arg in "$@"; do
case "$arg" in
--update) update ;;
--upgrade) upgrade ;;
--dist-upgrade) dist_upgrade ;;
--cleanup) cleanup ;;
--system-info) system_info ;;
--check) check_broken ;;
--full) full ;;
--verbose) VERBOSE=true ;;
--quiet) VERBOSE=false ;;
*) usage ;;
esac
done
# ---------------------------------------------------------------------
# SUMMARY & END
# ---------------------------------------------------------------------
EXIT_CODE=$?
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))
if [ $EXIT_CODE -eq 0 ] && [ $ERROR_COUNT -eq 0 ]; then
log SUCCESS "===== Update Script Completed Successfully ====="
else
log ERROR "===== Update Script Failed (Exit Code: $EXIT_CODE) ====="
fi
log INFO "End Date : $(date '+%Y-%m-%d %H:%M:%S')"
log INFO "Duration : ${DURATION} seconds"
# Summary Report
log INFO "===== Summary Report ====="
log INFO "Packages Upgraded : $UPGRADE_COUNT"
log WARNING "Warnings : $WARNING_COUNT"
log ERROR "Errors : $ERROR_COUNT"
if [ $EXIT_CODE -eq 0 ] && [ $ERROR_COUNT -eq 0 ]; then
log SUCCESS "Run completed successfully with no errors."
else
log ERROR "Run completed with issues. Check the log file: $LOGFILE"
fi