-
Notifications
You must be signed in to change notification settings - Fork 0
/
update
executable file
·110 lines (95 loc) · 2.54 KB
/
update
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
#!/bin/bash
# _ _
# _ _ _ __ __| | __ _| |_ ___
# | | | | '_ \ / _` |/ _` | __/ _ \
# | |_| | |_) | (_| | (_| | || __/
# \__,_| .__/ \__,_|\__,_|\__\___|
# |_|
#
# Script : update
# Location : /usr/local/bin
# Author : Jens Ochmann
# Date : 2024-06-02
# License : MIT
# Description : Performs updates and cleanup tasks on linux systems.
# Requires root : Yes
NC='\033[0m' # No Colo
RED='\033[0;31m'
# Ensure root
root() {
if [ "$(id -u)" != "0" ]; then
echo -e "${RED}This script must be run as root${NC}" >&2
exit 1
fi
}
root
# Ask for the username to update
echo
read -p "Enter the username to be updated: " USERNAME
# Define User-Dir (HOME)
USER_DIR="/home/$USERNAME"
# Load config file
CONFIG_FILE="$USER_DIR/.config/sc.conf"
if [[ -f "$CONFIG_FILE" ]]; then
source "$CONFIG_FILE"
else
echo -e "${RED}Error: Configuration file not found.${NC}"
echo -e "${RED}Please ensure the configuration file 'sc.conf' exists at $HOME/.config/${NC}"
exit 1
fi
update_packages() {
echo
echo -e "${YELLOW}Updating all installed packages...${NC}"
apt update && apt full-upgrade -y
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to update packages.${NC}" >&2
return 1
fi
echo
echo -e "${YELLOW}Updating snap packages...${NC}"
snap refresh
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to update snap packages.${NC}" >&2
return 1
fi
}
cleanup_system() {
echo
echo -e "${YELLOW}Deleting temporary files...${NC}"
rm -rf /tmp/* /var/tmp/*
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to delete temporary files.${NC}" >&2
return 1
fi
echo
echo -e "${YELLOW}Emptying the trash using trash-cli...${NC}"
trash-empty
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to empty the trash.${NC}" >&2
return 1
fi
echo
echo -e "${YELLOW}Cleaning APT cache...${NC}"
apt autoremove -y && apt autoclean
if [ $? -ne 0 ]; then
echo -e "${RED}Failed to clean APT cache.${NC}" >&2
return 1
fi
}
# Start the maintenance process
echo
echo -e "${YELLOW}Starting system maintenance...${NC}"
update_packages
update_result=$?
cleanup_system
cleanup_result=$?
# if both update and cleanup were successful
if [ $update_result -eq 0 ] && [ $cleanup_result -eq 0 ]; then
echo
echo -e "${GREEN}System maintenance completed successfully!${NC}"
echo
else
echo
echo -e "${RED}System maintenance encountered errors.${NC}" >&2
echo
fi