51 lines
1.8 KiB
Bash
Executable File
51 lines
1.8 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
#
|
|
# Run this script as root inside a service directory.
|
|
#
|
|
|
|
if [ -d "volumes" ]; then
|
|
# Dump mysql
|
|
if [ -d "volumes/mysql" ]; then
|
|
CONTAINER_NAME=$(echo ${PWD##*/} | sed -e 's/\.//g')_mariadb
|
|
DB_USER=$(nerdctl exec $CONTAINER_NAME bash -c 'echo "$MYSQL_USER"')
|
|
DB_PASSWORD=$(nerdctl exec $CONTAINER_NAME bash -c 'echo "$MYSQL_PASSWORD"')
|
|
DB_NAME=$(nerdctl exec $CONTAINER_NAME bash -c 'echo "$MYSQL_DATABASE"')
|
|
DUMP_NAME=$CONTAINER_NAME.sql
|
|
nerdctl exec -e MYSQL_PWD=$DB_PASSWORD $CONTAINER_NAME mariadb-dump -u $DB_USER $DB_NAME > volumes/$DUMP_NAME
|
|
fi
|
|
|
|
# Dump postgres
|
|
if [ -d "volumes/postgres" ]; then
|
|
CONTAINER_NAME=$(echo ${PWD##*/} | sed -e 's/\.//g')_postgres
|
|
DB_USER=$(nerdctl exec $CONTAINER_NAME bash -c 'echo "$POSTGRES_USER"')
|
|
DB_PASSWORD=$(nerdctl exec $CONTAINER_NAME bash -c 'echo "$POSTGRES_PASSWORD"')
|
|
DB_NAME=$(nerdctl exec $CONTAINER_NAME bash -c 'echo "$POSTGRES_DATABASE"')
|
|
DUMP_NAME=$CONTAINER_NAME.sql
|
|
nerdctl exec -e PGPASSWORD=$DB_PASSWORD $CONTAINER_NAME pg_dump -U $DB_USER $DB_NAME > volumes/$DUMP_NAME
|
|
fi
|
|
|
|
# List sockets to exclude
|
|
find . -type s -print > /tmp/sockets-to-exclude
|
|
if grep -q volumes "/tmp/sockets-to-exclude"; then
|
|
sed -i 's/volumes\///g' /tmp/sockets-to-exclude
|
|
fi
|
|
|
|
# Compress all volumes excerpt mysql and postgres
|
|
BACKUP=${PWD##*/}.tar.zst
|
|
tar -X /tmp/sockets-to-exclude --exclude='mysql' --exclude='postgres' --zstd -cf /tmp/$BACKUP -C volumes .
|
|
|
|
# Put it into the storage box (pubkey needed)
|
|
STORAGE_BOX=u442569@u442569.your-storagebox.de
|
|
TODAY=$(date +%F)
|
|
ssh -p23 $STORAGE_BOX "mkdir -p $TODAY"
|
|
scp -P 23 /tmp/$BACKUP $STORAGE_BOX:/home/$TODAY
|
|
|
|
# Clean
|
|
if [ -f "volumes/$DUMP_NAME" ]; then
|
|
rm volumes/$DUMP_NAME
|
|
fi
|
|
rm /tmp/$BACKUP
|
|
rm /tmp/sockets-to-exclude
|
|
fi
|