LXC Backup Restore

Prev Next

FMADIO LXC is structured such that all files for the LXC are contained within the directory structure of the container.

/opt/fmadio/lxc/<container name>/

This enables a simple tarball to be generated of the directory, similarly restoring that tarball on the same system, on a different system is straight forward. By tarballing the LXC directory above. It creates a simple and robust way to manage LXC containers.

The filename naming scheme of backups is:

<container name>-backup-YYYYMMDD-HHMM.tar.gz

Example:

fmadio@fmadio200v4-636:/opt/fmadio/lxc$ ls -al *backup*
-rw-r--r--    1 root     root     771946353 Oct  9 04:18 pcap2dns-20241002-0604-backup-20241009-0416.tar.gz
-rw-r--r--    1 root     root     721641207 Oct  3 11:07 pcap2dns-20241003-0935-backup-20241003-1106.tar.gz
fmadio@fmadio200v4-636:/opt/fmadio/lxc$

This can be used for backup of the container and also used as a distribution / install packaging approach for a container.

Backup the LXC Container

Step 1) Ensure the LXC Container is shutdown

Check the status

sudo lxc-ls -f

Example output is

fmadio@fmadio200v4-636:~$ sudo lxc-ls -f
NAME                   STATE   AUTOSTART GROUPS IPV4           IPV6 UNPRIVILEGED
pcap2dns-20240611-0523 STOPPED 0         -      -              -    false
pcap2dns-20240827-0525 STOPPED 0         -      -              -    false
pcap2dns-20241002-0604 STOPPED 0         -      -              -    false
pcap2dns-20241007-1033 RUNNING 0         -      192.168.255.30 -    false
pcap2json-202402091423 STOPPED 0         -      -              -    false
ubuntu22-202312061042  STOPPED 0         -      -              -    false
fmadio@fmadio200v4-636:~$

In the above example the LXC pcap2dns-20241007-1033 which we want to backup is in the RUNNING state.

To shutdown the container run the command

sudo lxc-stop -n <container name>

For example in the above example to power down LXC pcap2dns-20241007-1033 as the following

fmadio@fmadio200v4-636:~$ sudo lxc-stop -n pcap2dns-20241007-1033
fmadio@fmadio200v4-636:~$

Then check the container state is stopped

fmadio@fmadio200v4-636:~$ sudo lxc-ls -f
NAME                   STATE   AUTOSTART GROUPS IPV4 IPV6 UNPRIVILEGED
pcap2dns-20240611-0523 STOPPED 0         -      -    -    false
pcap2dns-20240827-0525 STOPPED 0         -      -    -    false
pcap2dns-20241002-0604 STOPPED 0         -      -    -    false
pcap2dns-20241007-1033 STOPPED 0         -      -    -    false
pcap2json-202402091423 STOPPED 0         -      -    -    false
ubuntu22-202312061042  STOPPED 0         -      -    -    false
fmadio@fmadio200v4-636:~$

Step 2) Run the backup

Backup uses the command

lxc_backup.lua <container name>

Optionally a path for the tarball can be specified

lxc_backup.lua <container name> <path to write tarball>

By default backups are stored in

/opt/fmadio/lxc/

In our example to backup LXC pcap2dns-20241007-1033 run the following command. This may take several minutes to run depending on the disk usage of the LXC container.

fmadio@fmadio200v4-636:~$ lxc_backup.lua pcap2dns-20241002-0604
fmad fmadlua Sep  7 2024 (/opt/fmadio/bin/fmadiolua /opt/fmadio/bin/lxc_backup.lua pcap2dns-20241002-0604 )
------------------------------------------------------
Backup Path:         /opt/fmadio/lxc/
Backup TarBall Name: pcap2dns-20241002-0604-backup-20241009-0416.tar.gz
LXC Directory:       /opt/fmadio/lxc/
LXC Name:            pcap2dns-20241002-0604
Current Date:        20241009-0416
------------------------------------------------------
sudo tar -czvf /opt/fmadio/lxc//pcap2dns-20241002-0604-backup-20241009-0416.tar.gz /opt/fmadio/lxc/pcap2dns-20241002-0604/*
tar: Removing leading `/' from member names
/opt/fmadio/lxc/pcap2dns-20241002-0604/backup.lua
tar: Removing leading `/' from hard link targets
2.27GiB 0:00:08 [ 265MiB/s] [                                                                                                 <=>
.
.
.
------------------------------------------------------
Backup File: /opt/fmadio/lxc//pcap2dns-20241003-0935-backup-20241009-0452.tar.gz
Backup Size:        721643872 (0.722 GB)
------------------------------------------------------
done 73.878011Sec 1.231300Min
fmadio@fmadio200v4-636:~$

Step 3) Confirm the backup file

After the backup has completed, confirm the location and size of the file

In the above example the filename is:

pcap2dns-20241002-0604-backup-20241009-0452.tar.gz

With a total storage size of 736MB

fmadio@fmadio200v4-636:/opt/fmadio/lxc$ ls -alh /opt/fmadio/lxc/pcap2dns-20241002-0604-backup-20241009-0452.tar.gz
-rw-r--r--    1 root     root      736.2M Oct  9 04:18 /opt/fmadio/lxc/pcap2dns-20241002-0604-backup-20241009-0452.tar.gz
fmadio@fmadio200v4-636:/opt/fmadio/lxc$

Restore LXC Container

Restoring the LXC container is quite straight forward. The following conditions are required

  • No container exists on the system with the same name

  • There is sufficient disk space

  • The tarball was created using the lxc_backup.lua command

To restore the container

Step 1) Run the Restore script

The restore script arguments are

lxc_restore.lua <location of tarball>

If no path is specified the default location of the tarball is

/opt/fmadio/lxc/

To restore the backup named

pcap2dns-20241003-0935-backup-20241003-1106.tar.gz

Run the following command

sudo lxc_restore.lua <container name backup tarball>

Example output

fmadio@fmadio200v4-636:~$ lxc_restore.lua pcap2dns-20241003-0935-backup-20241003-1106.tar.gz
fmad fmadlua Sep  7 2024 (/opt/fmadio/bin/fmadiolua /opt/fmadio/bin/lxc_restore.lua pcap2dns-20241003-0935-backup-20241003-1106.tar.gz )
------------------------------------------------------
Restore Path:         /opt/fmadio/lxc/pcap2dns-20241003-0935-backup-20241003-1106.tar.gz
Restore TarBall Name: pcap2dns-20241003-0935-backup-20241003-1106.tar.gz
LXC Directory:        /opt/fmadio/lxc/
LXC Name:             pcap2dns-20241003-0935
Current Date:         20241009-0439
------------------------------------------------------
Free Space on lxc directory           1.5T
Free Space on lxc directory in bytes: 1649267441664
Checking restore size.... please wait
------------------------------------------------------
Restore Size: 2857184276
sudo tar -xzf /opt/fmadio/lxc/pcap2dns-20241003-0935-0529-backup-20241016-0450.tar.gz -C / | pv -t
0:00:17
.
.

------------------------------------------------------
LXC restored: /opt/fmadio/lxc/pcap2dns-20241003-0935
------------------------------------------------------
done 12.840974Sec 0.214016Min
fmadio@fmadio200v4-636:~$

Step 2) Confirm container is restored

Confirm the LXC Container has been restored using the lxc tools

sudo lxc-ls -f

Example output is:

fmadio@fmadio200v4-636:~$ sudo lxc-ls -f
NAME                   STATE   AUTOSTART GROUPS IPV4 IPV6 UNPRIVILEGED
pcap2dns-20240611-0523 STOPPED 0         -      -    -    false
pcap2dns-20240827-0525 STOPPED 0         -      -    -    false
pcap2dns-20241002-0604 STOPPED 0         -      -    -    false
pcap2dns-20241003-0935 STOPPED 0         -      -    -    false
pcap2dns-20241007-1033 STOPPED 0         -      -    -    false
pcap2json-202402091423 STOPPED 0         -      -    -    false
ubuntu22-202312061042  STOPPED 0         -      -    -    false
fmadio@fmadio200v4-636:~$

In the above example we see the LXC container pcap2dns-20241003-0935 is present in the list.