Skip to content
www.mytechnote.ru
Menu
  • Administration
    • Scripts
  • Equipment
    • Reviews
    • Instructions
  • Other
  • English
    • English
    • Русский
Menu

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.

Posted on 17.09.201730.05.2024 by sanglyb

Post navigation

← How to Quickly Delete All MySQL Databases
Efficient Backup Scripting: A Guide to Automated Backup Rotation and Management with FTP in Bash →

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Подписывайтесь

Youtube
vk
telegram
github
twitter
rss
  • Форум
  • Об авторе
  • Поддержать автора

Популярные записи

Новые записи

  • How to Extend an LVM Partition in Linux
  • SCCM Reporting Errors After Migrating from SQL Server 2016 to 2019
  • Problem Using PassFiltEx on Older CPUs
  • How to remove attachments from old calendar events in Exchange using a script
  • How to download and install Office 2024 LTSC

Архивы

  • 2026 (3)
  • 2025 (5)
  • 2024 (7)
  • 2023 (30)
  • 2022 (4)
  • 2020 (17)
  • 2017 (82)
  • 2016 (5)
© 2015-2026 www.mytechnote.ru
Использование материалов сайта возможно только с разрешения владельца или обязательна индексируемая, прямая, передающая вес ссылка на материал.