ProxmoxVE-HelperScripts/misc/update-lxcs-cron.sh

51 lines
1.8 KiB
Bash
Raw Normal View History

2023-07-17 17:37:35 -04:00
#!/usr/bin/env bash
2024-01-01 12:13:05 -05:00
# Copyright (c) 2021-2024 tteck
2023-07-17 17:37:35 -04:00
# Author: tteck (tteckster)
# License: MIT
# https://github.com/tteck/Proxmox/raw/main/LICENSE
2024-09-05 06:07:26 -04:00
echo -e "\n $(date)"
2023-07-17 17:37:35 -04:00
excluded_containers=("$@")
function update_container() {
container=$1
name=$(pct exec "$container" hostname)
2024-09-05 06:07:26 -04:00
echo -e "\n [Info] Updating $container : $name \n"
2023-07-17 17:37:35 -04:00
os=$(pct config "$container" | awk '/^ostype/ {print $2}')
case "$os" in
2023-07-18 04:28:46 -04:00
alpine) pct exec "$container" -- ash -c "apk update && apk upgrade" ;;
archlinux) pct exec "$container" -- bash -c "pacman -Syyu --noconfirm" ;;
fedora | rocky | centos | alma) pct exec "$container" -- bash -c "dnf -y update && dnf -y upgrade" ;;
2024-09-05 06:07:26 -04:00
ubuntu | debian | devuan) pct exec "$container" -- bash -c "apt-get update && DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::="--force-confold" dist-upgrade -y; rm -rf /usr/lib/python3.*/EXTERNALLY-MANAGED" ;;
opensuse) pct exec "$container" -- bash -c "zypper ref && zypper --non-interactive dup" ;;
2023-07-17 17:37:35 -04:00
esac
}
for container in $(pct list | awk '{if(NR>1) print $1}'); do
excluded=false
for excluded_container in "${excluded_containers[@]}"; do
if [ "$container" == "$excluded_container" ]; then
excluded=true
break
fi
done
if [ "$excluded" == true ]; then
echo -e "[Info] Skipping $container"
sleep 1
else
status=$(pct status $container)
template=$(pct config $container | grep -q "template:" && echo "true" || echo "false")
if [ "$template" == "false" ] && [ "$status" == "status: stopped" ]; then
echo -e "[Info] Starting $container"
pct start $container
sleep 5
update_container $container
echo -e "[Info] Shutting down $container"
pct shutdown $container &
elif [ "$status" == "status: running" ]; then
update_container $container
fi
fi
done
wait