If you’ve been using LXD, you’re probably well familiar with the lxc snapshot command. The snapshot functionality included in LXD however leaves much to be desired. Especially if we’re wanting to do more risky behavior in our containers, like auto updating packages.
This is where lxdsnap comes in – a python based script that uses pylxd to interface with LXD’s rest API and create snapshots for all oontainers. Lxdsnap can then be setup as a cron job, giving up automatic snapshotting of our LXD containers! Props to Jdavid for this awesome script. You can find his github here
quoting from the git
Creates an snapashot of every LXD container in the localhost, once an hour. The name of the snapshot has the format YYYYMMDD-HH.
After the new snapshot has been created, old ones will be removed, except:
Those with a different format (to preserve snapshots created by other means).
The snapshot just created.
The first snapshot of the current day: YYYYMMMDD-00
Installing pylxd
To use this script, we first need to install pylxd on our host. We’ll be using pip to build and install pylxd.
Install the following packages
Install pip and other essentials sudo apt-get install python-pip python-dev build-essential libssl-dev python3.5-venv python3-dev
Upgrade pip
Upgrade pip pip install --upgrade pip
Install pylxd
pip install pylxd
Installing lxdsnap
Clone the lxdsnap git, and compile.
cd ~/ git clone https://github.com/jdavid/lxdsnap.git cd lxdsnap make install
If everything went well and no errors were displayed, lxdsnap is now fully installed and usable! Next step is setting up the cron job to ensure it runs automatically.
Setup lxdsnap as an automatic cronjob
To have lxdsnap automatically create snapshots, we’ll need to set it up as a cronjob.
Run the following as a user from the lxd group – the user you use to manage lxd is fine.
crontab -e
Paste the following, replacing username with the username of your user.
# LXD Snapshots, once an hour 30 * * * * /home/username/lxdsnap/venv/bin/python /home/username/lxdsnap/snap.py
Once saved, lxdsnap will automatically run once an hour (on the 30min mark) snapshotting every container on your localhost.
You can use
lxc info containername
to view these snapshots.
and
lxc restore containername snapshotname
To restore a snapshot.