From 996c9ba2f0f311cbbe1ec497aac8776f744750f3 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 26 Dec 2020 16:31:47 +0100 Subject: [PATCH] Refactored full code --- .travis.yml | 2 ++ README.md | 13 +++++++------ docker-volume-backup.sh | 25 ++++++++++--------------- 3 files changed, 19 insertions(+), 21 deletions(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..9c60b5d --- /dev/null +++ b/.travis.yml @@ -0,0 +1,2 @@ +language: shell +script: shellcheck $(find . -type f -name '*.sh') diff --git a/README.md b/README.md index 1f2d0d5..fe1be74 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,14 @@ # docker-volume-backup +[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](./LICENSE.txt) [![Travis CI](https://travis-ci.org/kevinveenbirkenbach/docker-volume-backup.svg?branch=master)](https://travis-ci.org/kevinveenbirkenbach/docker-volume-backup) +## goal This script backups all docker-volumes with the help of rsync. +## scheme +It is part of the following scheme: +![backup scheme](https://www.veen.world/wp-content/uploads/2020/12/server-backup-768x567.jpg) +Further information you will find [in this blog post](https://www.veen.world/2020/12/26/how-i-backup-dedicated-root-servers/). + ## Backup Execute: @@ -9,12 +16,6 @@ Execute: ./docker-volume-backup.sh ``` -Optional a user for the backup can be defined: - -```bash -./docker-volume-backup.sh administrator -``` - ## Test Delete the volume. diff --git a/docker-volume-backup.sh b/docker-volume-backup.sh index f0650a7..33b86c0 100644 --- a/docker-volume-backup.sh +++ b/docker-volume-backup.sh @@ -1,33 +1,28 @@ #!/bin/bash -# @todo rethink optional parameter -# @param $1 [optional] : The user for the backups # @see https://www.freedesktop.org/software/systemd/man/machine-id.html - -backup_time="$(date '+%Y%m%d%H%M%S')" -docker_backups_mount="/Backups/" -native_backups_mount_prefix="$(test -z "$1" && echo "$HOME" || echo "/home/$1")" -native_backups_mount="$native_backups_mount_prefix$docker_backups_mount" +backup_time="$(date '+%Y%m%d%H%M%S')"; +backups_folder="/Backups/"; for docker_container_name in $(docker ps --format '{{.Names}}'); do echo "stop container: $docker_container_name" && docker stop "$docker_container_name" for source_path in $(docker inspect --format '{{ range .Mounts }}{{ if eq .Type "volume" }}{{ println .Destination }}{{ end }}{{ end }}' "$docker_container_name"); do - repository_name="$(cd $(dirname "$(readlink -f "${0}")") && basename `git rev-parse --show-toplevel`)"; + repository_name="$(cd "$(dirname "$(readlink -f "${0}")")" && basename `git rev-parse --show-toplevel`)"; machine_id="$(sha256sum /etc/machine-id | head -c 64)"; - backup_repository_folder="$docker_backups_mount$machine_id/$repository_name/"; + backup_repository_folder="$backups_folder$machine_id/$repository_name/"; destination_path="$backup_repository_folder""latest/$docker_container_name$source_path"; log_path="$backup_repository_folder""log.txt"; backup_dir_path="$backup_repository_folder""diffs/$backup_time/$docker_container_name$source_path"; - if [ -d "$native_backups_mount_prefix$destination_path" ] + if [ -d "$destination_path" ] then - echo "backup: $source_path" + echo "backup: $source_path"; else echo "first backup: $source_path" - mkdir -vp "$native_backups_mount_prefix$destination_path"; - mkdir -vp "$native_backups_mount_prefix$backup_dir_path"; + mkdir -vp "$destination_path"; + mkdir -vp "$backup_dir_path"; fi - docker run --rm --volumes-from "$docker_container_name" -v "$native_backups_mount:$docker_backups_mount" "kevinveenbirkenbach/alpine-rsync" sh -c " + docker run --rm --volumes-from "$docker_container_name" -v "$backups_folder:$backups_folder" "kevinveenbirkenbach/alpine-rsync" sh -c " rsync -abvv --delete --delete-excluded --log-file=$log_path --backup-dir=$backup_dir_path '$source_path/' $destination_path"; done - echo "start container: $docker_container_name" && docker start "$docker_container_name" + echo "start container: $docker_container_name" && docker start "$docker_container_name"; done