From 818e58f1795f6ec696f535dee9f1d424259f65a3 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Sun, 27 Mar 2022 19:28:00 +0200 Subject: [PATCH] big refactor too lazy to list things --- .gitignore | 14 --------- Dockerfile | 6 +--- README.md | 6 ++-- docker-compose.yml | 3 +- backup.sh => src/backup.sh | 2 ++ entrypoint.sh => src/entrypoint.sh | 17 +++++++---- src/init.sh | 43 ++++++++++++++++++++++++++++ interactive.sh => src/interactive.sh | 0 src/list.sh | 9 ++++++ restore.sh => src/restore.sh | 2 +- setup.sh => src/setup.sh | 4 +-- 11 files changed, 74 insertions(+), 32 deletions(-) rename backup.sh => src/backup.sh (93%) rename entrypoint.sh => src/entrypoint.sh (79%) create mode 100755 src/init.sh rename interactive.sh => src/interactive.sh (100%) create mode 100755 src/list.sh rename restore.sh => src/restore.sh (73%) mode change 100644 => 100755 rename setup.sh => src/setup.sh (90%) diff --git a/.gitignore b/.gitignore index c913ac4..e69de29 100644 --- a/.gitignore +++ b/.gitignore @@ -1,14 +0,0 @@ -* -!.gitignore -!Dockerfile -!docker-compose.yml -!backup.sh -!README.md -!.drone.yml -!init.sh -!setup.sh -!interactive.sh -!mysql.sh -!.dockerignore -!restore.sh -!entrypoint.sh diff --git a/Dockerfile b/Dockerfile index 18c642f..b13965b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,11 +4,7 @@ RUN apk add --no-cache mysql-client rclone bash fuse postgresql-client RUN mkdir /app && mkdir /config -COPY entrypoint.sh /app/entrypoint.sh -COPY setup.sh /app/setup.sh -COPY backup.sh /app/backup.sh -COPY interactive.sh /app/interactive.sh -COPY restore.sh /app/restore.sh +COPY src/ /app/ WORKDIR /config diff --git a/README.md b/README.md index 8b5c9e4..f71c53e 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Environment variables: `RESTIC_PASSWORD` or `RESTIC_PASSWORD_FILE` password for the repo -`RCLONE_CONFIG_FILE` content of the rclone config. +`RCLONE_CONFIG_CONTENT` content of the rclone config. For the database: @@ -34,7 +34,7 @@ Use the `/app/init.sh` script to init a new remote. # Directorys -`/config` Contains the rclone config. +`/config` Contains the rclone config. Can also be passed as env var. `/backup` Contains the mounted volumes as read only `/restore` Contains volumes to restore @@ -46,7 +46,7 @@ Here is an example of the script i use to backup my volumes: #!/usr/bin/env sh set -e -# get all volumes with the "backup.enable" lable on it +# get all volumes with the "backup.enable" label on it BACKUP_VOL=$(docker volume ls --filter "label=backup.enable" --format '-v {{ .Name }}:/backup/{{ .Name }}:ro ') docker run \ diff --git a/docker-compose.yml b/docker-compose.yml index 1591261..f929a99 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,4 +3,5 @@ version: "3" services: backup: build: . - image: djeeberjr/backup \ No newline at end of file + image: djeeberjr/backup + command: init \ No newline at end of file diff --git a/backup.sh b/src/backup.sh similarity index 93% rename from backup.sh rename to src/backup.sh index 5930c1b..36cb877 100755 --- a/backup.sh +++ b/src/backup.sh @@ -17,3 +17,5 @@ do done restic --verbose backup "/backup" + +restic --verbose prune --keep-last 3 diff --git a/entrypoint.sh b/src/entrypoint.sh similarity index 79% rename from entrypoint.sh rename to src/entrypoint.sh index 10e5e49..e57c30d 100755 --- a/entrypoint.sh +++ b/src/entrypoint.sh @@ -10,11 +10,17 @@ if [ -z "$COMMAND" ]; then echo "Available commands:" echo " backup" echo " restore" - echo " init TODO" + echo " init" echo " interactive" exit 1 fi +# if command is init +if [ "$COMMAND" = "init" ]; then + /app/init.sh + exit 0 +fi + . /app/setup.sh case "$COMMAND" in @@ -26,15 +32,14 @@ case "$COMMAND" in # restore /app/restore.sh ;; - init) - # init - # TODO: implement - /app/init.sh - ;; interactive) # interactive /app/interactive.sh ;; + list) + # list + /app/list.sh + ;; *) echo "Unknown command: $COMMAND" exit 1 diff --git a/src/init.sh b/src/init.sh new file mode 100755 index 0000000..0ffab31 --- /dev/null +++ b/src/init.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env sh + +set -e + +# if RCLONE_CONFIG_CONTENT is empty and no config file was found +if [ -z "$RCLONE_CONFIG_CONTENT" ] && [ ! -e "${RCLONE_CONFIG:=/config/rclone.conf}" ]; then + echo "No rclone config found" + rclone --config "${RCLONE_CONFIG:=/config/rclone.conf}" config +fi + +RCLONE_REMOTE="$(rclone listremotes | head -1)" + +echo "Using remote: $RCLONE_REMOTE" + +RESTIC_REPOSITORY="rclone:$RCLONE_REMOTE" + +echo "Using repository: $RESTIC_REPOSITORY" + +# if RESTIC_PASSWORD is empty +if [ -z "$RESTIC_PASSWORD" ] && [ -z "$RESTIC_PASSWORD_FILE" ]; then + echo "Set \$RESTIC_PASSWORD or \$RESTIC_PASSWORD_FILE" + exit 1 +fi + +export RCLONE_REMOTE +export RESTIC_REPOSITORY + +restic init + +echo "Display rclone config [y/N]?" +read -r answer + +# check if answer is yes +if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then + rclone --config "${RCLONE_CONFIG:=/config/rclone.conf}" config show + echo "" +fi + +echo "Environemnt variables:" +echo "RCLONE_REMOTE=$RCLONE_REMOTE" +echo "RESTIC_PASSWORD=*********" +echo "RESTIC_REPOSITORY=$RESTIC_REPOSITORY" +echo "" diff --git a/interactive.sh b/src/interactive.sh similarity index 100% rename from interactive.sh rename to src/interactive.sh diff --git a/src/list.sh b/src/list.sh new file mode 100755 index 0000000..6d79a0a --- /dev/null +++ b/src/list.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env sh + +set -e + +echo "Volumes:" +restic ls latest "/backup" + +echo "Snapshots:" +restic snapshots latest diff --git a/restore.sh b/src/restore.sh old mode 100644 new mode 100755 similarity index 73% rename from restore.sh rename to src/restore.sh index 1e2cd83..13f5cf6 --- a/restore.sh +++ b/src/restore.sh @@ -5,7 +5,7 @@ VOLUMES=$(find /backup -maxdepth 1 -type d -printf '%f\n' | tail -n +2) #loop volumes for VOLUME in $VOLUMES; do echo "Restoring $VOLUME" - restic -v restore latest --include "/backup/$VOLUME" + restic --verbose restore latest --include "/backup/$VOLUME" done diff --git a/setup.sh b/src/setup.sh similarity index 90% rename from setup.sh rename to src/setup.sh index 23ca20b..57751d7 100755 --- a/setup.sh +++ b/src/setup.sh @@ -2,8 +2,8 @@ set -e -if [ -n "$RCLONE_CONFIG_FILE" ]; then - echo "$RCLONE_CONFIG_FILE" > "${RCLONE_CONFIG:=/config/rclone.conf}" +if [ -n "$RCLONE_CONFIG_CONTENT" ]; then + echo "$RCLONE_CONFIG_CONTENT" > "${RCLONE_CONFIG:=/config/rclone.conf}" fi if [ -z "${RCLONE_CONFIG}" ]; then