No description
Find a file
nocci e324a188fe proxmox-lxc-shrink-restore.sh aktualisiert
script "remembers" used space in container so it does not need to start container after backup
2026-07-02 09:33:58 +00:00
LICENSE Initial commit 2026-07-02 09:02:43 +00:00
proxmox-lxc-shrink-restore.sh proxmox-lxc-shrink-restore.sh aktualisiert 2026-07-02 09:33:58 +00:00
README.md create README.md 2026-07-02 09:05:11 +00:00

Proxmox LXC Shrink/Restore Helper

Interactive Bash helper for safely shrinking Proxmox LXC containers by restoring a backup as a new container with a smaller root filesystem.

What this script does

Proxmox can easily grow LXC container disks, but shrinking them in place is risky and not directly supported in the same simple way. This script uses the safer approach:

  1. Select an existing LXC container as the source/reference container.
  2. Create a fresh backup or select an existing backup.
  3. Select a new target CTID.
  4. Select the target Proxmox storage.
  5. Choose a new root filesystem size.
  6. Restore the backup as a new container with the selected rootfs size.

The original container is never deleted automatically.

Safety features

The script is intentionally conservative.

It does not shrink the existing container directly. Instead, it restores a backup as a new container.

It calculates the minimum allowed root filesystem size based on the currently used space inside the source container plus a safety margin.

By default:

minimum size = used root filesystem space + 10%

The user cannot choose a smaller size than this calculated minimum.

The script also shows:

Original rootfs size
Current used space
Used space rounded up
Minimum allowed new size
Selected new size
Target storage
Target CTID
Backup file

Before restoring, the script prints a summary and asks for confirmation.

Important behavior after backup

The script uses:

vzdump --mode stop

for consistent backups.

After the backup, the script makes sure the source container remains stopped.

This is intentional.

If Proxmox or vzdump restores the previous running state after the backup, the script stops the source container again.

The source container is not restarted automatically by this script.

Requirements

Run this script directly on a Proxmox host as root.

Required commands:

pct
vzdump
pvesm
awk
sed
grep
sort
df
du
date
find
ls
tr

These are normally available on a Proxmox host.

Installation

Download or create the script file:

nano proxmox-lxc-shrink-restore.sh

Paste the script content into the file, then make it executable:

chmod +x proxmox-lxc-shrink-restore.sh

Usage

Run the script as root:

./proxmox-lxc-shrink-restore.sh

The script will guide you through the following steps:

  1. Select the source/reference container.
  2. Choose a backup directory.
  3. Create a fresh backup or select an existing one.
  4. Select the backup file to restore.
  5. Choose a new target CTID.
  6. Choose the target Proxmox storage.
  7. Enter the new rootfs size in GiB.
  8. Confirm the restore.
  9. Optionally start the newly restored container.

Example workflow

Assume the original container has:

CTID:                   123
Original rootfs size:   60 GiB
Used space:             18 GiB
Safety margin:          10%
Minimum allowed size:   20 GiB

The script will allow values such as:

20 GiB
25 GiB
30 GiB
60 GiB
100 GiB

It will reject values such as:

10 GiB
15 GiB
18 GiB
19 GiB

The restored container can then be created as a new CTID, for example:

Source CTID: 123
Target CTID: 124
New rootfs:  20 GiB

The original container remains untouched, except for being stopped during the backup process.

Changing the safety margin

At the top of the script, you can change the safety margin:

SAFETY_MARGIN_PERCENT=10

For example, to require at least 20 percent free space above the currently used size:

SAFETY_MARGIN_PERCENT=20

Notes about stopped containers

The minimum size check requires the source container to be running, because the script checks the used space with:

pct exec CTID -- df -Pk /

If the source container is stopped, the script cannot reliably calculate the used root filesystem size.

In that case, it will show a warning and continue without automatic minimum size validation.

For the safest result, start the source container before running the script, let the script calculate the used space, and then create the backup through the script.

After the restore

After restoring the new container, check it carefully before deleting or modifying the original container.

Useful commands:

pct status NEW_CTID
pct config NEW_CTID
pct exec NEW_CTID -- df -h /

Also check the actual services inside the container, for example:

pct exec NEW_CTID -- systemctl status

Depending on the container, you may also need to verify:

IP address
DNS
Mount points
Bind mounts
Application services
Databases
Reverse proxy configuration
Firewall rules
Autostart settings

Deleting the old container

This script does not delete the original container.

Only delete the old container after you have verified that the restored container works correctly.

Manual removal example:

pct stop OLD_CTID
pct destroy OLD_CTID

Be careful. pct destroy permanently removes the container.

Why restore instead of shrinking in place?

Shrinking filesystems and storage volumes in place can be risky, especially across different Proxmox storage backends such as LVM-thin, ZFS, directory storage, or Ceph.

The backup/restore method is easier to verify and safer because the original container remains available until the restored container has been tested.

Supported backup formats

The script searches for common LXC backup files:

vzdump-lxc-*.tar
vzdump-lxc-*.tar.gz
vzdump-lxc-*.tgz
vzdump-lxc-*.tar.zst
vzdump-lxc-*.tar.lzo

By default, it looks in:

/var/lib/vz/dump

You can enter a different backup directory during the interactive setup.

Disclaimer

Use this script at your own risk.

Always make sure you have a valid backup before changing or deleting containers.

The script is designed to avoid destructive actions, but you should still review every prompt carefully before confirming the restore.