diff options
author | crupest <crupest@outlook.com> | 2022-11-23 14:17:16 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-11-23 14:17:16 +0800 |
commit | 6272b219ba4fb29dd542d10cb846a0dbb6edf4ea (patch) | |
tree | 5096701ba667057049597ca8f913ee91aaa1e009 | |
parent | 647bebc5cc01ab6e1714885563d8b09e2ed51a24 (diff) | |
download | crupest-6272b219ba4fb29dd542d10cb846a0dbb6edf4ea.tar.gz crupest-6272b219ba4fb29dd542d10cb846a0dbb6edf4ea.tar.bz2 crupest-6272b219ba4fb29dd542d10cb846a0dbb6edf4ea.zip |
Add backup.
-rw-r--r-- | docker/auto-backup/Dockerfile | 10 | ||||
-rw-r--r-- | docker/auto-backup/daemon.bash | 58 | ||||
-rw-r--r-- | template/docker-compose.yaml.template | 17 | ||||
-rw-r--r-- | template/rclone.conf.template | 7 | ||||
-rwxr-xr-x | tool/aio.py | 15 |
5 files changed, 104 insertions, 3 deletions
diff --git a/docker/auto-backup/Dockerfile b/docker/auto-backup/Dockerfile new file mode 100644 index 0000000..a180740 --- /dev/null +++ b/docker/auto-backup/Dockerfile @@ -0,0 +1,10 @@ +FROM rsync/rsync:latest +ARG CRUPEST_AUTO_BACKUP_BUCKET_NAME +ARG CRUPEST_AUTO_BACKUP_INIT_DELAY=0 +ARG CRUPEST_AUTO_BACKUP_INTERVAL=1d +ENV CRUPEST_AUTO_BACKUP_INIT_DELAY=${CRUPEST_AUTO_BACKUP_INIT_DELAY} +ENV CRUPEST_AUTO_BACKUP_INTERVAL=${CRUPEST_AUTO_BACKUP_INTERVAL} +ENV CRUPEST_AUTO_BACKUP_BUCKET_NAME=${CRUPEST_AUTO_BACKUP_BUCKET_NAME} +COPY daemon.bash /daemon.bash +VOLUME [ "/data", "/config/rclone/rclone.conf" ] +ENTRYPOINT [ "/daemon.bash" ] diff --git a/docker/auto-backup/daemon.bash b/docker/auto-backup/daemon.bash new file mode 100644 index 0000000..db49e9b --- /dev/null +++ b/docker/auto-backup/daemon.bash @@ -0,0 +1,58 @@ +#!/usr/bin/env bash + +set -e + +# Check I'm root. +if [[ $EUID -ne 0 ]]; then + echo "This script must be run as root" 1>&2 + exit 1 +fi + +# Check if CRUPEST_AUTO_BACKUP_BUCKET_NAME is defined. +if [[ -z "$CRUPEST_AUTO_BACKUP_BUCKET_NAME" ]]; then + echo "CRUPEST_AUTO_BACKUP_BUCKET_NAME is not defined or empty" + exit 1 +fi + +rclone --version + +# Check xz and tar +xz --version +tar --version + +function backup { + # Output "Begin backup..." in yellow and restore default + echo -e "\e[0;103m\e[K\e[1mBegin backup..." "\e[0m" + + # Get current time and convert it to YYYY-MM-DDTHH.MM.SS + current_time=$(date +%Y-%m-%dT%H.%M.%S) + echo "Current time: $current_time" + echo "Create tar.xz for data..." + + # tar and xz /data to tmp + tar -cJf /tmp/data.tar.xz -C / data + + echo "Use rclone to upload data..." + # upload to remote + rclone copyto /tmp/data.tar.xz "mycos:$CRUPEST_AUTO_BACKUP_BUCKET_NAME/$current_time.tar.xz" + + echo "Remove tmp file..." + # remove tmp + rm /tmp/data.tar.xz + + # echo "Backup finished!" in green and restore default + echo -e "\e[0;102m\e[K\e[1mFinish backup!" "\e[0m" +} + +echo "Initial delay: $CRUPEST_AUTO_BACKUP_INIT_DELAY" +sleep "$CRUPEST_AUTO_BACKUP_INIT_DELAY" + +# forever loop +while true; do + backup + + # sleep for CRUPEST_AUTO_BACKUP_INTERVAL + echo "Sleep for $CRUPEST_AUTO_BACKUP_INTERVAL for next backup..." + sleep "$CRUPEST_AUTO_BACKUP_INTERVAL" +done + diff --git a/template/docker-compose.yaml.template b/template/docker-compose.yaml.template index 83e383b..5982e62 100644 --- a/template/docker-compose.yaml.template +++ b/template/docker-compose.yaml.template @@ -100,6 +100,23 @@ services: networks: - internal + auto-backup: + pull_policy: build + build: + context: ./docker/auto-backup + dockerfile: Dockerfile + pull: true + args: CRUPEST_AUTO_BACKUP_BUCKET_NAME=${CRUPEST_AUTO_BACKUP_BUCKET_NAME} + tags: + - "crupest/auto-backup:latest" + container_name: auto-backup + volumes: + - "./data:/data:ro" + - "./rclone.conf:/config/rclone/rclone.conf:ro" + restart: on-failure:3 + networks: + - internal + mailserver: image: docker.io/mailserver/docker-mailserver:latest pull_policy: always diff --git a/template/rclone.conf.template b/template/rclone.conf.template new file mode 100644 index 0000000..38ef2dd --- /dev/null +++ b/template/rclone.conf.template @@ -0,0 +1,7 @@ +[mycos] +type = s3 +provider = TencentCOS +access_key_id = ${CRUPEST_AUTO_BACKUP_COS_ACCESS_KEY_ID} +secret_access_key = ${CRUPEST_AUTO_BACKUP_COS_SECRET_ACCESS_KEY} +endpoint = cos.${CRUPEST_AUTO_BACKUP_COS_REGION}.myqcloud.com +acl = default diff --git a/tool/aio.py b/tool/aio.py index cdb616c..703b4f2 100755 --- a/tool/aio.py +++ b/tool/aio.py @@ -240,7 +240,7 @@ for filename in template_name_list: class ConfigVar: - def __init__(self, name: str, description: str, default_value_generator): + def __init__(self, name: str, description: str, default_value_generator, /, default_value_for_ask=None): """Create a config var. Args: @@ -251,10 +251,11 @@ class ConfigVar: self.name = name self.description = description self.default_value_generator = default_value_generator + self.default_value_for_ask = default_value_for_ask def get_default_value(self): if isinstance(self.default_value_generator, str): - return Prompt.ask(self.default_value_generator, console=console) + return Prompt.ask(self.default_value_generator, console=console, default=self.default_value_for_ask) else: return self.default_value_generator() @@ -275,7 +276,15 @@ config_var_list: list = [ ConfigVar("CRUPEST_HALO_DB_PASSWORD", "password for halo h2 database, once used never change it", lambda: os.urandom(8).hex()), ConfigVar("CRUPEST_IN_CHINA", - "set to true if you are in China, some network optimization will be applied", lambda: "false") + "set to true if you are in China, some network optimization will be applied", lambda: "false"), + ConfigVar("CRUPEST_AUTO_BACKUP_COS_ACCESS_KEY_ID", + "access key id for Tencent COS, used for auto backup", "Please input your Tencent COS access key id for backup:"), + ConfigVar("CRUPEST_AUTO_BACKUP_COS_SECRET_ACCESS_KEY", + "access key secret for Tencent COS, used for auto backup", "Please input your Tencent COS access key for backup:"), + ConfigVar("CRUPEST_AUTO_BACKUP_COS_REGION", + "region for Tencent COS, used for auto backup", "Please input your Tencent COS region for backup:", "ap-hongkong"), + ConfigVar("CRUPEST_AUTO_BACKUP_BUCKET_NAME", + "bucket name for Tencent COS, used for auto backup", "Please input your Tencent COS bucket name for backup:") ] config_var_name_set = set([config_var.name for config_var in config_var_list]) |