From 1a05030c205fdec5936865118878005152d8ebe7 Mon Sep 17 00:00:00 2001 From: rohhie Date: Fri, 5 May 2023 12:44:29 +0900 Subject: [PATCH] Add backup and restore --- .gitignore | 1 + 11-backup.sh | 30 ++++++++++++++++++++++++++ 12-restore.sh | 38 +++++++++++++++++++++++++++++++++ backup/Dockerfile | 6 ++++++ backup/backup.sh | 44 +++++++++++++++++++++++++++++++++++++++ backup/docker-compose.yml | 35 +++++++++++++++++++++++++++++++ backup/entrypoint.sh | 27 ++++++++++++++++++++++++ backup/restore.sh | 39 ++++++++++++++++++++++++++++++++++ 8 files changed, 220 insertions(+) create mode 100755 11-backup.sh create mode 100755 12-restore.sh create mode 100644 backup/Dockerfile create mode 100755 backup/backup.sh create mode 100644 backup/docker-compose.yml create mode 100755 backup/entrypoint.sh create mode 100755 backup/restore.sh diff --git a/.gitignore b/.gitignore index 27f6302..5967ca0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ cert/ *.tar.gz +*.tar exampleCA/ docker/config/gitea/conf/app.ini .setupphase.txt diff --git a/11-backup.sh b/11-backup.sh new file mode 100755 index 0000000..c48fffd --- /dev/null +++ b/11-backup.sh @@ -0,0 +1,30 @@ +#!/bin/bash +SCRIPTHOME=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + +secho() { + for ((i=0;i<$1;i++)); do echo ""; done + echo $2 +} + +sudo echo -n "" +if [ $(whoami) = "root" ]; then + echo "sudo せずに実行してください" + exit 0 +fi + +cd $SCRIPTHOME/backup + +echo "=== kopano, gitea, rordbコンテナの停止" +sudo docker container stop kopano gitea rordb + +secho 3 "=== バックアップコンテナの起動" +sudo docker compose up --build + +secho 3 "=== バックアップコンテナの削除" +sudo docker compose down + +secho 3 "=== kopano, gitea, rordbコンテナの起動" +sudo docker container start kopano gitea rordb + +secho 3 "=== バックアップ完了(&SCRIPTHOME/backup/backups)" +exit diff --git a/12-restore.sh b/12-restore.sh new file mode 100755 index 0000000..01c8eba --- /dev/null +++ b/12-restore.sh @@ -0,0 +1,38 @@ +#!/bin/bash +SCRIPTHOME=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) + +secho() { + for ((i=0;i<$1;i++)); do echo ""; done + echo $2 +} + +sudo echo -n "" +if [ $(whoami) = "root" ]; then + echo "sudo せずに実行してください" + exit 0 +fi + +cd $SCRIPTHOME/backup + +echo "=== docker-compose.ymlの書き換え" +sed -i "s/DO_BACKUP: .*/DO_BACKUP: \"restore\"/g" docker-compose.yml + +secho 3 "=== kopano, gitea, rordbコンテナの停止" +sudo docker container stop kopano gitea rordb + +secho 3 "=== バックアップコンテナの起動" +sudo docker compose up -d --build +sudo docker attach backup + +secho 3 "=== バックアップコンテナの削除" +sudo docker compose down + +secho 3 "=== kopano, gitea, rordbコンテナの起動" +sudo docker container start kopano gitea rordb + +echo "=== docker-compose.ymlの書き戻し" +sed -i "s/DO_BACKUP: .*/DO_BACKUP: \"true\"/g" docker-compose.yml + +secho 3 "=== リストア完了(&SCRIPTHOME/backup/backups)" +echo "git.example.netにSSHで接続するには公開鍵を登録し直してください" +exit diff --git a/backup/Dockerfile b/backup/Dockerfile new file mode 100644 index 0000000..e7718ca --- /dev/null +++ b/backup/Dockerfile @@ -0,0 +1,6 @@ +FROM alpine:latest +ADD entrypoint.sh / +ADD backup.sh / +ADD restore.sh / +ENTRYPOINT ["/entrypoint.sh"] +RUN apk add tzdata diff --git a/backup/backup.sh b/backup/backup.sh new file mode 100755 index 0000000..e1f6778 --- /dev/null +++ b/backup/backup.sh @@ -0,0 +1,44 @@ +#!/bin/ash + +# Create backup files. +TMP_TARGET=/mnt/backups/backup-$(hostname)-$(date +'%Y-%m-%d-%H-%M-%S').tar +echo $TMP_TARGET + +# +# Kopano +# +echo "Kopano" +cd /tmp +tar -cvf $TMP_TARGET.1 \ + kopano/var/lib/kopano/attachments + +# +# Gitea +# +echo "Gitea" +cd /tmp +tar -cvf $TMP_TARGET.2 \ + gitea/data + +# +# MySQL +# +echo "MySQL" +cd /tmp +tar -cvf $TMP_TARGET.3 \ + mysql/var/lib/mysql + +# +# merge tarball. +# +echo "Merge" +head -c -$((512*2)) $TMP_TARGET.1 > $TMP_TARGET +head -c -$((512*2)) $TMP_TARGET.2 >> $TMP_TARGET +head -c -$((512*2)) $TMP_TARGET.3 >> $TMP_TARGET +rm $TMP_TARGET.1 $TMP_TARGET.2 $TMP_TARGET.3 + +echo "Archive" +gzip $TMP_TARGET + +# Finish. +echo "Backed up." diff --git a/backup/docker-compose.yml b/backup/docker-compose.yml new file mode 100644 index 0000000..7aabdc2 --- /dev/null +++ b/backup/docker-compose.yml @@ -0,0 +1,35 @@ +version: "3.9" + +volumes: + kopano: + name: docker_kopano + external: true + gitea: + name: docker_gitea + external: true + db: + name: docker_mysql + external: true + +services: + backup: + build: ./ + image: custom/backup:1.0.0 + container_name: backup + restart: "no" + environment: + TZ: Asia/Tokyo + DO_BACKUP: "true" + hostname: exbackup + stdin_open: true + tty: true + volumes: + # Kopano + - kopano:/tmp/kopano/var/lib/kopano/attachments + # Gitea + - gitea:/tmp/gitea/data + - ../docker/config/gitea/conf:/tmp/gitea/data/gitea/conf + # MySQL + - db:/tmp/mysql/var/lib/mysql + # Backup directory + - ../backup/backups:/mnt/backups diff --git a/backup/entrypoint.sh b/backup/entrypoint.sh new file mode 100755 index 0000000..02db0e8 --- /dev/null +++ b/backup/entrypoint.sh @@ -0,0 +1,27 @@ +#!/bin/ash + +echo "Start container with parameter : $@" + +trap sig_term SIGTERM + +sig_term() { + echo "CATCH SIGTERM" + wait + exit 0 +} + +# Execute paramater. +exec "$@" + +if [[ $DO_BACKUP == "true" ]]; then + exec /backup.sh + exit 0 +fi + +if [[ $DO_BACKUP == "restore" ]]; then + exec /restore.sh + exit 0 +fi + +# Infinity roop. +while : ; do sleep 1 ; done diff --git a/backup/restore.sh b/backup/restore.sh new file mode 100755 index 0000000..31d6062 --- /dev/null +++ b/backup/restore.sh @@ -0,0 +1,39 @@ +#!/bin/ash + +sleep 5 + +# Ask for restore file name. +if [[ $(echo $(find /mnt/backups -type f -maxdepth 1 | wc -l)) -eq 0 ]]; then + echo "There is no backup." + exit +fi + +echo "The following backups are available." +ls /mnt/backups +echo +echo "Enter the backup file name to be restored." +echo -n "-> " +read TMP_SELECT + +TMP_TARGET=/mnt/backups/$TMP_SELECT + +if [[ -f $TMP_TARGET ]]; then + echo $TMP_TARGET +else + echo "File not found." + exit +fi + +# +# Restore +# +gzip -dk $TMP_TARGET +TMP_TAR=${TMP_TARGET%.*} + +cd /tmp +tar -xvf $TMP_TAR + +rm $TMP_TAR + +# Finish. +echo "Restored."