Skip to content

Commit

Permalink
DB Backup/Restore POC (#214)
Browse files Browse the repository at this point in the history
* DB Backup/Restore POC

* Bump version
  • Loading branch information
mathew-fleisch committed Jul 30, 2022
1 parent 2becd70 commit 14edb68
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 21 deletions.
4 changes: 4 additions & 0 deletions .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export DB_ROOT_PASSWORD=password
export DB_USER=root
export DB_DUMP_FILENAME=bot-dump.sql
export DB_CONTAINER_NAME=fodmysql
export DB_BACKUP_DEPLOY_KEY=Tm90IGFjdHVhbGx5IGEgcHJpdmF0ZSBrZXkuLi4g8J+ZhAo=
export DB_BACKUP_SUB_DIR=agimus
export DB_BACKUP_GITHUB_USER=
export DB_BACKUP_GITHUB_EMAIL=

# Path to configuration json
export BOT_CONTAINER_NAME=agimus
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/pr-test-and-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ jobs:
sleep 5
echo "Delete (restart) AGIMUS pod now that MySQL should be running and seeded..."
kubectl --namespace agimus delete pod $(kubectl --namespace agimus get pods | grep agimus | awk '{print $1}') || true
sleep 3
echo "If after 20 seconds the string 'BOT IS ONLINE AND READY FOR COMMANDS' is not present in the logs, the test has failed..."
sleep 20
timeout 20s kubectl --namespace agimus logs -f $(kubectl --namespace agimus get pods | grep agimus | awk '{print $1}') || true
bash_test="$(kubectl --namespace agimus logs $(kubectl --namespace agimus get pods | grep agimus | awk '{print $1}') | grep 'BOT IS ONLINE AND READY FOR COMMANDS')"
echo "$bash_test"
if [[ -n "$bash_test" ]]; then
Expand Down Expand Up @@ -127,7 +128,7 @@ jobs:
echo "Configmaps:"
kubectl --namespace agimus get configmaps
echo "Agimus Config:"
kubectl --namespace agimus get configmaps agimus-config -o yaml
kubectl --namespace agimus get configmaps agimus-config -o jsonpath='{.data.local\.json}' | jq '.' || true
echo "Describe Agimus pod:"
kubectl --namespace agimus describe pod $(kubectl --namespace agimus get pods | grep agimus | awk '{print $1}') || true
echo "Logs:"
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,6 @@ images/ep.jpg
images/slot_results/*.png
images/trades/*.png
!images/slot_results/template*
!images/profiles/template*
!images/profiles/template*
database/*
database
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ FROM ubuntu:20.04
RUN rm /bin/sh && ln -s /bin/bash /bin/sh \
&& apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
curl wget apt-utils python3 python3-pip make build-essential locales openssl git jq tzdata sudo lsb-release \
curl wget apt-utils python3 python3-pip make build-essential locales openssl git jq tzdata sudo lsb-release mysql-client \
&& touch /etc/sudoers.d/bot-user \
&& echo "bot ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/bot-user \
&& useradd -ms /bin/bash bot \
Expand Down
64 changes: 49 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ BOT_CONTAINER_NAME?=agimus
LOCAL_KIND_CONFIG?=kind-config.yaml
namespace?=agimus
SHELL=/bin/bash
DB_BACKUP_RESTORE_COMMIT?=main
ifneq (,$(wildcard ./.env))
include .env
export
Expand Down Expand Up @@ -70,30 +71,63 @@ docker-lint: ## Lint the container with dockle

.PHONY: db-mysql
db-mysql: ## MySQL session in running db container
@docker-compose exec db mysql -u"${DB_USER}" -p"${DB_PASS}" "${DB_NAME}"
@docker-compose exec db mysql -u$(DB_USER) -p$(DB_PASS) $(DB_NAME)

.PHONY: db-bash
db-bash: ## Bash session in running db container
@docker-compose exec db bash

.PHONY: db-dump
db-dump: ## Dump the database to a file at ./$DB_DUMP_FILENAME
@docker-compose exec db bash -c 'mysqldump -u"${DB_USER}" -p"${DB_PASS}" -B ${DB_NAME} 2>/dev/null' > ./${DB_DUMP_FILENAME}
db-dump: ## Dump the database to a file at $DB_DUMP_FILENAME
@docker-compose exec app mysqldump -h$(DB_HOST) -u$(DB_USER) -p$(DB_PASS) -B $(DB_NAME) 2>/dev/null > $(DB_DUMP_FILENAME)

.PHONY: db-load
db-load: ## Load the database from a file at ./$DB_DUMP_FILENAME
@docker-compose exec -T db sh -c 'exec mysql -u"${DB_USER}" -p"${DB_PASS}" "${DB_NAME}"' < ./${DB_DUMP_FILENAME}
db-load: ## Load the database from a file at $DB_DUMP_FILENAME
@docker-compose exec app mysql -h$(DB_HOST) -u$(DB_USER) -p$(DB_PASS) $(DB_NAME) < $(DB_DUMP_FILENAME)

.PHONY: db-seed
db-seed: ## Reload the database from a file at ./$DB_SEED_FILEPATH
@docker-compose exec -T db sh -c 'exec mysql -u"${DB_USER}" -p"${DB_PASS}" <<< "DROP DATABASE IF EXISTS FoD;"'
@docker-compose exec -T db sh -c 'exec mysql -u"${DB_USER}" -p"${DB_PASS}" <<< "create database FoD;"'
@docker-compose exec -T db sh -c 'exec mysql -u"${DB_USER}" -p"${DB_PASS}" "${DB_NAME}"' < ./${DB_SEED_FILEPATH}

# mysql session in pod
# kubectl exec -it my-cluster-mysql-0 -c mysql -- mysql -uroot -ppassword
# Run sql file in pod
# kubectl exec my-cluster-mysql-0 -c mysql -- mysql -uroot -ppassword < bot-dump.sql
db-seed: ## Reload the database from a file at $DB_SEED_FILEPATH
@docker-compose exec -T app mysql -h$(DB_HOST) -u$(DB_USER) -p$(DB_PASS) <<< "DROP DATABASE IF EXISTS FoD;"
@docker-compose exec -T app mysql -h$(DB_HOST) -u$(DB_USER) -p$(DB_PASS) <<< "create database FoD;"
@docker-compose exec -T app mysql -h$(DB_HOST) -u$(DB_USER) -p$(DB_PASS) $(DB_NAME) < $(DB_SEED_FILEPATH)

.PHONY: db-backup
db-backup: db-setup-git ## Back the database to a file at $DB_DUMP_FILENAME then commit it to the private database repository (intended to run inside AGIMUS container)
@echo "Dumping db to $(DB_DUMP_FILENAME)"
@mysqldump -h$(DB_HOST) -u$(DB_USER) -p$(DB_PASS) -B $(DB_NAME) > $(DB_DUMP_FILENAME)
@rm -rf database || true
git clone git@github.com:Friends-of-DeSoto/database.git
@mkdir -p database/$(DB_BACKUP_SUB_DIR)
cp $(DB_DUMP_FILENAME) database/$(DB_BACKUP_SUB_DIR)/agimus.sql
cd database \
&& git add $(DB_BACKUP_SUB_DIR)/agimus.sql \
&& git commit -m "Backup AGIMUS DB: $(DB_BACKUP_SUB_DIR) $(shell date)" \
&& git push origin main

.PHONY: db-restore
db-restore: db-setup-git ## Restore the database from the private database repository (intended to run inside AGIMUS container)
@rm -rf database || true
git clone git@github.com:Friends-of-DeSoto/database.git \
&& cd database \
&& git reset --hard $(DB_BACKUP_RESTORE_COMMIT)
@mkdir -p database/$(DB_BACKUP_SUB_DIR)
cp database/$(DB_BACKUP_SUB_DIR)/agimus.sql $(DB_DUMP_FILENAME)
@mysql -h$(DB_HOST) -u$(DB_USER) -p$(DB_PASS) <<< "DROP DATABASE IF EXISTS FoD;"
@mysql -h$(DB_HOST) -u$(DB_USER) -p$(DB_PASS) <<< "create database FoD;"
@mysql -h$(DB_HOST) -u$(DB_USER) -p$(DB_PASS) $(DB_NAME) < $(DB_DUMP_FILENAME)

.PHONY: db-setup-git
db-setup-git: ## Decode private deploy key environment variable and set up git for that user (intended to run inside AGIMUS container)
ifneq ("$(wildcard $(HOME)/.ssh/id_ed25519)","")
@echo "$(HOME)/.ssh/id_ed25519 - ssh key already exists. skipping git setup..."
else
mkdir -p $(HOME)/.ssh
@echo $(DB_BACKUP_DEPLOY_KEY) | base64 -d > $(HOME)/.ssh/id_ed25519
chmod 600 $(HOME)/.ssh/id_ed25519
git config --global user.name $(DB_BACKUP_GITHUB_USER)
git config --global user.email $(DB_BACKUP_GITHUB_EMAIL)
ssh-keyscan github.com >> $(HOME)/.ssh/known_hosts
endif

##@ Kubernetes in Docker (KinD) stuff

Expand Down Expand Up @@ -155,7 +189,7 @@ helm-uninstall: helm-config-rm ## Remove AGIMUS helm chart
@helm --namespace $(namespace) delete agimus

.PHONY: helm-db-load
helm-db-load: ## Load the database from a file at ./$DB_SEED_FILEPATH
helm-db-load: ## Load the database from a file at $DB_SEED_FILEPATH
@kubectl --namespace $(namespace) exec -i $(shell make --no-print-directory helm-db-pod) \
-- bash -c 'exec mysql -h127.0.0.1 -u"${DB_USER}" -p"${DB_PASS}"' < ${DB_SEED_FILEPATH}

Expand Down
4 changes: 2 additions & 2 deletions charts/agimus/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ apiVersion: v2
name: agimus
description: A helm chart for a discord bot that also runs a mysql db
type: application
version: v1.3.5
appVersion: v1.3.5
version: v1.3.6
appVersion: v1.3.6

0 comments on commit 14edb68

Please sign in to comment.