Created
December 5, 2019 18:09
-
-
Save deathbybandaid/a8c4bbf04e138104dd357399a7e9b97f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Configurables | |
# exclude list rules: | |
# * () runs task for all containers | |
# * (2050) excludes container 2050 | |
# * (2050 2051) excludes container 2050 and 2051 | |
# * (all) excludes all containers | |
timezone_exclude_list=() | |
lxc_cleanup_exclude_list=(2050) | |
backup_filename_tweak_exclude=(all) | |
# Job variables | |
job_vmtype=`echo ${VMTYPE}` # will be one of: (qemu lxc) | |
job_phase=`echo "$1"` # will be one of: (job-start job-end job-abort backup-start backup-end backup-abort log-end pre-stop pre-restart) | |
job_type=`echo "$2"` # will be one of: (stop suspend snapshot) | |
job_id=`echo "$3"` # container/VM ID | |
# Host variables | |
hosttz=`cat /etc/timezone` | |
# Guest variables | |
if [ "$job_vmtype" == "lxc" ] | |
then | |
pct_config_ostype=`pct config "$job_id" | grep ostype | cut -d ' ' -f2` | |
pct_status=`pct status "$job_id" | cut -d ' ' -f2` | |
fi | |
# additional variables | |
debian_list=(debian ubuntu) | |
# LXC timezone setting | |
if [ "$job_phase" == "backup-start" ] | |
then | |
# LXC | |
if [ "$job_vmtype" == "lxc" ] | |
then | |
# excluded ID | |
if (printf '%s\n' "${timezone_exclude_list[@]}" | grep -xq $job_id) || (printf '%s\n' "${timezone_exclude_list[@]}" | grep -xq all) | |
then | |
echo "Not updating Container time (excluded)" | |
else | |
# LXC state must be running | |
if [[ "$pct_status" == "running" ]] | |
then | |
# debian/ubuntu Only | |
if (printf '%s\n' "${debian_list[@]}" | grep -xq $pct_config_ostype) | |
then | |
lxctz=`pct exec "$job_id" -- bash -c "cat /etc/timezone"` | |
if [ $hosttz != $lxctz ] | |
then | |
echo "Proxmox host timezone is $hosttz and the containers is $lxctz . fixing now" | |
pct exec "$job_id" -- bash -c "echo $hosttz > /etc/timezone" | |
else | |
echo "Not updating Container time (already matching host)" | |
fi | |
else | |
echo "Not updating Container time (unsupported OS)" | |
# End OS type | |
fi | |
else | |
echo "Not updating Container time (container not running)" | |
# End state check | |
fi | |
# end of excluded check | |
fi | |
# End of LXC | |
fi | |
# End of backup-start | |
fi | |
# LXC apt clear | |
if [ "$job_phase" == "backup-start" ] | |
then | |
# LXC | |
if [ "$job_vmtype" == "lxc" ] | |
then | |
# excluded ID | |
if (printf '%s\n' "${lxc_cleanup_exclude_list[@]}" | grep -xq $job_id) || (printf '%s\n' "${lxc_cleanup_exclude_list[@]}" | grep -xq all) | |
then | |
echo "Skipping pre backup guest cleanup (excluded)" | |
else | |
# LXC state must be running | |
if [[ "$pct_status" == "running" ]] | |
then | |
# debian/ubuntu Only | |
if (printf '%s\n' "${debian_list[@]}" | grep -xq $pct_config_ostype) | |
then | |
echo "Running pre backup apt clear" | |
prespace=`pct exec "$job_id" -- bash -c "du -sh --block-size=1 /var/cache/apt/ | cut -f1"` | |
pct exec "$job_id" -- bash -c "apt-get clean" | |
postspace=`pct exec "$job_id" -- bash -c "du -sh --block-size=1 /var/cache/apt/ | cut -f1"` | |
spacesavedbytes=`expr $postspace - $prespace` | |
spacesavedbytes=${spacesavedbytes#-} | |
spacesavedkb=`expr $spacesavedbytes / 1024` | |
spacesavedmb=`expr $spacesavedbytes / 1024 / 1024` | |
spacesavedgb=`expr $spacesavedbytes / 1024 / 1024 / 1024` | |
if [[ "$spacesavedgb" -gt 0 ]] | |
then | |
spacesavedwords="$spacesavedgb GB" | |
elif [[ "$spacesavedmb" -gt 0 ]] | |
then | |
spacesavedwords="$spacesavedmb MB" | |
elif [[ "$spacesavedkb" -gt 0 ]] | |
then | |
spacesavedwords="$spacesavedkb KB" | |
elif [[ "$spacesavedbytes" -gt 0 ]] | |
then | |
spacesavedwords="$spacesavedbytes Bytes" | |
else | |
spacesavedwords="zero space" | |
fi | |
echo "Pre backup guest cleanup saved $spacesavedwords" | |
else | |
echo "Skipping pre backup guest cleanup (unsupported OS)" | |
# End OS type | |
fi | |
else | |
echo "Skipping pre backup guest cleanup (container not running)" | |
# End state check | |
fi | |
# end of excluded check | |
fi | |
# End of LXC | |
fi | |
# End of backup-start | |
fi | |
# Change backup filename | |
if [ "$job_phase" == "backup-end" ] | |
then | |
# excluded ID | |
if (printf '%s\n' "${backup_filename_tweak_exclude[@]}" | grep -xq $job_id) || (printf '%s\n' "${backup_filename_tweak_exclude[@]}" | grep -xq all) | |
then | |
echo "Skipping backup renaming (excluded)" | |
else | |
tarfile=${TARFILE} | |
exten=${tarfile#*.} | |
filename=${tarfile%.*.*} | |
hostname=${HOSTNAME} | |
hostname=`echo $hostname | cut -d "." -f1` | |
newfilename="$filename-$hostname.$exten" | |
echo "Renaming $tarfile to $newfilename" | |
mv $tarfile $newfilename | |
echo "Backup renaming successful" | |
# end of excluded check | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment