-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
131 lines (106 loc) · 5 KB
/
install.sh
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
#!/bin/bash
# System Update and Cleanup initialization Script
# -----------------------------------------------
# This script collects basic info in order to configure the two other scripts to suit your needs.
# Then it sets them executable and adds them to cron for automatic execution
#
# Before using this script, ensure that you have backed up all important data. While updates
# generally are safe, there is always a small risk of system instability or data loss during
# the process. Use this script at your own risk.
#
# It is recommended to run this script initially in a testing environment before deploying
# it on a production system.
#
# Make sure that you have sufficient permissions to execute system updates and that your
# user is able to run commands with 'sudo'.
if [ "$(whoami)" != "root" ]; then
SUDO=sudo
fi
echo "AUTOMATED PACKAGE MAINTENANCE FOR DEBIAN-BASED LINUX SYSTEMS"
echo "This script suite automates the maintenance of a Debian-based Linux system. It handles package updates, upgrades all installed packages, removes obsolete packages, and cleans up the local package cache. If required, it will also trigger a system restart."
echo "WARNING : Before you go further with this, you should review the 2 scripts system_update.sh and system_cleanup.sh to make sure they suit your needs and, give them a first manual run to make sure everything is OK. Please refer to the documentation (or README.md file) for more information."
# Function to validate an email address
is_valid_email() {
local email="$1"
[[ "$email" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$ ]]
}
# 1. Copying files in path for a system wide utilization (+ remove .sh extension)
# Check if files exist and confirm replacement
if [ -f /usr/local/bin/system_update ] || [ -f /usr/local/bin/system_cleanup ]; then
echo "-----------------------------------------------------------"
read -p "Files already exist in /usr/local/bin/. Do you want to replace them? (y/n): " choice
if [ "$choice" = "y" ]; then
for file in system_*.sh; do
base_name=$(basename "$file" .sh)
${SUDO} cp -f "$file" "/usr/local/bin/$base_name"
done
echo "Done."
else
echo "Files not replaced. Aborting !"
exit 1
fi
else
for file in system_*.sh; do
base_name=$(basename "$file" .sh)
${SUDO} cp "$file" "/usr/local/bin/$base_name"
done
fi
# 2. Ask for the email address for notifications (mandatory)
while true; do
echo "-----------------------------------------------------------"
echo "Please, enter the email address you wish to be notified on :"
read -r email
# Validate the email address
if is_valid_email "$email"; then
echo "Valid email address. Updating system_update command..."
# Replace '[email protected]' in system_update.sh with the provided email address
sed -i "s/[email protected]/$email/" /usr/local/bin/system_update
echo "Email address updated successfully."
break
else
echo "Invalid email address. Please try again."
fi
done
# 3. Ask for the time of day to perform the maintenance routine
while true; do
echo "What time should the package maintenance begin? (choose an integer between 0 (midnight) and 23 (11PM))"
read -r time
# Check if the input is an integer between 0 and 23
if [[ "$time" =~ ^[0-9]+$ ]] && [ "$time" -ge 0 ] && [ "$time" -le 23 ]; then
echo "Valid time selected: $time:00."
break
else
echo "Invalid input. Please enter a number between 0 and 23."
fi
done
# 4. Schedule the maintenance tasks
# Define the paths to the scripts for further reference
update_script="/usr/local/bin/system_update"
cleanup_script="/usr/local/bin/system_cleanup"
# Calculate the time for the cleanup script (1 hour after the chosen time)
cleanup_time=$(( (time + 1) % 24 ))
# Define the cron job entries
update_job="0 $time * * * root $update_script"
cleanup_job="0 $cleanup_time * * * root $cleanup_script"
# Define the cron file paths
update_cron_file="/etc/cron.d/system_update"
cleanup_cron_file="/etc/cron.d/system_cleanup"
# Create or replace the cron files with the new job entries
echo "$update_job" | ${SUDO} tee "$update_cron_file" > /dev/null
echo "$cleanup_job" | ${SUDO} tee "$cleanup_cron_file" > /dev/null
# Set the correct permissions for the cron files
${SUDO} chmod 644 "$update_cron_file"
${SUDO} chmod 644 "$cleanup_cron_file"
# Ensure that the files are owned by root
${SUDO} chown root:root "$update_cron_file"
${SUDO} chown root:root "$cleanup_cron_file"
echo "Cron job created or updated successfully."
echo "Packages updates will run daily at $time:00."
echo "System cleanup will run daily at $cleanup_time:00."
echo "The email address $email will be notified if an upgrade or a system restart occurs."
echo "See also /var/log/system_update.log and system_cleanup.log for more detailled outputs."
# 6. Make the scripts executable
${SUDO} chmod +x "$update_script"
${SUDO} chmod +x "$cleanup_script"
echo "You're all set ! :-)"
exit 0