dump db into .sql

This commit is contained in:
julien 2025-01-19 14:20:56 +01:00
parent 7f1c51fc27
commit 20a5d32789
2 changed files with 35 additions and 2 deletions

35
sbin/vol-compress Executable file → Normal file
View File

@ -1,9 +1,40 @@
#!/bin/bash #!/bin/bash
#
# Run this script as root inside a service directory.
#
if [ ! -d "volumes" ]; then 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).' 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 exit
fi fi
ARCHIVE=${PWD##*/}.tar.zst # Dump mysql
tar --zstd -cf $ARCHIVE -C volumes . 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_$(date +%F_%T).sql
if [ -f "volumes/*.sql" ]; then
rm volumes/*.sql
fi
nerdctl exec -e $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_$(date +%F_%T).sql
if [ -f "volumes/*.sql" ]; then
rm volumes/*.sql
fi
nerdctl exec -e $CONTAINER_NAME pg_dump -U $DB_USER $DB_NAME > volumes/$DUMP_NAME
fi
# Compress all volumes excerpt mysql and postgres into the staorage box (pubkey needed)
BACKUP=${PWD##*/}.tar.zst
ssh u441501@u441501.your-storagebox.de tar --zstd --exclude='volumes/mysql' --exclude='volumes/postgres' -cf - volumes > $BACKUP

View File

@ -6,3 +6,5 @@ if [ -d "volumes" ]; then
fi fi
mkdir volumes mkdir volumes
tar --zstd --same-owner -xvf $ARCHIVE -C volumes tar --zstd --same-owner -xvf $ARCHIVE -C volumes
# Add import .sql if db