0%

Server自动维护脚本

Ubuntu22的每日自动维护脚本

Ubuntu服务器每日凌晨五点自动重启,更新apt,更新github的DNS,配合crontab实现自动化。

本脚本不可直接使用,需要更改github_host备份文件的位置

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
day=$(date +"%d")
hour=$(date +"%H")
datetime=$(date +"%Y年%m月%d日 %H:%M:%S")
echo "===================${datetime}==================="
# 五点
if [ "${hour}" = "05" ]
then
echo "===================升级apt=============================="
apt update
apt upgrade -y

echo "===================升级github hosts====================="
rm -f github_host备份文件的位置
wget -O github_host备份文件的位置 https://raw.hellogithub.com/hosts
cp /etc/hosts /etc/hosts.backup
start=$(awk '/GitHub520 Host Start/{print NR}' /etc/hosts)
end=$(awk '/GitHub520 Host End/{print NR}' /etc/hosts)
sed -i "${start},${end}d" /etc/hosts
cat github_host备份文件的位置 >> /etc/hosts
cat /etc/hosts | grep "Update time"
echo "===================github hosts更新完成===================\n"
fi

# 重启
init 6

crontab定时指令

如下,每天凌晨五点执行该脚本

0 5 */1 * * sh $SCRIPTS/apt_upgrade.sh