123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #!/usr/bin/env bash
- set -eu
- script_name=$0
- DB_BACKEND=$(echo $script_name | cut -d- -f2)
- export DB_BACKEND
- die() {
- echo >&2 "$@"
- exit 1
- }
- MYSQL_HOST=${MYSQL_HOST:-127.0.0.1}
- MYSQL_PORT=${MYSQL_PORT:-3306}
- MYSQL_PASSWORD=${MYSQL_PASSWORD:-password}
- MYSQL_USER=${MYSQL_USER:-root}
- about() {
- die "usage: $script_name [ config_yaml | setup | dump <backup_file> | restore <backup_file> ]"
- }
- check_mysql_client() {
- if ! command -v mysql >/dev/null; then
- die "missing required program 'mysql' as a mysql client (package mariadb-client-core-10.6 on debian like system)"
- fi
- }
- silence_password_warning() {
- ( ( ( "$@" >&9 ) 2>&1 \
- | fgrep -v "[Warning] Using a password on the command line interface can be insecure." ) >&2 ) 9>&1 || [[ $? == 1 ]]
- }
- exec_sql() {
- cmd="${1?Missing required sql command}"
- silence_password_warning \
- mysql \
- "--host=${MYSQL_HOST}" \
- "--user=${MYSQL_USER}" \
- "--port=${MYSQL_PORT}" \
- "--password=${MYSQL_PASSWORD}" <<< "$cmd"
- }
- requirements() {
- check_mysql_client
- }
- setup() {
- exec_sql "DROP DATABASE IF EXISTS crowdsec_test;"
- exec_sql "CREATE DATABASE crowdsec_test;"
- exec_sql "DROP USER IF EXISTS crowdsec_test;"
- exec_sql "CREATE USER 'crowdsec_test' IDENTIFIED BY 'crowdsec_test';"
- exec_sql "GRANT ALL PRIVILEGES ON crowdsec_test.* TO 'crowdsec_test';"
- }
- dump() {
- backup_file="${1?Missing file to backup database to}"
- COLUMN_STATISTICS=
- if mysqldump --column-statistics 2>&1 | grep -q -v 'unknown option'; then
- COLUMN_STATISTICS='--column-statistics=0'
- fi
- silence_password_warning \
- mysqldump \
- $COLUMN_STATISTICS \
- "--host=${MYSQL_HOST}" \
- "--port=${MYSQL_PORT}" \
- "--user=${MYSQL_USER}" \
- "--password=${MYSQL_PASSWORD}" \
- --databases crowdsec_test > "$backup_file"
- }
- restore() {
- backup_file="${1?missing file to restore database from}"
- [ -f "$backup_file" ] || die "Backup file $backup_file doesn't exist"
- silence_password_warning \
- mysql \
- "--host=${MYSQL_HOST}" \
- "--user=${MYSQL_USER}" \
- "--port=${MYSQL_PORT}" \
- "--password=${MYSQL_PASSWORD}" < "$backup_file"
- exec_sql "DROP USER IF EXISTS 'crowdsec_test';"
- exec_sql "CREATE USER 'crowdsec_test' IDENTIFIED BY 'crowdsec_test';"
- exec_sql "GRANT ALL PRIVILEGES ON crowdsec_test.* TO 'crowdsec_test';"
- }
- config_yaml() {
- MYSQL_PORT=${MYSQL_PORT} MYSQL_HOST=${MYSQL_HOST} yq '
- .db_config.type="$DB_BACKEND"|
- .db_config.user="crowdsec_test" |
- .db_config.password="crowdsec_test" |
- .db_config.db_name="crowdsec_test" |
- .db_config.host=strenv(MYSQL_HOST) |
- .db_config.port=env(MYSQL_PORT) |
- del(.db_config.db_path)
- ' -i "${CONFIG_YAML}"
- }
- [ $# -lt 1 ] && about
- case "$1" in
- setup)
- setup
- ;;
- config-yaml)
- config_yaml
- ;;
- dump)
- shift
- dump "$@"
- ;;
- restore)
- shift
- restore "$@"
- ;;
- *)
- about
- ;;
- esac;
|