From 25e4bdda143970076429419d09f19c90aaf6b200 Mon Sep 17 00:00:00 2001 From: Clemens Geibel Date: Fri, 9 Jul 2021 12:04:35 +0200 Subject: [PATCH] Added backup docker image --- infrastrucure/docker-backup/build.py | 51 +++++++++++++++++++ infrastrucure/docker-backup/image/Dockerfile | 5 ++ .../docker-backup/image/resources/backup.sh | 21 ++++++++ .../resources/entrypoint-start-and-wait.sh | 12 +++++ .../image/resources/entrypoint.sh | 8 +++ .../docker-backup/image/resources/init.sh | 14 +++++ .../docker-backup/image/resources/install.sh | 11 ++++ .../image/resources/restic-snapshots.sh | 15 ++++++ .../docker-backup/image/resources/restore.sh | 31 +++++++++++ infrastrucure/docker-backup/test/Dockerfile | 8 +++ .../docker-backup/test/serverspec.edn | 7 +++ .../{docker => docker-jira}/build.py | 0 .../{docker => docker-jira}/image/Dockerfile | 0 .../image/resources/dbconfig.xml | 0 .../image/resources/entrypoint.sh | 0 .../image/resources/install.sh | 0 .../image/resources/logging.properties | 0 .../image/resources/server.xml | 0 .../image/resources/setenv.sh | 0 .../image/resources/user.sh | 0 .../{docker => docker-jira}/test/Dockerfile | 0 .../test/serverspec.edn | 0 22 files changed, 183 insertions(+) create mode 100644 infrastrucure/docker-backup/build.py create mode 100644 infrastrucure/docker-backup/image/Dockerfile create mode 100755 infrastrucure/docker-backup/image/resources/backup.sh create mode 100644 infrastrucure/docker-backup/image/resources/entrypoint-start-and-wait.sh create mode 100755 infrastrucure/docker-backup/image/resources/entrypoint.sh create mode 100755 infrastrucure/docker-backup/image/resources/init.sh create mode 100755 infrastrucure/docker-backup/image/resources/install.sh create mode 100755 infrastrucure/docker-backup/image/resources/restic-snapshots.sh create mode 100755 infrastrucure/docker-backup/image/resources/restore.sh create mode 100644 infrastrucure/docker-backup/test/Dockerfile create mode 100644 infrastrucure/docker-backup/test/serverspec.edn rename infrastrucure/{docker => docker-jira}/build.py (100%) rename infrastrucure/{docker => docker-jira}/image/Dockerfile (100%) rename infrastrucure/{docker => docker-jira}/image/resources/dbconfig.xml (100%) rename infrastrucure/{docker => docker-jira}/image/resources/entrypoint.sh (100%) rename infrastrucure/{docker => docker-jira}/image/resources/install.sh (100%) rename infrastrucure/{docker => docker-jira}/image/resources/logging.properties (100%) rename infrastrucure/{docker => docker-jira}/image/resources/server.xml (100%) rename infrastrucure/{docker => docker-jira}/image/resources/setenv.sh (100%) rename infrastrucure/{docker => docker-jira}/image/resources/user.sh (100%) rename infrastrucure/{docker => docker-jira}/test/Dockerfile (100%) rename infrastrucure/{docker => docker-jira}/test/serverspec.edn (100%) diff --git a/infrastrucure/docker-backup/build.py b/infrastrucure/docker-backup/build.py new file mode 100644 index 0000000..0452f29 --- /dev/null +++ b/infrastrucure/docker-backup/build.py @@ -0,0 +1,51 @@ +from os import environ +from pybuilder.core import task, init +from ddadevops import * +import logging + +name = 'c4k-jira-backup' +MODULE = 'docker' +PROJECT_ROOT_PATH = '../..' + +class MyBuild(DevopsDockerBuild): + pass + +@init +def initialize(project): + project.build_depends_on('ddadevops>=0.12.4') + stage = 'prod' + dockerhub_user = environ.get('DOCKERHUB_USER') + if not dockerhub_user: + dockerhub_user = gopass_field_from_path('meissa/web/docker.com', 'login') + dockerhub_password = environ.get('DOCKERHUB_PASSWORD') + if not dockerhub_password: + dockerhub_password = gopass_password_from_path('meissa/web/docker.com') + tag = environ.get('CI_COMMIT_TAG') + if not tag: + tag = get_tag_from_latest_commit() + config = create_devops_docker_build_config( + stage, PROJECT_ROOT_PATH, MODULE, dockerhub_user, dockerhub_password, docker_publish_tag=tag) + build = MyBuild(project, config) + build.initialize_build_dir() + + +@task +def image(project): + build = get_devops_build(project) + build.image() + +@task +def drun(project): + build = get_devops_build(project) + build.drun() + +@task +def publish(project): + build = get_devops_build(project) + build.dockerhub_login() + build.dockerhub_publish() + +@task +def test(project): + build = get_devops_build(project) + build.test() diff --git a/infrastrucure/docker-backup/image/Dockerfile b/infrastrucure/docker-backup/image/Dockerfile new file mode 100644 index 0000000..20e4c73 --- /dev/null +++ b/infrastrucure/docker-backup/image/Dockerfile @@ -0,0 +1,5 @@ +FROM domaindrivenarchitecture/dda-backup + +# Prepare Entrypoint Script +ADD resources /tmp +RUN /tmp/install.sh diff --git a/infrastrucure/docker-backup/image/resources/backup.sh b/infrastrucure/docker-backup/image/resources/backup.sh new file mode 100755 index 0000000..954090d --- /dev/null +++ b/infrastrucure/docker-backup/image/resources/backup.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +set -o pipefail + +function main() { + file_env AWS_ACCESS_KEY_ID + file_env AWS_SECRET_ACCESS_KEY + file_env POSTGRES_DB + file_env POSTGRES_PASSWORD + file_env POSTGRES_USER + file_env RESTIC_DAYS_TO_KEEP 14 + + #backup-roles 'oc_' TODO: lookup for jira roles (Also in init.sh & restore.sh) + backup-db-dump + backup-fs-from-directory '/var/backups/' 'data/' +} + +source /usr/local/lib/functions.sh +source /usr/local/lib/file-functions.sh + +main diff --git a/infrastrucure/docker-backup/image/resources/entrypoint-start-and-wait.sh b/infrastrucure/docker-backup/image/resources/entrypoint-start-and-wait.sh new file mode 100644 index 0000000..99e4a3c --- /dev/null +++ b/infrastrucure/docker-backup/image/resources/entrypoint-start-and-wait.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +function main() { + + while true; do + sleep 1m + done +} + +source /usr/local/lib/functions.sh +source /usr/local/lib/pg-functions.sh +main \ No newline at end of file diff --git a/infrastrucure/docker-backup/image/resources/entrypoint.sh b/infrastrucure/docker-backup/image/resources/entrypoint.sh new file mode 100755 index 0000000..c03cb42 --- /dev/null +++ b/infrastrucure/docker-backup/image/resources/entrypoint.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +function main() { + /usr/local/bin/backup.sh +} + +source /usr/local/lib/functions.sh +main diff --git a/infrastrucure/docker-backup/image/resources/init.sh b/infrastrucure/docker-backup/image/resources/init.sh new file mode 100755 index 0000000..69ae9a0 --- /dev/null +++ b/infrastrucure/docker-backup/image/resources/init.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +function main() { + file_env AWS_ACCESS_KEY_ID + file_env AWS_SECRET_ACCESS_KEY + + #init-role-repo + init-database-repo + init-file-repo +} + +source /usr/local/lib/functions.sh +source /usr/local/lib/file-functions.sh +main diff --git a/infrastrucure/docker-backup/image/resources/install.sh b/infrastrucure/docker-backup/image/resources/install.sh new file mode 100755 index 0000000..1a8cbd7 --- /dev/null +++ b/infrastrucure/docker-backup/image/resources/install.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +apt-get update > /dev/null; + +install -m 0700 /tmp/entrypoint.sh / +install -m 0700 /tmp/entrypoint-start-and-wait.sh / + +install -m 0700 /tmp/init.sh /usr/local/bin/ +install -m 0700 /tmp/backup.sh /usr/local/bin/ +install -m 0700 /tmp/restore.sh /usr/local/bin/ +install -m 0700 /tmp/restic-snapshots.sh /usr/local/bin/ diff --git a/infrastrucure/docker-backup/image/resources/restic-snapshots.sh b/infrastrucure/docker-backup/image/resources/restic-snapshots.sh new file mode 100755 index 0000000..ca889ce --- /dev/null +++ b/infrastrucure/docker-backup/image/resources/restic-snapshots.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +set -o pipefail + +function main() { + file_env AWS_ACCESS_KEY_ID + file_env AWS_SECRET_ACCESS_KEY + + restic -r ${RESTIC_REPOSITORY}/files snapshots +} + +source /usr/local/lib/functions.sh +source /usr/local/lib/file-functions.sh + +main diff --git a/infrastrucure/docker-backup/image/resources/restore.sh b/infrastrucure/docker-backup/image/resources/restore.sh new file mode 100755 index 0000000..26b0b17 --- /dev/null +++ b/infrastrucure/docker-backup/image/resources/restore.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +set -Eeo pipefail + +function main() { + + file_env AWS_ACCESS_KEY_ID + file_env AWS_SECRET_ACCESS_KEY + + file_env POSTGRES_DB + file_env POSTGRES_PASSWORD + file_env POSTGRES_USER + + # Restore latest snapshot into /var/backups/restic-restore + rm -rf /var/backups/restic-restore + restore-directory '/var/backups/restic-restore' + + # Restore data dir backup + rm -rf /var/backups/data/* + cp -a /var/backups/restic-restore/data/* /var/backups/data + + # Restore db + drop-create-db + #restore-roles + restore-db +} + +source /usr/local/lib/functions.sh +source /usr/local/lib/file-functions.sh +main + diff --git a/infrastrucure/docker-backup/test/Dockerfile b/infrastrucure/docker-backup/test/Dockerfile new file mode 100644 index 0000000..79bf2c5 --- /dev/null +++ b/infrastrucure/docker-backup/test/Dockerfile @@ -0,0 +1,8 @@ +FROM c4k-jira-backup + +RUN curl -L -o /tmp/serverspec.jar \ + https://github.com/DomainDrivenArchitecture/dda-serverspec-crate/releases/download/2.0.0/dda-serverspec-standalone.jar + +COPY serverspec.edn /tmp/serverspec.edn + +RUN java -jar /tmp/serverspec.jar /tmp/serverspec.edn -v \ No newline at end of file diff --git a/infrastrucure/docker-backup/test/serverspec.edn b/infrastrucure/docker-backup/test/serverspec.edn new file mode 100644 index 0000000..37bbbfb --- /dev/null +++ b/infrastrucure/docker-backup/test/serverspec.edn @@ -0,0 +1,7 @@ +{:file [{:path "/usr/local/bin/init.sh" :mod "700"} + {:path "/usr/local/bin/backup.sh" :mod "700"} + {:path "/usr/local/bin/restore.sh" :mod "700"} + {:path "/usr/local/bin/export-db.sh" :mod "700"} + {:path "/usr/local/bin/restic-snapshots.sh" :mod "700"} + {:path "/entrypoint.sh" :mod "700"} + {:path "/entrypoint-start-and-wait.sh" :mod "700"}]} diff --git a/infrastrucure/docker/build.py b/infrastrucure/docker-jira/build.py similarity index 100% rename from infrastrucure/docker/build.py rename to infrastrucure/docker-jira/build.py diff --git a/infrastrucure/docker/image/Dockerfile b/infrastrucure/docker-jira/image/Dockerfile similarity index 100% rename from infrastrucure/docker/image/Dockerfile rename to infrastrucure/docker-jira/image/Dockerfile diff --git a/infrastrucure/docker/image/resources/dbconfig.xml b/infrastrucure/docker-jira/image/resources/dbconfig.xml similarity index 100% rename from infrastrucure/docker/image/resources/dbconfig.xml rename to infrastrucure/docker-jira/image/resources/dbconfig.xml diff --git a/infrastrucure/docker/image/resources/entrypoint.sh b/infrastrucure/docker-jira/image/resources/entrypoint.sh similarity index 100% rename from infrastrucure/docker/image/resources/entrypoint.sh rename to infrastrucure/docker-jira/image/resources/entrypoint.sh diff --git a/infrastrucure/docker/image/resources/install.sh b/infrastrucure/docker-jira/image/resources/install.sh similarity index 100% rename from infrastrucure/docker/image/resources/install.sh rename to infrastrucure/docker-jira/image/resources/install.sh diff --git a/infrastrucure/docker/image/resources/logging.properties b/infrastrucure/docker-jira/image/resources/logging.properties similarity index 100% rename from infrastrucure/docker/image/resources/logging.properties rename to infrastrucure/docker-jira/image/resources/logging.properties diff --git a/infrastrucure/docker/image/resources/server.xml b/infrastrucure/docker-jira/image/resources/server.xml similarity index 100% rename from infrastrucure/docker/image/resources/server.xml rename to infrastrucure/docker-jira/image/resources/server.xml diff --git a/infrastrucure/docker/image/resources/setenv.sh b/infrastrucure/docker-jira/image/resources/setenv.sh similarity index 100% rename from infrastrucure/docker/image/resources/setenv.sh rename to infrastrucure/docker-jira/image/resources/setenv.sh diff --git a/infrastrucure/docker/image/resources/user.sh b/infrastrucure/docker-jira/image/resources/user.sh similarity index 100% rename from infrastrucure/docker/image/resources/user.sh rename to infrastrucure/docker-jira/image/resources/user.sh diff --git a/infrastrucure/docker/test/Dockerfile b/infrastrucure/docker-jira/test/Dockerfile similarity index 100% rename from infrastrucure/docker/test/Dockerfile rename to infrastrucure/docker-jira/test/Dockerfile diff --git a/infrastrucure/docker/test/serverspec.edn b/infrastrucure/docker-jira/test/serverspec.edn similarity index 100% rename from infrastrucure/docker/test/serverspec.edn rename to infrastrucure/docker-jira/test/serverspec.edn