Skip to content

Instantly share code, notes, and snippets.

@mietzen
Last active March 25, 2024 21:19
Show Gist options
  • Save mietzen/1fca565922a507131f8fb43055ddcd3f to your computer and use it in GitHub Desktop.
Save mietzen/1fca565922a507131f8fb43055ddcd3f to your computer and use it in GitHub Desktop.
Keep custom Proxmox LXC template up to date

Keep custom Proxmox LXC template up to date

This script fully clones LXC 999 (Debian Based Container) to the storage named SSD-Storage starts it and installs updates. Afterwards it will dump a Backup to the storage named NAS mounted in /mnt/pve/NAS and copies it with a meaningfull name in the local template cache.

Every Sunday this script will archive a copy of the template and name it with the current date, the last 4 archived images will be kept.

If you want to reuse this script edit the storages, e.g. if you only have the local storage replace SSD-Storage and NAS with local, also replace /mnt/pve/NAS/dump/ with /var/lib/vz/. Also be sure to that ID 9876 is not taken and your custom template is ID 999, or replace the ID's accordingly.

#!/bin/bash

rm -rf /mnt/pve/NAS/dump/vzdump-lxc-9876*
pct clone 999 9876 --full --hostname "custom-debian-intermediate-stage" --storage SSD-Storage
pct start 9876
pct exec 9876 -- bash -c 'apt-get update && apt-get dist-upgrade -y && apt-get autoremove -y && apt-get clean -y && history -c'
pct stop 9876
vzdump 9876 --compress gzip --mode stop --storage NAS
mv /mnt/pve/NAS/dump/vzdump-lxc-9876*.tar.gz /var/lib/vz/template/cache/debian-bullseye-custom_arm64.tar.gz
rm /mnt/pve/NAS/dump/vzdump-lxc-9876*.log
pct destroy 9876 --purge

if [[ $(date +%u) -eq 7 ]]; then 
    cp /var/lib/vz/template/cache/debian-bullseye-custom_arm64.tar.gz "/var/lib/vz/template/cache/debian-bullseye-custom_arm64_$(date +'%Y.%m.%d').tar.gz"
fi

if [[ $(ls -1 /var/lib/vz/template/cache/debian-bullseye-custom_arm64_* | wc -l) -gt 4 ]]; then 
    rm "$(ls -t /var/lib/vz/template/cache/debian-bullseye-custom_arm64_* | tail -1)"
fi

exit 0
#!/bin/bash
rm -rf /mnt/pve/NAS/dump/vzdump-lxc-9876*
pct clone 999 9876 --full --hostname "custom-debian-intermediate-stage" --storage SSD-Storage
pct start 9876
pct exec 9876 -- bash -c 'apt-get update && apt-get dist-upgrade -y && apt-get autoremove -y && apt-get clean -y && history -c'
pct stop 9876
vzdump 9876 --compress gzip --mode stop --storage NAS
mv /mnt/pve/NAS/dump/vzdump-lxc-9876*.tar.gz /var/lib/vz/template/cache/debian-bullseye-custom_arm64.tar.gz
rm /mnt/pve/NAS/dump/vzdump-lxc-9876*.log
pct destroy 9876 --purge
if [[ $(date +%u) -eq 7 ]]; then
cp /var/lib/vz/template/cache/debian-bullseye-custom_arm64.tar.gz "/var/lib/vz/template/cache/debian-bullseye-custom_arm64_$(date +'%Y.%m.%d').tar.gz"
fi
if [[ $(ls -1 /var/lib/vz/template/cache/debian-bullseye-custom_arm64_* | wc -l) -gt 4 ]]; then
rm "$(ls -t /var/lib/vz/template/cache/debian-bullseye-custom_arm64_* | tail -1)"
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment