From aa812c456c4a773138cec2a5f7e30e426463e3b7 Mon Sep 17 00:00:00 2001 From: rick Date: Sat, 3 Aug 2019 21:17:48 +0200 Subject: [PATCH] added some validations --- README.md | 3 --- lxc_backup.sh | 22 ++++++++++++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3f1a4a8..a3578e0 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,3 @@ Backupscript for LXC Container This script creates a copy of all running containers, backs them up (via lxc export) and deletes them afterwards. Copys are created to prevent database inconsistencies -> copies are way faster than exports - -Default backuppath for lxd snap installs are `/var/snap/lxd/common/lxd/backups` you can simlink this dir to your favorite backup destination -In this script its `/home/rick-monitoring/backup/` diff --git a/lxc_backup.sh b/lxc_backup.sh index 7aab385..a99a531 100755 --- a/lxc_backup.sh +++ b/lxc_backup.sh @@ -1,12 +1,30 @@ #!/bin/bash #this script makes a copy of each container and backups afterwards the copies +#set -x + +backuppath='/home/rick-monitoring/backup/' +backupuser='rick-monitoring' + +#some validations +if [ ! -d ${backuppath} ] +then + echo "Please set a backup directory which is accessible by this script" + exit 2 +fi + +id $backupuser > /dev/null 2>&1 +if [ $? ! -eq 0 ] +then + echo "Please set a valid user within this script" + exit 2 +fi for container in $(lxc list --format csv -c n | grep -v backup); do lxc copy ${container} ${container}-backup - lxc export ${container}-backup $(date +%Y_%m_%d)-${container}.tar.gz --optimized-storage + lxc export ${container}-backup ${backuppath}$(date +%Y_%m_%d)-${container}.tar.gz --optimized-storage lxc delete ${container}-backup done #own data for backupuser -chown -R rick-monitoring:rick-monitoring /home/rick-monitoring/backup +chown -R ${backupuser}:${backupuser} ${backuppath}