2025-01-14 18:01:34 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
2025-01-19 14:20:56 +01:00
|
|
|
#
|
|
|
|
# Run this script as root inside a service directory.
|
|
|
|
#
|
|
|
|
|
2025-01-14 18:01:34 +01:00
|
|
|
if [ ! -d "volumes" ]; then
|
|
|
|
echo 'There is no data to archive ! Make sure you are in a service directory with persistent data (ie. must be a "volumes" directory there).'
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2025-01-19 14:20:56 +01:00
|
|
|
# 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"')
|
2025-01-19 15:38:05 +01:00
|
|
|
DUMP_NAME=$CONTAINER_NAME.sql
|
2025-01-19 14:51:50 +01:00
|
|
|
nerdctl exec -e MYSQL_PWD=$DB_PASSWORD $CONTAINER_NAME mariadb-dump -u $DB_USER $DB_NAME > volumes/$DUMP_NAME
|
2025-01-19 14:20:56 +01:00
|
|
|
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"')
|
2025-01-19 15:38:05 +01:00
|
|
|
DUMP_NAME=$CONTAINER_NAME.sql
|
2025-01-19 14:20:56 +01:00
|
|
|
nerdctl exec -e $CONTAINER_NAME pg_dump -U $DB_USER $DB_NAME > volumes/$DUMP_NAME
|
|
|
|
fi
|
|
|
|
|
2025-01-19 15:15:46 +01:00
|
|
|
# Compress all volumes excerpt mysql and postgres into the storage box (pubkey needed)
|
2025-01-19 14:20:56 +01:00
|
|
|
BACKUP=${PWD##*/}.tar.zst
|
2025-01-19 15:34:03 +01:00
|
|
|
tar --zstd -cf /tmp/$BACKUP -C volumes .
|
2025-01-19 15:38:05 +01:00
|
|
|
rm volumes/$DUMP_NAME
|
2025-01-19 15:32:06 +01:00
|
|
|
TODAY=$(date +%F)
|
2025-01-19 15:38:05 +01:00
|
|
|
scp -r /tmp/$BACKUP u442569@u442569.your-storagebox.de:/$TODAY/$BACKUP
|
2025-01-19 15:15:46 +01:00
|
|
|
rm /tmp/$BACKUP
|