From 907cf4215b5301b9d56d19bbd120efcaa59a91a0 Mon Sep 17 00:00:00 2001 From: julien Date: Sun, 19 Jan 2025 16:51:01 +0100 Subject: [PATCH] wrapper backup script --- README.md | 2 ++ sbin/vol-backup | 43 ++++++++++++++++++++++++++++++++++++++++++ sbin/vol-backup-all | 6 ++++++ sbin/vol-compress | 46 --------------------------------------------- sbin/vol-extract | 10 ---------- sbin/vol-restore | 17 +++++++++++++++++ 6 files changed, 68 insertions(+), 56 deletions(-) create mode 100644 sbin/vol-backup create mode 100644 sbin/vol-backup-all delete mode 100644 sbin/vol-compress delete mode 100755 sbin/vol-extract create mode 100755 sbin/vol-restore diff --git a/README.md b/README.md index 5e59705..25b2d95 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ # scripts Execute `sync.sh` to copy scripts on the server. + +> `vol-restore` is not yet finished ! diff --git a/sbin/vol-backup b/sbin/vol-backup new file mode 100644 index 0000000..213c279 --- /dev/null +++ b/sbin/vol-backup @@ -0,0 +1,43 @@ +#!/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 + + # Compress all volumes excerpt mysql and postgres + BACKUP=${PWD##*/}.tar.zst + tar --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 +fi diff --git a/sbin/vol-backup-all b/sbin/vol-backup-all new file mode 100644 index 0000000..a7b367c --- /dev/null +++ b/sbin/vol-backup-all @@ -0,0 +1,6 @@ +#!/bin/bash + +for dir in /srv + cd $dir + vol-backup +done diff --git a/sbin/vol-compress b/sbin/vol-compress deleted file mode 100644 index e52e060..0000000 --- a/sbin/vol-compress +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/bash - -# -# Run this script as root inside a service directory. -# - -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 - -# 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 - -# Compress all volumes excerpt mysql and postgres -BACKUP=${PWD##*/}.tar.zst -tar --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 diff --git a/sbin/vol-extract b/sbin/vol-extract deleted file mode 100755 index 447231d..0000000 --- a/sbin/vol-extract +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -ARCHIVE=${PWD##*/}.tar.zst -if [ -d "volumes" ]; then - rm -r volumes -fi -mkdir volumes -tar --zstd --same-owner -xvf $ARCHIVE -C volumes - -# Add import .sql if db diff --git a/sbin/vol-restore b/sbin/vol-restore new file mode 100755 index 0000000..43cb280 --- /dev/null +++ b/sbin/vol-restore @@ -0,0 +1,17 @@ +#!/bin/bash + +# +# Run this script as root inside a service directory. +# The backup have to be in the same directory. +# + +BACKUP=${PWD##*/}.tar.zst +if [ -d "volumes" ]; then + rm -r volumes +fi +mkdir volumes +tar --zstd --same-owner -xvf $BACKUP -C volumes + +# Todo : +# Import .sql if there +# rm volumes/*.sql \ No newline at end of file