From bb28a9ddc1d08aa337bf501bc403a4e6e3dd9b15 Mon Sep 17 00:00:00 2001 From: quietsy Date: Sat, 11 Jan 2025 22:51:40 +0200 Subject: [PATCH] Add logrotate --- Dockerfile | 1 + Dockerfile.aarch64 | 1 + README.md | 1 + readme-vars.yml | 1 + root/defaults/custom.cnf | 2 +- root/etc/logrotate.d/mariadb | 23 ++++++++ .../s6-rc.d/init-mariadb-initdb/run | 2 + .../dependencies.d/svc-mariadb | 0 .../s6-rc.d/init-mariadb-poststart/run | 53 +++++++++++++++++++ .../type | 0 .../s6-rc.d/init-mariadb-poststart/up | 1 + .../s6-rc.d/init-mariadb-upgrade/run | 27 ---------- .../s6-rc.d/init-mariadb-upgrade/up | 1 - ...mariadb-upgrade => init-mariadb-poststart} | 0 14 files changed, 84 insertions(+), 29 deletions(-) create mode 100644 root/etc/logrotate.d/mariadb rename root/etc/s6-overlay/s6-rc.d/{init-mariadb-upgrade => init-mariadb-poststart}/dependencies.d/svc-mariadb (100%) create mode 100755 root/etc/s6-overlay/s6-rc.d/init-mariadb-poststart/run rename root/etc/s6-overlay/s6-rc.d/{init-mariadb-upgrade => init-mariadb-poststart}/type (100%) create mode 100644 root/etc/s6-overlay/s6-rc.d/init-mariadb-poststart/up delete mode 100755 root/etc/s6-overlay/s6-rc.d/init-mariadb-upgrade/run delete mode 100644 root/etc/s6-overlay/s6-rc.d/init-mariadb-upgrade/up rename root/etc/s6-overlay/s6-rc.d/user/contents.d/{init-mariadb-upgrade => init-mariadb-poststart} (100%) diff --git a/Dockerfile b/Dockerfile index 433f328..e9ac85d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,6 +21,7 @@ RUN \ fi && \ apk add --no-cache \ gnupg \ + logrotate \ mariadb==${MARIADB_VERSION} \ mariadb-backup==${MARIADB_VERSION} \ mariadb-client==${MARIADB_VERSION} \ diff --git a/Dockerfile.aarch64 b/Dockerfile.aarch64 index 40b4635..bc36cf9 100644 --- a/Dockerfile.aarch64 +++ b/Dockerfile.aarch64 @@ -21,6 +21,7 @@ RUN \ fi && \ apk add --no-cache \ gnupg \ + logrotate \ mariadb==${MARIADB_VERSION} \ mariadb-backup==${MARIADB_VERSION} \ mariadb-client==${MARIADB_VERSION} \ diff --git a/README.md b/README.md index 742b5e6..2dc52fe 100644 --- a/README.md +++ b/README.md @@ -366,6 +366,7 @@ Once registered you can define the dockerfile to use with `-f Dockerfile.aarch64 ## Versions +* **11.01.25:** - Add log rotation, follow the instructions in the container log. * **06.01.25:** - Rebase to Alpine 3.21. * **31.05.24:** - Rebase to Alpine 3.20. * **23.12.23:** - Rebase to Alpine 3.19. diff --git a/readme-vars.yml b/readme-vars.yml index 8448d2d..2965847 100644 --- a/readme-vars.yml +++ b/readme-vars.yml @@ -142,6 +142,7 @@ init_diagram: | "mariadb:latest" <- Base Images # changelog changelogs: + - {date: "11.01.25:", desc: "Add log rotation, follow the instructions in the container log."} - {date: "06.01.25:", desc: "Rebase to Alpine 3.21."} - {date: "31.05.24:", desc: "Rebase to Alpine 3.20."} - {date: "23.12.23:", desc: "Rebase to Alpine 3.19."} diff --git a/root/defaults/custom.cnf b/root/defaults/custom.cnf index 1c68d60..bb288cd 100644 --- a/root/defaults/custom.cnf +++ b/root/defaults/custom.cnf @@ -102,7 +102,7 @@ console = 1 # log_warnings = 2 # Error logging goes to syslog due to /etc/mysql/conf.d/mysqld_safe_syslog.cnf -#log_error = /config/log/mysql/mysql.log +log_error = /config/log/mysql/mariadb-error.log # # Enable the slow query log to see queries with especially long duration slow_query_log = 1 diff --git a/root/etc/logrotate.d/mariadb b/root/etc/logrotate.d/mariadb new file mode 100644 index 0000000..95b74a3 --- /dev/null +++ b/root/etc/logrotate.d/mariadb @@ -0,0 +1,23 @@ +/config/log/mysql/*.log /config/log/mysql/*.err { + firstaction + /usr/bin/mariadb-admin --local version 2> /dev/null + endscript + su abc abc + missingok + create 660 abc abc + notifempty + daily + minsize 1M + maxsize 100M + rotate 30 + dateext + dateformat .%Y-%m-%d-%H-%M-%S + compress + delaycompress + sharedscripts + olddir archive/ + createolddir 770 abc abc + postrotate + /usr/bin/mariadb-admin --local flush-error-log flush-engine-log flush-general-log flush-slow-log + endscript +} diff --git a/root/etc/s6-overlay/s6-rc.d/init-mariadb-initdb/run b/root/etc/s6-overlay/s6-rc.d/init-mariadb-initdb/run index 8f60c54..68d003a 100755 --- a/root/etc/s6-overlay/s6-rc.d/init-mariadb-initdb/run +++ b/root/etc/s6-overlay/s6-rc.d/init-mariadb-initdb/run @@ -57,6 +57,8 @@ EOM cat >>"${tempSqlFile}" <<-EONEWSQL $MYSQL_PASS GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION ; +DROP USER 'root'@'localhost'; +GRANT ALL ON *.* TO 'root'@'localhost' IDENTIFIED VIA unix_socket WITH GRANT OPTION ; DROP DATABASE IF EXISTS test ; $MYSQL_DB_SETUP EONEWSQL diff --git a/root/etc/s6-overlay/s6-rc.d/init-mariadb-upgrade/dependencies.d/svc-mariadb b/root/etc/s6-overlay/s6-rc.d/init-mariadb-poststart/dependencies.d/svc-mariadb similarity index 100% rename from root/etc/s6-overlay/s6-rc.d/init-mariadb-upgrade/dependencies.d/svc-mariadb rename to root/etc/s6-overlay/s6-rc.d/init-mariadb-poststart/dependencies.d/svc-mariadb diff --git a/root/etc/s6-overlay/s6-rc.d/init-mariadb-poststart/run b/root/etc/s6-overlay/s6-rc.d/init-mariadb-poststart/run new file mode 100755 index 0000000..a4788d1 --- /dev/null +++ b/root/etc/s6-overlay/s6-rc.d/init-mariadb-poststart/run @@ -0,0 +1,53 @@ +#!/usr/bin/with-contenv bash +# shellcheck shell=bash + +# check logrotate permissions +if mariadb-admin --local version 2> /dev/null; then + echo "Logrotate is enabled" +else + cat <<-EOFPASS + + + +##################################################################################### +# # +# Logrotate requires the following permission: # +# GRANT ALL ON *.* TO root@localhost IDENTIFIED VIA unix_socket WITH GRANT OPTION ; # +# You can grant it via: # +# mariadb -uroot -p # +# # +# You can read more about it here: # +# https://mariadb.com/kb/en/authentication-from-mariadb-10-4/ # +# # +##################################################################################### + + + +EOFPASS +fi + +# check for upgrades +if [[ "${#MYSQL_ROOT_PASSWORD}" -gt "3" ]]; then + # display a message about upgrading database if needed + if mariadb-upgrade -u root -p"${MYSQL_ROOT_PASSWORD}" --check-if-upgrade-is-needed >/dev/null 2>&1; then + cat <<-EOF + + + +################################################################# +# # +# An upgrade is required on your databases. # +# # +# Stop any services that are accessing databases # +# in this container, and then run the command # +# # +# mariadb-upgrade -u root # +# # +################################################################# + + + +EOF + sleep 5s + fi +fi diff --git a/root/etc/s6-overlay/s6-rc.d/init-mariadb-upgrade/type b/root/etc/s6-overlay/s6-rc.d/init-mariadb-poststart/type similarity index 100% rename from root/etc/s6-overlay/s6-rc.d/init-mariadb-upgrade/type rename to root/etc/s6-overlay/s6-rc.d/init-mariadb-poststart/type diff --git a/root/etc/s6-overlay/s6-rc.d/init-mariadb-poststart/up b/root/etc/s6-overlay/s6-rc.d/init-mariadb-poststart/up new file mode 100644 index 0000000..82cc70d --- /dev/null +++ b/root/etc/s6-overlay/s6-rc.d/init-mariadb-poststart/up @@ -0,0 +1 @@ +/etc/s6-overlay/s6-rc.d/init-mariadb-poststart/run diff --git a/root/etc/s6-overlay/s6-rc.d/init-mariadb-upgrade/run b/root/etc/s6-overlay/s6-rc.d/init-mariadb-upgrade/run deleted file mode 100755 index 4efbab5..0000000 --- a/root/etc/s6-overlay/s6-rc.d/init-mariadb-upgrade/run +++ /dev/null @@ -1,27 +0,0 @@ -#!/usr/bin/with-contenv bash -# shellcheck shell=bash - -if [[ "${#MYSQL_ROOT_PASSWORD}" -gt "3" ]]; then - # display a message about upgrading database if needed - if mariadb-upgrade -u root -p"${MYSQL_ROOT_PASSWORD}" --check-if-upgrade-is-needed >/dev/null 2>&1; then - cat <<-EOF - - - -################################################################# -# # -# An upgrade is required on your databases. # -# # -# Stop any services that are accessing databases # -# in this container, and then run the command # -# # -# mariadb-upgrade -u root -p # -# # -################################################################# - - - -EOF - sleep 5s - fi -fi diff --git a/root/etc/s6-overlay/s6-rc.d/init-mariadb-upgrade/up b/root/etc/s6-overlay/s6-rc.d/init-mariadb-upgrade/up deleted file mode 100644 index 8e3e9f0..0000000 --- a/root/etc/s6-overlay/s6-rc.d/init-mariadb-upgrade/up +++ /dev/null @@ -1 +0,0 @@ -/etc/s6-overlay/s6-rc.d/init-mariadb-upgrade/run diff --git a/root/etc/s6-overlay/s6-rc.d/user/contents.d/init-mariadb-upgrade b/root/etc/s6-overlay/s6-rc.d/user/contents.d/init-mariadb-poststart similarity index 100% rename from root/etc/s6-overlay/s6-rc.d/user/contents.d/init-mariadb-upgrade rename to root/etc/s6-overlay/s6-rc.d/user/contents.d/init-mariadb-poststart