diff --git a/bin/database.sh b/bin/database.sh index ec1462d..f127eb2 100755 --- a/bin/database.sh +++ b/bin/database.sh @@ -8,6 +8,7 @@ SQL_PASS='' ANY="'%'" SET_OK=0 EPACE=' ' +METHOD=0 echow(){ FLAG=${1} @@ -23,6 +24,9 @@ help_message(){ echow '-D, --domain [DOMAIN_NAME] -U, --user [xxx] -P, --password [xxx] -DB, --database [xxx]' echo "${EPACE}${EPACE}Example: database.sh -D example.com -U USERNAME -P PASSWORD -DB DATABASENAME" echo "${EPACE}${EPACE}Will create Database/username/password by given" + echow '-r, --delete [DOMAIN_NAME] -DB, --database [xxx] -U, --user [xxx]' + echo "${EPACE}${EPACE}Example: database.sh -r example.com -DB DATABASENAME -U USERNAME" + echo "${EPACE}${EPACE}Will delete database (require) and username (optional) by given" echow '-H, --help' echo "${EPACE}${EPACE}Display help and exit." exit 0 @@ -94,6 +98,14 @@ check_db_exist(){ fi } +check_db_not_exist(){ + docker compose exec -T mysql su -c "test -e /var/lib/mysql/${1}" + if [ ${?} != 0 ]; then + echo "Database ${1} doesn't exist, skip DB deletion!" + exit 0 + fi +} + db_setup(){ docker compose exec -T mysql su -c 'mysql -uroot -p${MYSQL_ROOT_PASSWORD} \ -e "CREATE DATABASE '${SQL_DB}';" \ @@ -102,6 +114,22 @@ db_setup(){ SET_OK=${?} } +db_delete(){ + if [ "${SQL_DB}" == '' ]; then + echo "Database parameter is required!" + exit 0 + fi + if [ "${SQL_USER}" == '' ]; then + SQL_USER="${SQL_DB}" + fi + check_db_not_exist ${SQL_DB} + docker compose exec -T mysql su -c 'mysql -uroot -p${MYSQL_ROOT_PASSWORD} \ + -e "DROP DATABASE IF EXISTS '${SQL_DB}';" \ + -e "DROP USER IF EXISTS '${SQL_USER}'@'${ANY}';" \ + -e "FLUSH PRIVILEGES;"' + echo "Database ${SQL_DB} and User ${SQL_USER} are deleted!" +} + auto_setup_main(){ check_input ${DOMAIN} gen_pass @@ -124,6 +152,10 @@ specify_setup_main(){ } main(){ + if [ ${METHOD} == 1 ]; then + db_delete + exit 0 + fi if [ "${SQL_USER}" != '' ] && [ "${SQL_PASS}" != '' ] && [ "${SQL_DB}" != '' ]; then specify_setup_main else @@ -148,7 +180,11 @@ while [ ! -z "${1}" ]; do ;; -db | -DB | -database| --database) shift SQL_DB="${1}" - ;; + ;; + -[rR] | -del | --del | --delete) shift + DOMAIN="${1}" + METHOD=1 + ;; *) help_message ;; diff --git a/docker-compose.yml b/docker-compose.yml index 0f77563..a94ba0d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,7 +4,7 @@ services: image: mariadb:10.5.9 logging: driver: none - command: --max_allowed_packet=256M + command: ["--max-allowed-packet=512M"] volumes: - "./data/db:/var/lib/mysql:delegated" environment: @@ -48,6 +48,19 @@ services: restart: always networks: - default + redis: + image: "redis:alpine" + logging: + driver: none + # command: redis-server --requirepass 8b405f60665e48f795752e534d93b722 + volumes: + - ./redis/data:/var/lib/redis + - ./redis/redis.conf:/usr/local/etc/redis/redis.conf + environment: + - REDIS_REPLICATION_MODE=master + restart: always + networks: + - default networks: default: driver: bridge \ No newline at end of file