Files
bash_mng_scripts/update-lxc-container.sh

51 lines
1.5 KiB
Bash
Executable File

#!/bin/bash
declare -A pids
BRed='\033[1;31m' # Red
BGreen='\033[1;32m' # Green
Color_Off='\033[0m' # Text Reset
TMSTMP=$(date +'%Y%m%d%H%M%S')
function stop_container() {
sleep 120
lxc stop ${1}
echo -e $(date) "- ${BGreen}Container ${1} stopped${Color_Off}"
}
echo $(date) "- Start updating LXC container..."
for cont_name in $(lxc list type=container -c n --format csv); do
echo $(date) "- Make ${cont_name} container snapshot..."
lxc snapshot ${cont_name}
echo $(date) "- Updating ${cont_name} container..."
Status=$(lxc list type=container -c ns --format csv | grep ${cont_name} | cut -d',' -f 2)
if [ "${Status}" == "STOPPED" ]; then
echo -e $(date) "- ${BGreen}Start container ${cont_name}${Color_Off}"
lxc start ${cont_name}
sleep 5
else
echo -e $(date) "- ${BGreen}Container ${cont_name} is already started${Color_Off}"
fi
lxc exec ${cont_name} -- sh -c "apt update"
sleep 2
lxc exec ${cont_name} -- sh -c "apt upgrade -y"
sleep 2
lxc exec ${cont_name} -- sh -c "apt autoremove"
sleep 2
echo -e $(date) "- Refresh snap packages"
lxc exec ${cont_name} -- bash -c "if [[ \$(which snap > /dev/null; echo \$?) == 0 ]]; then snap refresh ; fi"
if [ "${Status}" == "STOPPED" ]; then
echo $(date) "- Stop container ${cont_name}"
stop_container ${cont_name} &
pids[${cont_name}]=$!
fi
sleep 2
done
for pid in ${pids[@]}; do
wait $pid
done
echo $(date) "- Updating LXC container ended"