2024-08-14 10:49:57 +05:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
if [ "$(id -u)" != "0" ]; then
|
|
|
|
echo -e "\033[31mThis script requires superuser rights\033[0m"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$1" ]; then
|
|
|
|
echo "Please provide the MYSQL_DB as argument"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
SECRETS_PATH=/data/secrets/$SERVER_DOMAIN/$SERVER_DOMAIN.env
|
|
|
|
|
|
|
|
trap 'echo -e "\033[31mmariadb.sh: Something went wrong\033[0m"; exit 1' ERR
|
|
|
|
set -e
|
|
|
|
|
|
|
|
export DEBIAN_FRONTEND=noninteractive
|
|
|
|
|
|
|
|
source $SECRETS_PATH
|
|
|
|
|
|
|
|
if [ -z "$MARIADB_ROOT_PASSWORD" ]; then
|
|
|
|
echo "Error: MARIADB_ROOT_PASSWORD is not set or is empty"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
MYSQL_DB="$1"
|
|
|
|
MYSQL_USER=root
|
|
|
|
|
|
|
|
CURRENT_MONTH=$(date +%Y-%m)
|
|
|
|
CURRENT_DATE=$(date +%Y-%m-%d)
|
|
|
|
CURRENT_DATETIME=$(date +%d-%m-%Y_%H%M%S)
|
|
|
|
BACKUPS_PATH=/backups/mariadb
|
|
|
|
DOCKER_SERVICE_NAME=mariadb
|
|
|
|
|
2024-08-21 17:48:38 +05:00
|
|
|
BACKUP_FOLDER=$BACKUPS_PATH/$MYSQL_DB
|
2024-08-14 10:49:57 +05:00
|
|
|
|
|
|
|
echo 'check BACKUP_FOLDER...'
|
|
|
|
mkdir -p "$BACKUP_FOLDER"
|
|
|
|
|
|
|
|
echo 'Creating mariadb backups...'
|
|
|
|
cd "$BACKUP_FOLDER"
|
|
|
|
if [ -f 'dump_'"$MYSQL_DB"'.sql' ]; then
|
|
|
|
rm 'dump_'"$MYSQL_DB"'.sql'
|
|
|
|
fi
|
|
|
|
DB_BACKUP_FILENAME=$MYSQL_DB'_'$CURRENT_DATETIME'.tar.gz'
|
|
|
|
|
2024-08-21 18:44:53 +05:00
|
|
|
echo 'Exec mariadb-dump...'
|
|
|
|
docker exec -t $DOCKER_SERVICE_NAME mariadb-dump -u $MYSQL_USER -p$MARIADB_ROOT_PASSWORD $MYSQL_DB > 'dump_'"$MYSQL_DB"'.sql'
|
2024-08-14 10:49:57 +05:00
|
|
|
|
|
|
|
echo 'Pack to tar.gz...'
|
|
|
|
tar -cf - 'dump_'"$MYSQL_DB"'.sql' | gzip -9 > "$DB_BACKUP_FILENAME"
|
|
|
|
rm 'dump_'"$MYSQL_DB"'.sql'
|
|
|
|
|
|
|
|
cd "$BACKUP_FOLDER"
|
|
|
|
|
|
|
|
echo 'Write md5 sum...'
|
|
|
|
md5sum * > MD5SUMS
|
|
|
|
|
|
|
|
trap - ERR
|
|
|
|
echo 'The backup of the '"$MYSQL_DB"' database was completed successfully'
|