Refactored full code

This commit is contained in:
Kevin Veen-Birkenbach 2020-12-26 16:31:47 +01:00
parent 7594f5047d
commit 996c9ba2f0
3 changed files with 19 additions and 21 deletions

2
.travis.yml Normal file
View File

@ -0,0 +1,2 @@
language: shell
script: shellcheck $(find . -type f -name '*.sh')

View File

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

View File

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