instance-mysql 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. COLUMN_STATISTICS=
  45. if mysqldump --column-statistics 2>&1 | grep -q -v 'unknown option'; then
  46. COLUMN_STATISTICS='--column-statistics=0'
  47. fi
  48. silence_password_warning \
  49. mysqldump \
  50. "${COLUMN_STATISTICS}" \
  51. "--host=${MYSQL_HOST}" \
  52. "--port=${MYSQL_PORT}" \
  53. "--user=${MYSQL_USER}" \
  54. "--password=${MYSQL_PASSWORD}" \
  55. --databases crowdsec_test > "${backup_file}"
  56. }
  57. restore() {
  58. backup_file="${1?missing file to restore database from}"
  59. [[ -f "${backup_file}" ]] || die "Backup file ${backup_file} doesn't exist"
  60. silence_password_warning \
  61. mysql \
  62. "--host=${MYSQL_HOST}" \
  63. "--user=${MYSQL_USER}" \
  64. "--port=${MYSQL_PORT}" \
  65. "--password=${MYSQL_PASSWORD}" < "${backup_file}"
  66. exec_sql "DROP USER IF EXISTS 'crowdsec_test';"
  67. exec_sql "CREATE USER 'crowdsec_test' IDENTIFIED BY 'crowdsec_test';"
  68. exec_sql "GRANT ALL PRIVILEGES ON crowdsec_test.* TO 'crowdsec_test';"
  69. }
  70. config_yaml() {
  71. MYSQL_PORT=${MYSQL_PORT} MYSQL_HOST=${MYSQL_HOST} yq e '
  72. .db_config.type=strenv(DB_BACKEND)|
  73. .db_config.user="crowdsec_test" |
  74. .db_config.password="crowdsec_test" |
  75. .db_config.db_name="crowdsec_test" |
  76. .db_config.host=strenv(MYSQL_HOST) |
  77. .db_config.port=env(MYSQL_PORT) |
  78. del(.db_config.db_path)
  79. ' -i "${CONFIG_YAML}"
  80. }
  81. [[ $# -lt 1 ]] && about
  82. check_requirements
  83. case "$1" in
  84. setup)
  85. setup
  86. ;;
  87. config-yaml)
  88. config_yaml
  89. ;;
  90. dump)
  91. shift
  92. dump "$@"
  93. ;;
  94. restore)
  95. shift
  96. restore "$@"
  97. ;;
  98. exec_sql)
  99. shift
  100. exec_sql "$@"
  101. ;;
  102. *)
  103. about
  104. ;;
  105. esac;