update-lxcs-cron.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env bash
  2. # Copyright (c) 2021-2023 tteck
  3. # Author: tteck (tteckster)
  4. # License: MIT
  5. # https://github.com/tteck/Proxmox/raw/main/LICENSE
  6. echo "$(date)"
  7. excluded_containers=("$@")
  8. function update_container() {
  9. container=$1
  10. name=$(pct exec "$container" hostname)
  11. os=$(pct config "$container" | awk '/^ostype/ {print $2}')
  12. if [[ "$os" == "ubuntu" || "$os" == "debian" ]]; then
  13. disk_info=$(pct exec "$container" df /boot | awk 'NR==2{gsub("%","",$5); printf "%s %.1fG %.1fG %.1fG", $5, $3/1024/1024, $2/1024/1024, $4/1024/1024 }')
  14. read -ra disk_info_array <<<"$disk_info"
  15. echo -e "\n[Info] Updating $container : $name - Boot Disk: ${disk_info_array[0]}% full [${disk_info_array[1]}/${disk_info_array[2]} used, ${disk_info_array[3]} free]"
  16. else
  17. echo -e "\n[Info] Updating $container : $name - [No disk info for ${os}]"
  18. fi
  19. case "$os" in
  20. alpine) pct exec "$container" -- ash -c "apk update && apk upgrade" ;;
  21. archlinux) pct exec "$container" -- bash -c "pacman -Syyu --noconfirm" ;;
  22. fedora | rocky | centos | alma) pct exec "$container" -- bash -c "dnf -y update && dnf -y upgrade" ;;
  23. ubuntu | debian | devuan) pct exec "$container" -- bash -c "apt-get update 2>/dev/null | grep 'packages.*upgraded'; apt list --upgradable && DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::="--force-confold" dist-upgrade -y" ;;
  24. esac
  25. }
  26. for container in $(pct list | awk '{if(NR>1) print $1}'); do
  27. excluded=false
  28. for excluded_container in "${excluded_containers[@]}"; do
  29. if [ "$container" == "$excluded_container" ]; then
  30. excluded=true
  31. break
  32. fi
  33. done
  34. if [ "$excluded" == true ]; then
  35. header_info
  36. echo -e "[Info] Skipping $container"
  37. sleep 1
  38. else
  39. status=$(pct status $container)
  40. template=$(pct config $container | grep -q "template:" && echo "true" || echo "false")
  41. if [ "$template" == "false" ] && [ "$status" == "status: stopped" ]; then
  42. echo -e "[Info] Starting $container"
  43. pct start $container
  44. echo -e "[Info] Waiting For $container To Start"
  45. sleep 5
  46. update_container $container
  47. echo -e "[Info] Shutting down $container"
  48. pct shutdown $container &
  49. elif [ "$status" == "status: running" ]; then
  50. update_container $container
  51. fi
  52. fi
  53. done
  54. wait
  55. echo -e "Finished, All Containers Updated. \n"