Add backup and restore

master
rohhie 2023-05-05 12:44:29 +09:00
parent 6f12e54f9c
commit 1a05030c20
8 changed files with 220 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
cert/ cert/
*.tar.gz *.tar.gz
*.tar
exampleCA/ exampleCA/
docker/config/gitea/conf/app.ini docker/config/gitea/conf/app.ini
.setupphase.txt .setupphase.txt

30
11-backup.sh Executable file
View File

@ -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

38
12-restore.sh Executable file
View File

@ -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

6
backup/Dockerfile Normal file
View File

@ -0,0 +1,6 @@
FROM alpine:latest
ADD entrypoint.sh /
ADD backup.sh /
ADD restore.sh /
ENTRYPOINT ["/entrypoint.sh"]
RUN apk add tzdata

44
backup/backup.sh Executable file
View File

@ -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."

35
backup/docker-compose.yml Normal file
View File

@ -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

27
backup/entrypoint.sh Executable file
View File

@ -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

39
backup/restore.sh Executable file
View File

@ -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."