-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathucaresystem-core
executable file
·492 lines (449 loc) · 14.9 KB
/
ucaresystem-core
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#!/usr/bin/sudo /bin/bash
set -e
#
#
#_______________________________________________
#THIS IS THE Terminal Version of uCareSystem
#_______________________________________________
# Name : uCareSystem
# License: GPL3 (http://www.gnu.org/licenses/gpl.html)
# Author : Salih Emin
# WebSite: http://utappia.org
# Email : salih-(at)-utappia.org
# Date : 11-12-2024 (first release 19-02-2009)
# Version: 24.12.11
# System : Ubuntu Linux and derivatives. With Deb, Snap or Flatpak. (Partial support for Debian and WSL2)
# Description:
#This simple script will automatically refresh your package list, download and install
#updates (if there are any),
#remove any remaining packages and configuration files without interference.
#
## Script starts here
#Variables
GREEN="\e[32m"
CYAN="\e[36m"
MAGENTA="\e[35m"
YELLOW="\e[33m"
ENDCOLOR="\e[0m"
DIST_CODENAME=$(lsb_release --codename | cut -f2)
DATE=$(date +%F_%T)
UCARE_VERSION="24.12.11"
PREV_VER="24.11"
USERNAME=$(logname 2>/dev/null || echo $USER)
DONATE="https://www.paypal.com/donate/?hosted_button_id=SATQ6Y9S3UCSG"
# Checking if the user has run the script with "sudo" or not
if [[ $EUID -ne 0 ]] ; then
clear
echo ""
echo -e "${YELLOW} uCareSystem must be run as root or with sudo. ${ENDCOLOR}"
echo -e "${YELLOW} Now I will just exit... ${ENDCOLOR}" 1>&2
echo ""
sleep 2
exit 1
fi
# Function to check internet connectivity
function CHECK_INTERNET {
if curl -4 -s --head http://archive.ubuntu.com | head -n 1 | grep "200 OK" > /dev/null; then
return 0 # Internet connection is available
else
return 1 # No internet connection
fi
}
# Check internet connectivity
if ! CHECK_INTERNET; then
echo -e "${YELLOW} No internet connection detected... ${ENDCOLOR}"
sleep 1
echo "Please ensure that your system is connected to the internet,"
echo "and then try again..."
echo "Now I will just exit..."1>&2
echo ""
sleep 2
exit 1
fi
# Simple countdown function
function COUNTDOWN {
secs=$1
while [ $secs -gt 0 ]; do
echo -ne "$MESSAGE in ${YELLOW}$secs ${ENDCOLOR}seconds... (to exit now, press Ctrl+C) \r"
sleep 1
: $((secs--))
done
}
duration=5 # countdown duration in seconds
function WELCOME_SCREEN {
echo "________________________________________________________"
echo ""
echo -e " ${CYAN} uCareSystem $UCARE_VERSION ${ENDCOLOR}"
echo " ~~~~~~~~~~~~~~~~~~~~"
echo " Welcome to all-in-one System Update and maintenance app"
echo ""
echo " Dear $USERNAME"
echo ""
echo " If you've found it helpful and it has saved you time"
echo " please consider supporting its development with a"
echo " donation via PayPal. Your support not only fuels"
echo " continuous improvement but also inspires and"
echo " motivates me to keep pushing forward while also shows"
echo " your appreciation for the time and effort that I have"
echo " put into the project:"
echo ""
echo -e "${CYAN}$DONATE ${ENDCOLOR}"
echo "________________________________________________________"
echo
# start the countdown
MESSAGE="uCareSystem will start"
COUNTDOWN $duration
}
function MAINTENANCE {
echo
echo
echo -e "${MAGENTA} Started ${ENDCOLOR} "
sleep 1
echo
echo -e " ${GREEN} Checking Disk usage before System Maintenance ${ENDCOLOR}"
sleep 1
echo
df_output=$(df -h / | awk 'NR==2 {print "Percentage of disk space used:", $5, "\nFree space available:", $4}')
echo ""
echo -e "${YELLOW}$df_output ${ENDCOLOR}"
echo
sleep 1
# The following is for when the unit file, source configuration file or drop-ins
# of apt-news.service changed on disk and systemd wasn't aware of it.
if command -v systemctl &> /dev/null; then
echo -e "${YELLOW}Reloading systemd manager configuration ${ENDCOLOR}"
sudo systemctl daemon-reload
sleep 1
echo
fi
## Updates package lists
echo -e "${GREEN} Updating package lists ${ENDCOLOR}"
echo
# Update package lists and filter out warnings
sudo apt update 2> >(sed 's/^WARNING.*//') |
# Print each line with padding to fit the terminal width
xargs -I{} printf '%-*s\r' $(tput cols) "{}" && echo
echo -e "${YELLOW}Here are the packages that can be upgraded if they are not kept back due to phased releases: ${ENDCOLOR}"
sleep 1
sudo apt list --upgradable;
echo
echo -e "${GREEN} Finished updating package lists ${ENDCOLOR}"
sleep 1
echo
## Updates packages and libraries
sudo apt full-upgrade -V -y;
echo
echo -e "${GREEN} Finished upgrading system packages and libraries ${ENDCOLOR}"
sleep 1
echo
## Removes unneeded packages
sudo apt autoremove -y --purge;
echo
echo -e "${GREEN} Finished removing unneeded packages ${ENDCOLOR}"
sleep 1
echo
echo -e "${GREEN} Checking for Snap and Flatpak updates ${ENDCOLOR}"
echo
# Check if Snap is installed
if command -v snap &> /dev/null; then
# Stop all running Snap applications
echo -e "${YELLOW}Stopping all running Snap applications... ${ENDCOLOR}"
stopped_snaps=()
while IFS= read -r snapname; do
if sudo snap stop "$snapname" &>/dev/null; then
stopped_snaps+=("$snapname")
fi
done < <(sudo snap list | awk '/^name/ {next} {print $1}')
# Print the names of stopped Snap applications
if [ ${#stopped_snaps[@]} -gt 0 ]; then
echo -e "${YELLOW}Stopped Snap applications: ${ENDCOLOR}"
for snapname in "${stopped_snaps[@]}"; do
echo "$snapname"
done
# Check for updates and install them
echo -e "${YELLOW}Checking for Snap package updates and installing them... ${ENDCOLOR}"
echo
sudo snap refresh
# Remove old revisions of Snap packages
echo
echo -e "${YELLOW}Removing old revisions of Snap packages ${ENDCOLOR}"
echo
LANG=C sudo snap list --all | awk '/disabled/{print $1, $3}' |
while read snapname revision; do
sudo snap remove --purge "$snapname" --revision="$revision"
done
echo -e "${YELLOW}Old revisions of Snap packages removed successfully. ${ENDCOLOR}"
sleep 1
# Start previously stopped Snap applications
echo
echo -e "${YELLOW}Starting previously stopped Snap applications... ${ENDCOLOR}"
for snapname in "${stopped_snaps[@]}"; do
sudo snap start "$snapname" &>/dev/null
done
else
echo -e "${YELLOW}No Snap applications were stopped. ${ENDCOLOR}"
fi
else
echo
echo -e "${YELLOW}Snap is not available on this system. Skipping. ${ENDCOLOR}"
sleep 1
fi
# Check if flatpak is installed then start maintenance
if command -v flatpak &>/dev/null; then
echo ""
echo -e "${YELLOW}Checking for Flatpak package updates and installing them... ${ENDCOLOR}"
sleep 1
sudo flatpak update -y
echo ""
echo -e "${YELLOW}Removing unused Flatpak packages ${ENDCOLOR}"
sudo flatpak uninstall --unused -y
sleep 1
echo
else
echo ""
echo -e "${YELLOW}Flatpak is not available on this system. Skipping. ${ENDCOLOR}"
sleep 1
fi
echo
echo -e "${GREEN} Finished Snap and/or Flatpak package maintenance ${ENDCOLOR}"
sleep 1
echo
# purge-old-kernels - remove old kernel packages
# Copyright (C) 2012 Dustin Kirkland <kirkland -(at)- ubuntu.com>
#
# Authors: Dustin Kirkland <kirkland-(at)-ubuntu.com>
# Kees Cook <kees-(at)-ubuntu.com>
#
# NOTE: This script will ALWAYS keep the currently running kernel
# NOTE: Default is to keep 2 more, user overrides with --keep N
KEEP=2
# NOTE: Any unrecognized option will be passed straight through to apt
APT_OPTS=
while [ ! -z "$1" ]; do
case "$1" in
--keep)
# User specified the number of kernels to keep
KEEP="$2"
shift 2
;;
*)
APT_OPTS="$APT_OPTS $1"
shift 1
;;
esac
done
# Build our list of kernel packages to purge
CANDIDATES=$(ls -tr /boot/vmlinuz-* | head -n -${KEEP} | grep -v "$(uname -r)$" | cut -d- -f2- | awk '{print "linux-image-" $0 " linux-headers-" $0}' )
for c in $CANDIDATES; do
dpkg-query -s "$c" >/dev/null 2>&1 && PURGE="$PURGE $c"
done
if [ -z "$PURGE" ]; then
echo -e "${YELLOW}No kernels are eligible for removal ${ENDCOLOR}"
fi
sudo apt $APT_OPTS remove -y --purge $PURGE;
echo
echo -e "${GREEN} Finished removing old kernels ${ENDCOLOR}"
sleep 1
echo
## Removes unused config files of uninstalled packages
dpkg --get-selections | grep deinstall | cut -f1 | xargs sudo apt autoremove -y --purge;
echo
echo -e "${GREEN} Finished removing the config files of uninstalled packages ${ENDCOLOR}"
sleep 1
echo
## Removes package files that can no longer be downloaded and everything except
# the lock file in /var/cache/apt/archives, including directories.
sudo apt autoclean -y; sudo apt clean -y;
echo
echo -e "${GREEN} Finished cleaning up downloaded packages ${ENDCOLOR}"
sleep 1
echo
echo -e "${GREEN} Checking to see if a reboot is required ${ENDCOLOR}"
## Check to see if a reboot is required
if [ -f /var/run/reboot-required ]; then
echo
echo -e "${MAGENTA}* * * * * * * * * * * * * * * * * *${ENDCOLOR}"
echo -e "${MAGENTA}*${ENDCOLOR} Dear $USERNAME"
echo -e "${MAGENTA}*${ENDCOLOR}"
echo -e "${MAGENTA}*${ENDCOLOR} Consider rebooting your system"
echo -e "${MAGENTA}*${ENDCOLOR} to finish applying updates"
echo -e "${MAGENTA}*${ENDCOLOR} requested by the following"
echo -e "${MAGENTA}*${ENDCOLOR} packages:"
echo -e "${MAGENTA}*${ENDCOLOR} "
if [ -f /var/run/reboot-required.pkgs ]; then
cat /var/run/reboot-required.pkgs | uniq
echo -e "${MAGENTA}* * * * * * * * * * * * * * * * * *${ENDCOLOR}"
else
echo -e "${YELLOW}Cannot find the list of packages... Skipping ${ENDCOLOR}"
fi
sleep 1
else
echo
echo -e "${YELLOW} No reboot is required at this time... ${ENDCOLOR}"
echo -e "${YELLOW} Happy to be $(uptime -p) ${ENDCOLOR}"
echo -e "${YELLOW} and serve you since $(uptime -s)... ${ENDCOLOR}"
echo -e "${YELLOW} Enjoy your system... ${ENDCOLOR}"
echo
sleep 1
fi
sleep 1
echo
echo -e "${GREEN} Checking Disk usage after System Maintenance ${ENDCOLOR}"
echo
df_output=$(df -h / | awk 'NR==2 {print "Percentage of disk space used:", $5, "\nFree space available:", $4}')
echo -e "${YELLOW}$df_output ${ENDCOLOR}"
echo
sleep 1
}
function UPGRADE_EOL_TO_NEXT {
cp "/etc/apt/sources.list" "/etc/apt/sources.eol_${DATE}"
cat <<EOT > /etc/apt/sources.list
deb http://old-releases.ubuntu.com/ubuntu/ $DIST_CODENAME main restricted universe multiverse
deb http://old-releases.ubuntu.com/ubuntu/ $DIST_CODENAME-updates main restricted universe multiverse
deb http://old-releases.ubuntu.com/ubuntu/ $DIST_CODENAME-security main restricted universe multiverse
EOT
sudo apt install update-manager-core
sudo apt dist-upgrade -y
sudo do-release-upgrade
}
function UPGRADE_TO_NEXT_RELEASE {
sleep 1
echo "##########################################"
echo "uCareSystem will now check for the"
echo "availability of the next version"
echo "and it will start the upgrade"
echo "##########################################"
echo ""
sleep 1
do-release-upgrade
sleep 2
}
function UPGRADE_TO_DEVEL_RELEASE {
sleep 1
echo "##########################################"
echo "uCareSystem will now check for the"
echo "availability of the next development"
echo "version and it will start the upgrade"
echo "##########################################"
echo ""
sleep 1
do-release-upgrade -d
sleep 2
}
function SHOW_HELP {
cat << EOF
uCareSystem
~ $UCARE_VERSION ~
All-in-one system update and maintenance app
Usage: sudo ucaresystem-core <option>
If no option is specified, it performs the regular maintenance tasks.
It updates system, snap, and flatpak packages, removes old kernels, old
Snap and flatpack revisions and unneeded packages, and cleans up
configurations of uninstalled packages.
Options:
-h --help Display this help and exit
-u --upgrade Upgrade to the next release. Note: If you use a
regular release it will upgrade to the next one. If
you are on a LTS version, it will upgrade only to
the next LTS.
-d --development Upgrade to the next development release
-eol --end-of-life-upgrade
CAUTION: Do not use this if your Ubuntu has not
reached its End of life support.
Check https://wiki.ubuntu.com/Releases
This option is ONLY for Ubuntu releases that have
reached their EOL and you want to upgrade
it to the next supported version.
-r --reboot Reboot the system, after completing all the tasks,
-s --shutdown Shutdown the system, after completing all the tasks,
EOF
}
function GOODBYE {
sleep 1
echo "___________________________________________________________"
echo ""
echo -e " ${CYAN} uCareSystem $UCARE_VERSION ${ENDCOLOR}"
echo " ~~~~~~~~~~~~~~~~~~~~"
echo ""
echo " I am deeply grateful for your generous donations"
echo " during the $PREV_VER development cycle. Your contributions"
echo " have been invaluable in helping me enhance and"
echo " maintain the project. Thank you for believing in this"
echo " project and for being a crucial part of its success."
echo ""
echo " Dear $USERNAME, the following names are the legends"
echo " of $PREV_VER development cycle, supporters:"
echo ""
echo " - P. Loughman"
echo " - D. Emge"
echo " - K. A. Gkountras (Jemadux)"
echo " - B. Breitfeld"
echo " - S. McClatchie"
echo " - J. A. Villareal"
echo " - D. E. Swarthout"
echo ""
echo -e "${CYAN}$DONATE ${ENDCOLOR}"
echo ""
sleep 2
echo -e " ${MAGENTA}All maintenance Complete... GOODBYE${ENDCOLOR}"
echo "___________________________________________________________"
echo
sleep 1
}
function SHUT_DOWN {
sleep 1
echo
echo
echo -e "${GREEN} Shutting down as you requested ${ENDCOLOR}"
echo
MESSAGE="Shutting down..."
COUNTDOWN $duration
if command -v systemctl &> /dev/null; then
sudo systemctl poweroff
else
sudo poweroff
fi
}
function RE_BOOT {
sleep 1
echo
echo
echo -e "${GREEN} Rebooting as you requested ${ENDCOLOR}"
echo
MESSAGE="Rebooting..."
COUNTDOWN $duration
if command -v systemctl &> /dev/null; then
sudo systemctl reboot
else
sudo reboot
fi
sudo reboot
## End of script
}
# The main process starts
while [ "$1" != "" ]; do
case $1 in
-u | --upgrade ) WELCOME_SCREEN && MAINTENANCE && UPGRADE_TO_NEXT_RELEASE
exit
;;
-d | --development ) WELCOME_SCREEN && MAINTENANCE && UPGRADE_TO_DEVEL_RELEASE
exit
;;
-h | --help ) SHOW_HELP
exit
;;
-eol | --end-of-life-upgrade ) UPGRADE_EOL_TO_NEXT
exit
;;
-s | --shutdown ) WELCOME_SCREEN && MAINTENANCE && GOODBYE && SHUT_DOWN
exit
;;
-r | --reboot ) WELCOME_SCREEN && MAINTENANCE && GOODBYE && RE_BOOT
exit
;;
* ) SHOW_HELP
exit 1
esac
done
## If no parameter is given just do the regular maintenance
WELCOME_SCREEN && MAINTENANCE && GOODBYE