Compare commits

..

9 Commits

Author SHA1 Message Date
tteckster ba966fdff8
Update cron-update-lxcs.sh
This script is designed to schedule a cron job that updates all LXCs every Sunday at midnight.
2023-07-17 22:03:18 -04:00
tteckster 891bf6fe9f
Create cron-update-lxcs.sh 2023-07-17 21:09:55 -04:00
tteckster 054a4b8920
Delete cron-update-lxcs.sh 2023-07-17 18:50:19 -04:00
tteckster 3d8d8fc867
Update cron-update-lxcs.sh
tweak
2023-07-17 18:41:44 -04:00
tteckster 86bb11f25f
Update cron-update-lxcs.sh
tweak
2023-07-17 18:16:54 -04:00
tteckster 71bece9d27
Update cron-update-lxcs.sh
tweak
2023-07-17 17:58:06 -04:00
tteckster f0742abb2e
Update cron-update-lxcs.sh
tweak
2023-07-17 17:48:39 -04:00
tteckster abee2dfb1b
Create cron-update-lxcs.sh 2023-07-17 17:39:22 -04:00
tteckster 12dc7162a9
Create update-lxcs-cron.sh 2023-07-17 17:37:35 -04:00
2 changed files with 118 additions and 0 deletions

59
misc/cron-update-lxcs.sh Normal file
View File

@ -0,0 +1,59 @@
#!/usr/bin/env bash
# Copyright (c) 2021-2023 tteck
# Author: tteck (tteckster)
# License: MIT
# https://github.com/tteck/Proxmox/raw/main/LICENSE
clear
cat <<"EOF"
__ __ __ __ __ _ ________ ______
/ / / /___ ____/ /___ _/ /____ / / | |/ / ____/____ / ____/________ ____
/ / / / __ \/ __ / __ `/ __/ _ \ / / | / / / ___/ / / / ___/ __ \/ __ \
/ /_/ / /_/ / /_/ / /_/ / /_/ __/ / /___/ / /___(__ ) / /___/ / / /_/ / / / /
\____/ .___/\__,_/\__,_/\__/\___/ /_____/_/|_\____/____/ \____/_/ \____/_/ /_/
/_/
EOF
add() {
while true; do
read -p "This script will schedule a cron job that updates all LXCs every Sunday at midnight. Proceed(y/n)?" yn
case $yn in
[Yy]*) break ;;
[Nn]*) exit ;;
*) echo "Please answer yes or no." ;;
esac
done
sh -c '(crontab -l -u root 2>/dev/null; echo "0 0 * * 0 PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin /bin/bash -c \"\$(wget -qLO - https://github.com/tteck/Proxmox/raw/main/misc/update-lxcs-cron.sh)\" >>/var/log/update-lxcs-cron.log 2>/dev/null") | crontab -u root -'
clear
echo -e "\n To view Update LXCs Cron logs: cat /var/log/update-lxcs-cron.log"
}
remove() {
(crontab -l | grep -v "github.com/tteck/Proxmox/raw/main/misc/update-lxcs-cron.sh") | crontab -
rm /var/log/update-lxcs-cron.log
echo "Removed Update LXCs Cron from Proxmox VE"
}
# Define options for the whiptail menu
OPTIONS=(Add "Add Update LXCs Cron to Proxmox VE" \
Remove "Remove Update LXCs Cron from Proxmox VE")
# Show the whiptail menu and save the user's choice
CHOICE=$(whiptail --title "Update LXCs Cron for Proxmox VE" --menu "Select an option:" 10 58 2 \
"${OPTIONS[@]}" 3>&1 1>&2 2>&3)
# Check the user's choice and perform the corresponding action
case $CHOICE in
"Add")
add
;;
"Remove")
remove
;;
*)
echo "Exiting..."
exit 0
;;
esac

59
misc/update-lxcs-cron.sh Normal file
View File

@ -0,0 +1,59 @@
#!/usr/bin/env bash
# Copyright (c) 2021-2023 tteck
# Author: tteck (tteckster)
# License: MIT
# https://github.com/tteck/Proxmox/raw/main/LICENSE
echo "$(date)"
excluded_containers=("$@")
function update_container() {
container=$1
name=$(pct exec "$container" hostname)
os=$(pct config "$container" | awk '/^ostype/ {print $2}')
if [[ "$os" == "ubuntu" || "$os" == "debian" ]]; then
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 }')
read -ra disk_info_array <<< "$disk_info"
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]"
else
echo -e "\n[Info] Updating $container : $name - [No disk info for ${os}]"
fi
case "$os" in
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" ;;
ubuntu|debian|devuan) pct exec "$container" -- bash -c "apt-get update 2>/dev/null | grep 'packages.*upgraded'; apt list --upgradable && apt-get -y dist-upgrade" ;;
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
header_info
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
echo -e "[Info] Waiting For $container To Start"
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
echo -e "Finished, All Containers Updated. \n"