2017-03-27 21:16:15 +02:00
#!/bin/bash
2021-12-30 18:37:52 +01:00
2022-10-15 07:49:01 +02:00
# Get hostname: try read from file, else get from env
2022-10-16 21:46:12 +02:00
[ -z " ${ MYSQL_HOST_FILE } " ] || { MYSQL_HOST = $( head -1 " ${ MYSQL_HOST_FILE } " ) ; }
2022-10-15 07:49:01 +02:00
[ -z " ${ MYSQL_HOST } " ] && { echo "=> MYSQL_HOST cannot be empty" && exit 1; }
# Get username: try read from file, else get from env
[ -z " ${ MYSQL_USER_FILE } " ] || { MYSQL_USER = $( head -1 " ${ MYSQL_USER_FILE } " ) ; }
2017-03-27 21:16:15 +02:00
[ -z " ${ MYSQL_USER } " ] && { echo "=> MYSQL_USER cannot be empty" && exit 1; }
2022-10-15 07:49:01 +02:00
# Get password: try read from file, else get from env, else get from MYSQL_PASSWORD env
2021-12-30 18:37:52 +01:00
[ -z " ${ MYSQL_PASS_FILE } " ] || { MYSQL_PASS = $( head -1 " ${ MYSQL_PASS_FILE } " ) ; }
2018-04-26 19:56:25 +02:00
[ -z " ${ MYSQL_PASS : = $MYSQL_PASSWORD } " ] && { echo "=> MYSQL_PASS cannot be empty" && exit 1; }
2022-10-15 07:49:01 +02:00
# Get database name(s): try read from file, else get from env
# Note: when from file, there can be one database name per line in that file
[ -z " ${ MYSQL_DATABASE_FILE } " ] || { MYSQL_DATABASE = $( cat " ${ MYSQL_DATABASE_FILE } " ) ; }
# Get level from env, else use 6
2020-09-03 15:30:21 +02:00
[ -z " ${ GZIP_LEVEL } " ] && { GZIP_LEVEL = 6; }
2017-03-27 21:16:15 +02:00
2017-04-08 17:57:23 +02:00
DATE = $( date +%Y%m%d%H%M)
2018-11-12 21:12:53 +01:00
echo " => Backup started at $( date "+%Y-%m-%d %H:%M:%S" ) "
2022-06-14 17:18:01 +02:00
DATABASES = ${ MYSQL_DATABASE :- ${ MYSQL_DB :- $( mysql -h " $MYSQL_HOST " -P " $MYSQL_PORT " -u " $MYSQL_USER " -p" $MYSQL_PASS " $MYSQL_SSL_OPTS -e "SHOW DATABASES;" | tr -d "| " | grep -v Database) } }
2019-07-28 15:58:55 +02:00
for db in ${ DATABASES }
2017-04-16 11:29:31 +02:00
do
2021-12-30 18:38:06 +01:00
if [ [ " $db " != "information_schema" ] ] \
&& [ [ " $db " != "performance_schema" ] ] \
&& [ [ " $db " != "mysql" ] ] \
&& [ [ " $db " != "sys" ] ] \
&& [ [ " $db " != _* ] ]
2017-04-08 17:57:23 +02:00
then
2018-11-12 21:12:53 +01:00
echo " ==> Dumping database: $db "
2017-03-27 21:16:15 +02:00
FILENAME = /backup/$DATE .$db .sql
2022-02-04 20:44:11 +01:00
LATEST = /backup/latest.$db .sql
2024-12-08 13:56:43 +01:00
BASIC_OPTS = "--single-transaction"
if [ -n " $REMOVE_DUPLICATES " ]
then
echo "WARNING: disabling comments in backup to remove deuplicate backups. Automatic database name detection won't work so set MYSQL_DATABASE on restore"
BASIC_OPTS = " $BASIC_OPTS " --skip-comments
fi
if mysqldump $BASIC_OPTS $MYSQLDUMP_OPTS -h " $MYSQL_HOST " -P " $MYSQL_PORT " -u " $MYSQL_USER " -p" $MYSQL_PASS " $MYSQL_SSL_OPTS " $db " > " $FILENAME "
2017-04-08 17:57:23 +02:00
then
2022-02-04 20:44:11 +01:00
EXT =
if [ -z " ${ USE_PLAIN_SQL } " ]
then
echo " ==> Compressing $db with LEVEL $GZIP_LEVEL "
2024-12-08 11:12:39 +01:00
gzip " - $GZIP_LEVEL " -n -f " $FILENAME "
2022-02-04 20:44:11 +01:00
EXT = .gz
FILENAME = $FILENAME $EXT
LATEST = $LATEST $EXT
fi
BASENAME = $( basename " $FILENAME " )
echo " ==> Creating symlink to latest backup: $BASENAME "
2018-11-08 14:34:29 +01:00
rm " $LATEST " 2> /dev/null
2022-02-04 20:44:11 +01:00
cd /backup || exit && ln -s " $BASENAME " " $( basename " $LATEST " ) "
2024-12-08 11:12:39 +01:00
if [ -n " $REMOVE_DUPLICATES " ]
then
echo "=> Removing duplicate database dumps"
fdupes -idN /backup/
fi
2021-10-21 12:37:55 +02:00
if [ -n " $MAX_BACKUPS " ]
then
2023-01-06 09:29:31 +01:00
# Execute the delete script, delete older backup or other custom delete script
2023-01-19 20:14:48 +01:00
/delete.sh " $db " $EXT
2021-10-21 12:37:55 +02:00
fi
2017-03-27 21:16:15 +02:00
else
2017-04-08 17:57:23 +02:00
rm -rf " $FILENAME "
2017-03-27 21:16:15 +02:00
fi
fi
done
2019-07-28 15:58:55 +02:00
echo " => Backup process finished at $( date "+%Y-%m-%d %H:%M:%S" ) "