From 3d6a2236625c5b8f9f091425168df26e349c940a Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 5 Dec 2022 15:04:50 +0000 Subject: [PATCH] feat(local): add "local" project with many services --- local/.env.example | 1 + local/.gitignore | 1 + local/Makefile | 7 +++ local/docker-compose.yml | 70 +++++++++++++++++++++++++ local/utils-entrypoint.sh | 105 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 184 insertions(+) create mode 100644 local/.env.example create mode 100644 local/.gitignore create mode 100644 local/Makefile create mode 100644 local/docker-compose.yml create mode 100755 local/utils-entrypoint.sh diff --git a/local/.env.example b/local/.env.example new file mode 100644 index 0000000..a8d4b54 --- /dev/null +++ b/local/.env.example @@ -0,0 +1 @@ +BACKUPS_DIR=./backups diff --git a/local/.gitignore b/local/.gitignore new file mode 100644 index 0000000..5f2fcc7 --- /dev/null +++ b/local/.gitignore @@ -0,0 +1 @@ +backups/* diff --git a/local/Makefile b/local/Makefile new file mode 100644 index 0000000..a044e69 --- /dev/null +++ b/local/Makefile @@ -0,0 +1,7 @@ +.PHONY: backup-data +backup-data: + @docker compose run --rm utils backup + +.PHONY: restore-data +restore-data: + @docker compose run --rm utils restore diff --git a/local/docker-compose.yml b/local/docker-compose.yml new file mode 100644 index 0000000..3f826d0 --- /dev/null +++ b/local/docker-compose.yml @@ -0,0 +1,70 @@ +--- +version: "3.8" +volumes: + jaeger-data: + driver: local + mariadb-data: + driver: local + redis-data: + driver: local + +services: + utils: + profiles: [utils] + image: alpine:latest + restart: never + entrypoint: /entrypoint.sh + volumes: + - "./utils-entrypoint.sh:/entrypoint.sh" + - "jaeger-data:/data/jaeger" + - "mariadb-data:/data/mariadb" + - "redis-data:/data/redis" + - "${BACKUPS_DIR:-./backups}:/backups" + + mariadb: + image: mariadb:10.10 + container_name: mariadb + restart: unless-stopped + network_mode: bridge + environment: + MARIADB_ALLOW_EMPTY_ROOT_PASSWORD: "yes" + volumes: + - mariadb-data:/var/lib/mysql + ports: + - "127.0.0.1:3306:3306" + + redis: + image: redis:7 + container_name: redis + restart: unless-stopped + network_mode: bridge + command: + - "redis-server" + - "--save" + - "60" + - "1" + - "--loglevel warning" + volumes: + - redis-data:/data + ports: + - "127.0.0.1:6379:6379" + + jaeger: + image: jaegertracing/all-in-one:1 + environment: + COLLECTOR_ZIPKIN_HOST_PORT: ":9411" + SPAN_STORAGE_TYPE: "badger" + BADGER_EPHEMERAL: "false" + BADGER_DIRECTORY_VALUE: "/badger/data" + BADGER_DIRECTORY_KEY: "/badger/key" + volumes: + - jaeger-data:/badger + ports: + - "127.0.0.1:5775:5775/udp" + - "127.0.0.1:6831:6831/udp" + - "127.0.0.1:6832:6832/udp" + - "127.0.0.1:5778:5778" + - "127.0.0.1:16686:16686" + - "127.0.0.1:14268:14268" + - "127.0.0.1:14250:14250" + - "127.0.0.1:9411:9411" diff --git a/local/utils-entrypoint.sh b/local/utils-entrypoint.sh new file mode 100755 index 0000000..a7396d4 --- /dev/null +++ b/local/utils-entrypoint.sh @@ -0,0 +1,105 @@ +#!/bin/sh +set -e + +DATA_DIR=/data +BACKUPS_DIR=/backups + +backup() { + if [ -z "$1" ] || [ "$1" = "all" ]; then + for src in ${DATA_DIR}/*; do + backup_single "$(basename "$src")" "$2" + done + else + backup_single "$1" "$2" + fi +} + +backup_single() { + SOURCE="$1" + DEST="$SOURCE" + if [ -n "$2" ]; then + DEST="${DEST}-${2}" + fi + DEST="${DEST}.tar.gz" + + if [ -z "$SOURCE" ]; then + echo "Usage: backup [] []" >&2 + exit 1 + fi + + if [ ! -d "${DATA_DIR}/$SOURCE" ]; then + echo "ERROR: Source directory for $SOURCE does not exist." >&2 + exit 1 + fi + + if [ -f "${BACKUPS_DIR}/${DEST}" ]; then + echo "WARN: Overwriting existing destination file ${DEST}..." >&2 + fi + + cd "${DATA_DIR}/${SOURCE}" + echo "Backing up ${SOURCE} to ${DEST}..." + tar -czf "${BACKUPS_DIR}/${DEST}" . + echo "Backup of ${SOURCE} to ${DEST} complete." +} + +restore() { + if [ -z "$1" ] || [ "$1" = "all" ]; then + for src in ${DATA_DIR}/*; do + restore_single "$(basename "$src")" "$2" + done + else + restore_single "$1" "$2" + fi +} + +restore_single() { + SOURCE="$1" + + if [ -z "$SOURCE" ]; then + echo "Usage: restore [] []" >&2 + exit 1 + fi + + if [ ! -d "${DATA_DIR}/$SOURCE" ]; then + echo "ERROR: Restore target directory for $SOURCE does not exist." >&2 + exit 1 + fi + + DEST="" + if [ -n "$2" ]; then + if [ -f "${BACKUPS_DIR}/${2}" ]; then + DEST="${2}" + elif [ -f "${BACKUPS_DIR}/${2}.tar.gz" ]; then + DEST="${2}.tar.gz" + elif [ -f "${BACKUPS_DIR}/${SOURCE}-${2}" ]; then + DEST="${SOURCE}-${2}" + else + DEST="${SOURCE}-${2}.tar.gz" + fi + else + DEST="${SOURCE}.tar.gz" + fi + + if [ ! -f "${BACKUPS_DIR}/${DEST}" ]; then + echo "ERROR: Restore source file ${DEST} does not exist." >&2 + exit 1 + fi + + cd "${DATA_DIR}/${SOURCE}" + echo "Restoring ${DEST} to ${SOURCE}..." + tar -xzf "${BACKUPS_DIR}/${DEST}" . + echo "Restored ${DEST} to ${SOURCE}." +} + +case "$1" in + backup) + backup "$2" "$3" + ;; + restore) + restore "$2" "$3" + ;; + *) + echo "Usage: {backup|restore} [] []" >&2 + exit 1 + ;; +esac