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

8
README.md Normal file
View File

@@ -0,0 +1,8 @@
# Scripts
Some helpful bash scripts for [incus](https://linuxcontainers.org/incus)
- incus-container-upgrade: Perform `apt dist-upgrade` command on running instances.
- incus-backup: Backup instances FS and DBs to a remote location using rsync over ssh.
- incus-copy: Run a differential copy of all running instances to a remote incus server.
- incus-snapshot: Take a snapshot on all running instances.

41
incus-backup.db Normal file
View File

@@ -0,0 +1,41 @@
{
"bitwarden": {
"FS": ["/opt/bitwarden"]
},
"databap": {
"DB": ["databap"]
},
"freshrss": {
"DB": ["freshrss"]
},
"gateway": {
"FS": ["/var/www"]
},
"git": {
"DB": ["gitea"],
"FS": ["/home/git/projects"]
},
"mail": {
"DB": ["mailserver"],
"FS": ["/var/vmail", "/var/www"]
},
"nextcloud": {
"DB": ["nextcloud"],
"FS": ["/nextcloud"]
},
"seafile": {
"DB": ["ccnet-db", "seafile-db", "seahub-db"],
"FS": ["/opt/seafile"]
},
"solar": {
"DB": ["solar"],
"FS": ["/var/www/html/solar"]
},
"spot": {
"DB": ["spot"],
"FS": ["/var/www"]
},
"wedding": {
"FS": ["/var/www/mariage"]
}
}

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

15
incus-container-upgrade.sh Executable file
View File

@@ -0,0 +1,15 @@
#!/bin/bash
#
distUpgrade() {
CT=$1
DATE=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "\n*** [$DATE] - Dist-upgrading $CT container ***\n"
incus exec $CT --env DEBIAN_FRONTEND=noninteractive -- apt -qq update
incus exec $CT --env DEBIAN_FRONTEND=noninteractive -- apt -qq -y dist-upgrade
}
for CT in $(incus ls -c n -f compact,noheader status=RUNNING) ; do
distUpgrade "$CT" | tee -a /var/log/incus-container-upgrade.log
done

22
incus-copy.sh Executable file
View File

@@ -0,0 +1,22 @@
#!/bin/bash
#
# https://linuxcontainers.org/incus/docs/main/howto/move_instances/
#
#set -x
while getopts d:u:i:p:f:s: flag
do
case "${flag}" in
d) DEST=${OPTARG};;
m) MODE=${OPTARG};;
esac
done
for CT in $(/usr/bin/incus list -c n -f compact,noheader status=RUNNING) ; do
DATE=$(date '+%Y-%m-%d %H:%M:%S')
echo "[${DATE}] incus copy ${CT} ${DEST}:$CT --refresh --mode push"
/usr/bin/incus copy $CT ${DEST}:$CT --refresh --mode ${MODE} 2>&1 > /dev/null
done

13
incus-snapshot.sh Executable file
View File

@@ -0,0 +1,13 @@
#!/bin/bash
#
# https://linuxcontainers.org/incus/docs/main/howto/instances_backup/
#
#set -x
for CT in $(/usr/bin/incus list -c n -f compact,noheader status=RUNNING) ; do
DATE=$(date '+%Y-%m-%d %H:%M:%S')
echo "[${DATE}] incus snapshot $CT"
/usr/bin/incus snapshot $CT 2>&1 > /dev/null
done