instance-mysql 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env bash
  2. set -eu
  3. script_name=$0
  4. DB_BACKEND=$(echo "${script_name}" | cut -d- -f2)
  5. export DB_BACKEND
  6. die() {
  7. echo >&2 "$@"
  8. exit 1
  9. }
  10. MYSQL_HOST=${MYSQL_HOST:-127.0.0.1}
  11. MYSQL_PORT=${MYSQL_PORT:-3306}
  12. MYSQL_PASSWORD=${MYSQL_PASSWORD:-password}
  13. MYSQL_USER=${MYSQL_USER:-root}
  14. about() {
  15. die "usage: ${script_name} [ config_yaml | setup | dump <backup_file> | restore <backup_file> ]"
  16. }
  17. check_requirements() {
  18. if ! command -v mysql >/dev/null; then
  19. die "missing required program 'mysql' as a mysql client (package mariadb-client-core-10.6 on debian like system)"
  20. fi
  21. }
  22. silence_password_warning() {
  23. ( ( ( "$@" >&9 ) 2>&1 \
  24. | grep -F -v "[Warning] Using a password on the command line interface can be insecure." ) >&2 ) 9>&1 || [[ $? == 1 ]]
  25. }
  26. exec_sql() {
  27. cmd="${1?Missing required sql command}"
  28. silence_password_warning \
  29. mysql \
  30. "--host=${MYSQL_HOST}" \
  31. "--user=${MYSQL_USER}" \
  32. "--port=${MYSQL_PORT}" \
  33. "--password=${MYSQL_PASSWORD}" <<< "${cmd}"
  34. }
  35. setup() {
  36. exec_sql "DROP DATABASE IF EXISTS crowdsec_test;"
  37. exec_sql "CREATE DATABASE crowdsec_test;"
  38. exec_sql "DROP USER IF EXISTS crowdsec_test;"
  39. exec_sql "CREATE USER 'crowdsec_test' IDENTIFIED BY 'crowdsec_test';"
  40. exec_sql "GRANT ALL PRIVILEGES ON crowdsec_test.* TO 'crowdsec_test';"
  41. }
  42. dump() {
  43. backup_file="${1?Missing file to backup database to}"
  44. args=(mysqldump)
  45. if mysqldump --column-statistics 2>&1 | grep -q -v 'unknown option'; then
  46. args+=("--column-statistics=0")
  47. fi
  48. args+=("--host=${MYSQL_HOST}" "--port=${MYSQL_PORT}" "--user=${MYSQL_USER}" "--password=${MYSQL_PASSWORD}" --databases crowdsec_test)
  49. silence_password_warning "${args[@]}" > "${backup_file}"
  50. }
  51. restore() {
  52. backup_file="${1?missing file to restore database from}"
  53. [[ -f "${backup_file}" ]] || die "Backup file ${backup_file} doesn't exist"
  54. silence_password_warning \
  55. mysql \
  56. "--host=${MYSQL_HOST}" \
  57. "--user=${MYSQL_USER}" \
  58. "--port=${MYSQL_PORT}" \
  59. "--password=${MYSQL_PASSWORD}" < "${backup_file}"
  60. exec_sql "DROP USER IF EXISTS 'crowdsec_test';"
  61. exec_sql "CREATE USER 'crowdsec_test' IDENTIFIED BY 'crowdsec_test';"
  62. exec_sql "GRANT ALL PRIVILEGES ON crowdsec_test.* TO 'crowdsec_test';"
  63. }
  64. config_yaml() {
  65. MYSQL_PORT=${MYSQL_PORT} MYSQL_HOST=${MYSQL_HOST} yq e '
  66. .db_config.type=strenv(DB_BACKEND)|
  67. .db_config.user="crowdsec_test" |
  68. .db_config.password="crowdsec_test" |
  69. .db_config.db_name="crowdsec_test" |
  70. .db_config.host=strenv(MYSQL_HOST) |
  71. .db_config.port=env(MYSQL_PORT) |
  72. del(.db_config.db_path)
  73. ' -i "${CONFIG_YAML}"
  74. }
  75. [[ $# -lt 1 ]] && about
  76. check_requirements
  77. case "$1" in
  78. setup)
  79. setup
  80. ;;
  81. config-yaml)
  82. config_yaml
  83. ;;
  84. dump)
  85. shift
  86. dump "$@"
  87. ;;
  88. restore)
  89. shift
  90. restore "$@"
  91. ;;
  92. exec_sql)
  93. shift
  94. #
  95. # This command is meant to run a query against the the crowdsec database.
  96. # The exec_sql() function is more generic and is also used for database setup and backups.
  97. #
  98. # For this reason, we select the database here.
  99. #
  100. exec_sql "use crowdsec_test; $@"
  101. ;;
  102. *)
  103. about
  104. ;;
  105. esac;