Luigi Santivetti | b52d6d2 | 2020-05-18 00:56:27 +0100 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # mysql templates |
| 4 | # |
| 5 | # Copyright 2019 Luigi Santivetti <luigi.santivetti@gmail.com> |
| 6 | |
| 7 | # Permission is hereby granted, free of charge, to any person obtaining a |
| 8 | # copy of this software and associated documentation files (the "Software"), |
| 9 | # to deal in the Software without restriction, including without limitation |
| 10 | # the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 11 | # and/or sell copies of the Software, and to permit persons to whom the |
| 12 | # Software is furnished to do so, subject to the following conditions: |
| 13 | |
| 14 | # The above copyright notice and this permission notice (including the next |
| 15 | # paragraph) shall be included in all copies or substantial portions of the |
| 16 | # Software. |
| 17 | |
| 18 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 21 | # ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 22 | # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 23 | # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | |
| 25 | declare -r mycnf_t="\ |
| 26 | # Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
| 27 | # |
| 28 | # This program is free software; you can redistribute it and/or modify |
| 29 | # it under the terms of the GNU General Public License as published by |
| 30 | # the Free Software Foundation; version 2 of the License. |
| 31 | # |
| 32 | # This program is distributed in the hope that it will be useful, |
| 33 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 34 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 35 | # GNU General Public License for more details. |
| 36 | # |
| 37 | # You should have received a copy of the GNU General Public License |
| 38 | # along with this program; if not, write to the Free Software |
| 39 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 40 | # |
| 41 | # https://dev.mysql.com/doc/refman/${_MYSQL_DKRC_VERSION_:0:3}/en/server-system-variables.html |
| 42 | |
| 43 | !includedir /etc/mysql/conf.d/ |
| 44 | !includedir /etc/mysql/mysql.conf.d/ |
| 45 | |
| 46 | [mysqld] |
| 47 | |
| 48 | # Always enable SSL |
| 49 | require_secure_transport = ON |
| 50 | |
| 51 | # Certificates (use self signed) |
| 52 | ssl-cert=${_MYSQL_SSL_SERVER_CERT_F_} |
| 53 | ssl-key=${_MYSQL_SSL_SERVER_KEY_F_} |
| 54 | ssl-ca=${_MYSQL_SSL_SERVER_CA_F_} |
| 55 | |
| 56 | # Logs |
| 57 | general_log = ${_MYSQL_GENERAL_LOG_} |
| 58 | log_output = ${_MYSQL_LOG_OUTPUT_} |
| 59 | slow_query_log = ${_MYSQL_SLOW_QUERY_LOG_} |
| 60 | long_query_time = 2 |
| 61 | log_error_verbosity = 3 |
| 62 | general_log_file = ${_MYSQL_CON_GENERAL_LOG_F_} |
| 63 | slow_query_log_file = ${_MYSQL_CON_SLOW_QUERY_LOG_F_} |
| 64 | log_error = ${_MYSQL_CON_ERROR_LOG_F_} |
| 65 | |
| 66 | port=${_MYSQL_DB_PORT_} |
| 67 | explicit_defaults_for_timestamp = TRUE |
| 68 | #connect_timeout = 1000000 |
| 69 | #net_write_timeout = 1000000 |
| 70 | #wait_timeout = 1000000 |
| 71 | #max_allowed_packet = 1024M |
| 72 | #interactive_timeout = 1000000 |
| 73 | #net_buffer_length = 200M |
| 74 | #net_read_timeout = 1000000 |
| 75 | #bind-address = * |
| 76 | #innodb_data_file_path= |
| 77 | |
| 78 | #[mysqld_safe]" |
| 79 | |
| 80 | declare -r initdb_t="\ |
| 81 | CREATE DATABASE IF NOT EXISTS \`${_MYSQL_DB_NAME_}\`; |
| 82 | |
| 83 | use ${_MYSQL_DB_NAME_}; |
| 84 | |
| 85 | CREATE TABLE IF NOT EXISTS \`${_MYSQL_DB_TABLE_USERS_}\` ( |
| 86 | \`id\` int(10) unsigned NOT NULL AUTO_INCREMENT, |
| 87 | \`${_MYSQL_DB_FIELD_USERNAME_}\` varchar(20) NOT NULL, |
| 88 | \`group\` int(10) unsigned NOT NULL default 1, |
| 89 | \`status\` int(5) unsigned NOT NULL default 0, |
| 90 | \`email\` varchar(50) NOT NULL, |
| 91 | \`${_MYSQL_DB_FIELD_PASSWORD_}\` varchar(64) NOT NULL, |
| 92 | \`created\` datetime NOT NULL default current_timestamp, |
| 93 | \`updated\` timestamp NOT NULL default current_timestamp on update current_timestamp, |
| 94 | PRIMARY KEY (\`id\`), |
| 95 | UNIQUE KEY useremail (\`email\`) |
| 96 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
| 97 | |
| 98 | CREATE TABLE IF NOT EXISTS \`${_MYSQL_DB_TABLE_SESSIONS_}\` ( |
| 99 | \`id\` varchar(128) NOT NULL, |
| 100 | \`ip_address\` varchar(45) NOT NULL, |
| 101 | \`timestamp\` int(10) unsigned DEFAULT 0 NOT NULL, |
| 102 | \`data\` blob NOT NULL, |
| 103 | PRIMARY KEY \`ci_sessions_id\` (\`id\`) |
| 104 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;" |
| 105 | |
| 106 | if [ "${mod_mode}" = "${release}" ]; then |
| 107 | declare -rg dockerfile_debug_tools_t="" |
| 108 | else |
| 109 | declare -rg dockerfile_debug_tools_t="\ |
| 110 | # debug only |
| 111 | RUN apt-get -y install nmap |
| 112 | RUN apt-get -y install net-tools" |
| 113 | fi |
| 114 | |
| 115 | declare -r dockerfile_t="\ |
| 116 | ARG VERSION=${_MYSQL_DKRC_VERSION_} |
| 117 | FROM mysql:\$VERSION |
| 118 | |
| 119 | RUN apt-get update |
| 120 | ${dockerfile_debug_tools_t} |
| 121 | |
| 122 | # Add SQL configuration file into autoloaded default location |
| 123 | COPY initdb.sql /docker-entrypoint-initdb.d/ |
| 124 | |
| 125 | # MYSQL seems quite strict about files mode |
| 126 | RUN mkdir -p ${_MYSQL_CON_LOG_D_} && \\ |
| 127 | chown mysql:mysql /var/log/mysql && \\ |
| 128 | touch ${_MYSQL_CON_GENERAL_LOG_F_} && \\ |
| 129 | chown mysql:mysql ${_MYSQL_CON_GENERAL_LOG_F_} && \\ |
| 130 | touch ${_MYSQL_CON_ERROR_LOG_F_} && \\ |
| 131 | chown mysql:mysql ${_MYSQL_CON_ERROR_LOG_F_} && \\ |
| 132 | touch ${_MYSQL_CON_SLOW_QUERY_LOG_F_} && \\ |
| 133 | chown mysql:mysql ${_MYSQL_CON_SLOW_QUERY_LOG_F_}" |
| 134 | |
| 135 | declare -r mysql_cli_bang_t="\ |
| 136 | #!/bin/bash |
| 137 | |
| 138 | if (return 0 2>/dev/null); then |
| 139 | echo \"You must run this script\" >&2 |
| 140 | return 1 |
| 141 | fi |
| 142 | |
| 143 | declare -r running_services=\\ |
| 144 | \"sudo -E docker-compose ps --services --filter \\\"status=running\\\"\" |
| 145 | |
| 146 | function source_passwd_file |
| 147 | { |
| 148 | sudo whoami >/dev/null # cache password |
| 149 | |
| 150 | if [ ! -f \"\${PASSWD_F}\" ]; then |
| 151 | echo \"error: PASSWD file not found\" >&2 |
| 152 | exit 1 |
| 153 | fi |
| 154 | |
| 155 | if ! source \"\${PASSWD_F}\"; then |
| 156 | echo \"error: PASSWD file not sourced\" >&2 |
| 157 | exit 1 |
| 158 | fi |
| 159 | } |
| 160 | |
| 161 | set -eu |
| 162 | |
| 163 | case \"\$1\" in |
| 164 | --help ) cat <<EOF; exit 0 |
| 165 | `printf \"\\033[1m%s\\033[0m\\n\" \"NAME\"` |
| 166 | |
| 167 | \${BASH_SOURCE[0]//.\//} - Mysql CLI for ${host_name} |
| 168 | |
| 169 | `printf \"\\033[1m%s\\033[0m\\n\" \"USAGE\"` |
| 170 | |
| 171 | \$ \${BASH_SOURCE[0]} [ OPTION ] [ ARGS ... ] |
| 172 | |
| 173 | `printf \"\\033[1m%s\\033[0m\\n\" \"ENVIRONMENT\"` |
| 174 | |
| 175 | PASSWD_F path to file containing runtime credentials |
| 176 | |
| 177 | `printf \"\\033[1m%s\\033[0m\\n\" \"OPTION\"` |
| 178 | |
| 179 | --set-status string <email> integer <status>. Set the status |
| 180 | field for a user given its email |
| 181 | --export export mysql database to utf-8 text file |
| 182 | --import import a mysql database from an utf-8 text file |
| 183 | --help show this help |
| 184 | |
| 185 | `printf \"\\033[1m%s\\033[0m\\n\" \"END\"` |
| 186 | EOF |
| 187 | ;; |
| 188 | --export ) |
| 189 | source_passwd_file |
| 190 | declare -r now=\"\$(date +'%d%m%Y%H%M%S')\" |
| 191 | declare -r backup_name=\"${_MYSQL_DKRC_CONTAINER_}_${_MYSQL_DB_NAME_}-\${now}.sql\" |
| 192 | declare -r backup_path=\"${_MYSQL_BACKUP_PATH_}\" |
| 193 | declare -r backup_file=\"\${backup_path}/\${backup_name}\" |
| 194 | |
| 195 | pushd \"${instance_d}\" >/dev/null || exit 1 |
| 196 | if eval \"\${running_services}\" | grep -q -- \"${_MYSQL_DKRC_SERVICE_}\"; then |
| 197 | if ! (sudo -E docker exec ${_MYSQL_DKRC_CONTAINER_} /usr/bin/mysqldump \\ |
| 198 | -u root --password=\${_PASSWD_MYSQL_DB_PASSWORD_ROOT_} \\ |
| 199 | --databases ${_MYSQL_DB_NAME_} --result-file=\${backup_file}); then |
| 200 | echo \"error: cannot export database\" |
| 201 | popd >/dev/null |
| 202 | exit 1 |
| 203 | fi |
| 204 | sudo chmod 0400 ${_MYSQL_MYSQL_D_}/\${backup_name} |
| 205 | suod mv ${_MYSQL_MYSQL_D_}/\${backup_name} \$(pwd) |
| 206 | echo \"success: \$(pwd)/\${backup_name}\" |
| 207 | else |
| 208 | echo \"error: ${_MYSQL_DKRC_SERVICE_} not running\" |
| 209 | popd >/dev/null |
| 210 | exit 1 |
| 211 | fi ;; |
| 212 | --import ) |
| 213 | source_passwd_file |
| 214 | declare -r input_sql_bkp=\"\$(realpath \"\$2\")\" |
| 215 | if [ ! -f \"\${input_sql_bkp}\" ]; then |
| 216 | echo \"error: \${input_sql_bkp:-undefined}: file not found\" |
| 217 | exit 1 |
| 218 | fi |
| 219 | |
| 220 | pushd \"${instance_d}\" >/dev/null || exit 1 |
| 221 | if eval \"\${running_services}\" | grep -q -- \"${_MYSQL_DKRC_SERVICE_}\"; then |
| 222 | if ! (sudo cat \"\${input_sql_bkp}\" | sudo -E docker exec -i ${_MYSQL_DKRC_CONTAINER_} \\ |
| 223 | /usr/bin/mysql -u root --password=\${_PASSWD_MYSQL_DB_PASSWORD_ROOT_} ${_MYSQL_DB_NAME_}); then |
| 224 | echo \"error: cannot import database\" |
| 225 | popd >/dev/null |
| 226 | exit 1 |
| 227 | fi |
| 228 | echo \"success: \${input_sql_bkp}\" |
| 229 | else |
| 230 | echo \"error: ${_MYSQL_DKRC_SERVICE_} not running\" |
| 231 | popd >/dev/null |
| 232 | exit 1 |
| 233 | fi ;; |
| 234 | --set-status ) |
| 235 | source_passwd_file |
| 236 | declare -r email=\"\$2\" |
| 237 | declare -r status=\"\$3\" |
| 238 | declare -r query=\"UPDATE users SET status = \${status} WHERE email = '\${email}'\" |
| 239 | |
| 240 | pushd \"${instance_d}\" >/dev/null || exit 1 |
| 241 | if eval \"\${running_services}\" | grep -q -- \"${_MYSQL_DKRC_SERVICE_}\"; then |
| 242 | if ! (sudo -E docker exec -i ${_MYSQL_DKRC_CONTAINER_} \\ |
| 243 | /usr/bin/mysql -u root --password=\${_PASSWD_MYSQL_DB_PASSWORD_ROOT_} \\ |
| 244 | -D ${_MYSQL_DB_NAME_} -e \"\${query}\"); then |
| 245 | echo \"error: cannot update status\" |
| 246 | popd >/dev/null |
| 247 | exit 1 |
| 248 | fi |
| 249 | echo \"success: status updated\" |
| 250 | else |
| 251 | echo \"error: ${_MYSQL_DKRC_SERVICE_} not running\" |
| 252 | popd >/dev/null |
| 253 | exit 1 |
| 254 | fi ;; |
| 255 | esac" |