Skip to content

Commit

Permalink
Merge pull request #68 from travistran1989/master
Browse files Browse the repository at this point in the history
add redis container and support database deletion
  • Loading branch information
Code-Egg authored Jan 3, 2024
2 parents 3ca69ed + 51e9658 commit 3ee0c98
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
38 changes: 37 additions & 1 deletion bin/database.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ SQL_PASS=''
ANY="'%'"
SET_OK=0
EPACE=' '
METHOD=0

echow(){
FLAG=${1}
Expand All @@ -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
Expand Down Expand Up @@ -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}';" \
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
;;
Expand Down
15 changes: 14 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

0 comments on commit 3ee0c98

Please sign in to comment.