Skip to content

Instantly share code, notes, and snippets.

@dlage
Last active February 8, 2021 16:24
Show Gist options
  • Save dlage/a5576d2d28f1e6137586777babc823bb to your computer and use it in GitHub Desktop.
Save dlage/a5576d2d28f1e6137586777babc823bb to your computer and use it in GitHub Desktop.
Bash script to update the Proxmox Virtualizer and all the running containers using their respective package manager. Supports apt-get, yum and dnf. Log everything to file as well.
#!/bin/bash
#
# Update PVE machine
# Update all running containers
set -x
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3 RETURN
exec 1>${0}.log 2>&1 1>&3
# list of container ids we need to iterate through
containers=$(pct list | tail -n +2 | cut -f1 -d' ')
function update_container() {
container=$1
echo "[Info] Updating $container"
# to chain commands within one exec we will need to wrap them in bash
if [ -n "$(pct exec $container -- which apt-get 2>/dev/null)" ]; then
echo "[Info] Debian-based container $container"
pct exec $container -- bash -c "apt-get update && apt-get upgrade -y && apt-get autoremove -y"
elif [ -n "$(pct exec $container -- which yum 2>/dev/null)" ]; then
echo "[Info] RPM-based container $container"
pct exec $container -- bash -c "yum update -y"
elif [ -n "$(pct exec $container -- which dnf 2>/dev/null)" ]; then
echo "[Info] DNF-based container $container"
pct exec $container -- bash -c "dnf upgrade --refresh -y"
fi
}
echo "[Info] Updating Proxmox Virtualizer"
apt-get update
apt-get upgrade -y
for container in $containers
do
status=`pct status $container`
if [ "$status" == "status: running" ]; then
update_container $container
fi
done; wait
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment