first commit

This commit is contained in:
2025-11-25 17:54:48 +00:00
commit 9327e9d050
6 changed files with 154 additions and 0 deletions

55
incus-backup.sh Executable file
View File

@@ -0,0 +1,55 @@
#!/bin/bash
#
# Backup instances FS and DBs to a remote location using rsync over ssh.
#
while getopts d:u:i:p:f:s: flag
do
case "${flag}" in
d) DEST=${OPTARG};;
u) USER=${OPTARG};;
i) SSH_KEY=${OPTARG};;
p) SSH_PORT=${OPTARG};;
f) DB_FILE=${OPTARG};;
s) STORAGE_POOL=${OPTARG};;
esac
done
HOST=$(hostname -s)
BKP_DIR="/backup/${HOST}"
SSH_CMD="ssh -i ${SSH_KEY} -p ${SSH_PORT}"
CT_PREFIX="/var/lib/incus/storage-pools/${STORAGE_POOL}/containers"
# Backup incus DB
/usr/bin/incus admin sql local .dump | ${SSH_CMD} ${USER}@${DEST} "cat - > ${BKP_DIR}/incus-local-db.sql"
/usr/bin/incus admin sql global .dump | ${SSH_CMD} ${USER}@${DEST} "cat - > ${BKP_DIR}/incus-global-db.sql"
for CT in $(cat ${DB_FILE} | jq -r 'keys[]') ; do
SRC_DIR="${CT_PREFIX}/${CT}"
DST_DIR="${BKP_DIR}/${CT}"
# Backup container info
if [ -f "${SRC_DIR}/backup.yaml" ] ; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Starting $CT backup.yaml"
/usr/bin/rsync -a --del -e "${SSH_CMD}" ${SRC_DIR}/backup.yaml ${USER}@${DEST}:${DST_DIR}/
fi
# Backup Mysql dumps
for DB in $(cat $DB_FILE | jq -r ".${CT} | select(.DB != null) | .DB[]") ; do
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Starting $CT $DB database backup"
/usr/bin/incus exec -n $CT -- mariadb-dump --single-transaction --databases ${DB} | ${SSH_CMD} ${USER}@${DEST} "mkdir -p ${DST_DIR} ; cat - > ${DST_DIR}/mysql-${DB}.sql"
done
# Backup container rootfs paths
for FS in $(cat $DB_FILE | jq -r ".${CT} | select(.FS != null) | .FS[]") ; do
# Skip missing rootfs dir
if [ ! -d ${SRC_DIR}/rootfs ] ; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] WARNING : Missing rootfs for container ${CT}, skipping FS ${FS} ..."
continue
fi
# "/." used by rsync to limit the amount of path information that is sent as implied directories
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Starting $CT $FS filesystem backup"
/usr/bin/rsync -aR --del -e "${SSH_CMD}" ${SRC_DIR}/rootfs/.${FS} ${USER}@${DEST}:${DST_DIR}/
done
done