You are here

Automating LXC Container Backups: A Step-by-Step Guide


Automating LXC Container Backups: A Step-by-Step Guide

Hello tech enthusiasts! Ever struggled with manual backups for LXC containers? In today's post, I'll demonstrate a seamless way to automate LXC container backups, making your life a tad easier.

Without further ado, let's delve into the script that I religiously use for this task:

#!/bin/bash
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

DATE=`date +%Y-%m-%d`
# The name of the container to be backed up
NAME=git.mytechnote.ru
lxc-stop -n $NAME
tar --numeric-owner -czvf /tmp/$DATE-$NAME.tar.gz /var/lib/lxc/lxc/$NAME
lxc-start -n $NAME
# Ensure the target folder for the backup exists
mv /tmp/$DATE-$NAME.tar.gz /mnt/backup/lxc/
RETENTION=$((21*24*60))
# Specify the number of backup copies to retain
KEEP=8
find /mnt/wd-backup/lxc -type f -printf '%T@ %p\n' -mmin +$RETENTION | sort -nr | tail -n +$KEEP | xargs -r rm

Here's a breakdown of the script's functions:

  1. It momentarily halts the container.
  2. Compresses the container's filesystem and configurations into a temporary location.
  3. Shifts the compressed backup to a designated storage location, in this instance, a samba share at /mnt/wd-backup.

A heads-up for potential users: Make sure you adjust the container name, the default path (/var/lib/lxc), and the final backup storage location before running the script.

Please note that this script is primarily for demonstration purposes. In a production environment, the script should be refined, for instance, to use shadow copies so as not to stop the container.

Let's connect! Share your thoughts and experiences related to this post on our forum.

0 0

Share the article with your friends in social networks, maybe it will be useful to them.


If the article helped you, you can >>thank the author<<