instance-mysql 3.0 KB

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