Skip to content

Instantly share code, notes, and snippets.

@vehystrix
Last active September 14, 2025 13:27
Show Gist options
  • Select an option

  • Save vehystrix/152f2ea31637997e2d75fa6617aacb27 to your computer and use it in GitHub Desktop.

Select an option

Save vehystrix/152f2ea31637997e2d75fa6617aacb27 to your computer and use it in GitHub Desktop.
Offline TKLBAM backup and restore for proxmox LXC containers
#!/bin/bash
# This script manages TurnKey Linux Backup and Migration (TKLBAM) operations for LXC containers.
# It supports backup, restore, and cleanup operations using a specified profile and LXC container ID.
# Backups are stored in /root/turnkey_backup/<lxc_id>.
#
# You will need to generate a tklbam profile first.
# The easy way to do this is to download or clone https://github.com/turnkeylinux/tklbam-profiles
# then run:
# cat tklbam-profiles/core tklbam-profiles/<appliance name> > <profile config file>
# Finally create the profile itself:
# tklbam-internal create-profile <profile dir> <profile config file>
# See also https://www.turnkeylinux.org/forum/support/fri-20220520-1226/downloadable-tklbam-profiles-non-cloud-based-backups
usage() {
echo "Usage: $0 --profile <profile_path> --lxc-id <lxc_id> [--backup | --restore | --cleanup]"
echo
echo "This script manages TurnKey Linux Backup and Migration (TKLBAM) operations for LXC containers."
echo "Options:"
echo " --profile <profile_path> Path to the TKLBAM profile to use."
echo " --lxc-id <lxc_id> ID of the LXC container to operate on."
echo " --backup Perform a backup operation."
echo " --restore Perform a restore operation."
echo " --cleanup Perform a cleanup operation."
exit 1
}
# Parse arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
--profile) profile="$2"; shift ;;
--lxc-id) lxc_id="$2"; shift ;;
--backup) option="backup" ;;
--restore) option="restore" ;;
--cleanup) option="cleanup" ;;
*) usage ;;
esac
shift
done
# Validate required arguments
if [[ -z "$profile" || -z "$lxc_id" || -z "$option" ]]; then
usage
fi
lxc_disk="/rpool/data/subvol-$lxc_id-disk-0"
if [ -e "$lxc_disk/root/tklbam_profile" ]; then
rm -rf "$lxc_disk/root/tklbam_profile"
fi
if [ -e "$lxc_disk/root/tklbam_backup" ]; then
rm -rf "$lxc_disk/root/tklbam_backup"
fi
if [ -e "$lxc_disk/root/tklbam_backup.escrow" ]; then
rm -f "$lxc_disk/root/tklbam_backup.escrow"
fi
cp -r "$profile" "$lxc_disk/root/tklbam_profile"
chown -R 100000:100000 "$lxc_disk/root/tklbam_profile"
pct exec "$lxc_id" -- bash -c 'tklbam-init --solo --force-profile=/root/tklbam_profile'
if [ "$option" == "backup" ]; then
pct exec "$lxc_id" -- bash -c 'tklbam-backup --address=file:///root/tklbam_backup'
pct exec "$lxc_id" -- bash -c 'tklbam-escrow --no-passphrase /root/tklbam_backup.escrow'
if [ -e "/root/turnkey_backup/$lxc_id" ]; then
rm -rf "/root/turnkey_backup/$lxc_id"
fi
mkdir -p "/root/turnkey_backup/$lxc_id"
cp -r "$lxc_disk/root/tklbam_backup" "/root/turnkey_backup/$lxc_id/tklbam_backup"
cp "$lxc_disk/root/tklbam_backup.escrow" "/root/turnkey_backup/$lxc_id/tklbam_backup.escrow"
fi
if [ "$option" == "restore" ]; then
cp -r "/root/turnkey_backup/$lxc_id/tklbam_backup" "$lxc_disk/root/tklbam_backup"
cp "/root/turnkey_backup/$lxc_id/tklbam_backup.escrow" "$lxc_disk/root/tklbam_backup.escrow"
chown -R 100000:100000 "$lxc_disk/root/tklbam_backup"
chown 100000:100000 "$lxc_disk/root/tklbam_backup.escrow"
pct exec "$lxc_id" -- bash -c 'tklbam-restore --address=file:///root/tklbam_backup --keyfile=/root/tklbam_backup.escrow'
pct exec "$lxc_id" -- bash -c 'reboot'
fi
if [ "$option" == "cleanup" ]; then
rm -rf "/root/turnkey_backup/$lxc_id/tklbam_backup"
rm "/root/turnkey_backup/$lxc_id/tklbam_backup.escrow"
rm -rf "$lxc_disk/root/tklbam_backup"
rm "$lxc_disk/root/tklbam_backup.escrow"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment