LXD Backup script
This small Bash script will create an online backup your LXC container with help of Rclone and a custom bash script called lxdbackup I wrote to facilitate backups. With this combination you can backup your LCX containers to Amazon S3 or Openstack Swift.
What does it do:
- This scripts creates a backup image from a snapshot from your LXC container that is managed with LXD.
- The script will upload your image to a cloudstorage with Rclone.
- Online backup of your LXC container
- Creates an easy and ready to use LXC image to import with the LXC import command.
What it does not cover:
- Create a constant database backup, this does not work with backups.
- Handle your data retention, still work in progress.
You can run the script from the command line, or place it in your cron.
Install Rclone: Repository: https://github.com/ncw/rclone Installation instructions: http://rclone.org/install/
Or for a Linux 64 Bit Installation (Directly copied from rclone.org):
Fetch and unpack
curl -O http://downloads.rclone.org/rclone-current-linux-amd64.zip unzip rclone-current-linux-amd64.zip cd rclone-*-linux-amd64
Copy binary file
sudo cp rclone /usr/sbin/ sudo chown root:root /usr/sbin/rclone sudo chmod 755 /usr/sbin/rclone
sudo mkdir -p /usr/local/share/man/man1 sudo cp rclone.1 /usr/local/share/man/man1/ sudo mandb
Run rclone config to setup. See Rclone for more details.
Make sure rclone works with your Cloudstorage provider
rclone lsd mycloudstorage:
This action should list your cloudstorage bucket or container.
Install the lxdbackup script
Install it by cloning this repository on your LXC host:
git clone https://github.com/cloudrkt/lxdbackup
Then copy the lxdbackup to your $path:
cd lxdbackup && cp lxdbackup /usr/bin/
Don't forget to make it executable:
chmod +x /usr/bin/lxdbackup
Then test it with:
How do I set it to run automatically?
Use a cronjob like this to backup your container every day of the week on 01:10:
10 1 * * * flock -n /tmp/lxdbackup.lock lxdbackup container-name
You can check your system messages for information about the backup run.
How do I restore a container?
Download your container image from your cloudstorage and import it with LXC or even import it directly if your cloudstorage provides a HTTP(S) download for your image.
lxc image import <file> --alias <name>
Import from HTTPS directly:
lxc image import https://cloudrkt.com/lxdbackup --alias restored-image
This should give you a running server from the snapshot you made earlier.
This script will help you do your basic LXC backup tasks, and keeps it in a format you can restore easily without a lot of extra dependencies.
Please keep in mind that you need to check your backups regularly, and if possible as automated as possible to verify it.
Please check and try the lxdbackup and let me know if it works for you.