mysql-backup/backup-mysql.sh

55 lines
1.3 KiB
Bash
Raw Normal View History

2023-12-26 22:21:03 +02:00
#!/usr/bin/env bash
USER="root"
PASSWORD="secretpass"
GZIP_ENABLED="1"
2023-12-26 22:21:03 +02:00
OUTPUT_DIR="./output"
HOSTNAME="127.0.0.1"
USE_PASSWORD="0"
USE_HOSTNAME="0"
# automatically export all variables
set -a
source .env
set +a
if [ "${DEBUG}" -gt "0" ]
then
set -x
fi
2023-12-26 22:21:03 +02:00
PASSWORD_OPTIONS="-p${PASSWORD}"
HOST_OPTIONS="-h ${HOSTNAME}"
if [ "${USE_HOSTNAME}" -eq "0" ]
then
HOST_OPTIONS=""
fi
if [ "${USE_PASSWORD}" -eq "0" ]
then
PASSWORD_OPTIONS=""
fi
databases=$(mysql ${HOST_OPTIONS} -u ${USER} ${PASSWORD_OPTIONS} --batch -s -r -e 'show databases')
for db in ${databases}; do
2023-12-26 22:45:22 +02:00
if [[ "${db}" != "information_schema" ]] && [[ "${db}" != "performance_schema" ]] && [[ "${db}" != "mysql" ]] && [[ "${db}" != _* ]] && [[ "${db}" != "sys" ]]
2023-12-26 22:21:03 +02:00
then
DUMP_DIR="${OUTPUT_DIR}/${db}"
DUMP_FILENAME="${DUMP_DIR}/$(date +%Y%m%d_%H:%M:%S).${db}.sql"
mkdir -vp "${DUMP_DIR}"
echo "Dumping database: ${db} into: ${DUMP_FILENAME}"
if [ "${GZIP_ENABLED}" -gt "0" ]
2023-12-26 22:21:03 +02:00
then
mysqldump ${HOST_OPTIONS} -u ${USER} ${PASSWORD_OPTIONS} --default-character-set=utf8mb4 --routines=true --triggers=true --databases ${db} | gzip > "${DUMP_FILENAME}.gz"
else
mysqldump ${HOST_OPTIONS} -u ${USER} ${PASSWORD_OPTIONS} --default-character-set=utf8mb4 --routines=true --triggers=true --databases ${db} > "${DUMP_FILENAME}"
fi
fi
done
set +x