Refactored and implemented versioning

This commit is contained in:
Kevin Veen-Birkenbach 2020-10-12 11:35:02 +02:00
parent c9cba04199
commit 84dbc469df

View File

@ -1,22 +1,33 @@
#!/bin/bash
host_backup_folder_path="$HOME/Backup/docker/"
docker_backup_folder_path="/Backup/docker/"
mkdir -p "$host_backup_folder_path"
backup_time="$(date '+%Y%m%d%H%M%S')"
docker_backups_mount="/Backups/"
native_backups_mount_prefix="$HOME"
native_backups_mount="$native_backups_mount_prefix$docker_backups_mount"
for docker_container_name in $(docker ps --format '{{.Names}}');
do
backup_dir_base_path="$docker_backup_folder_path""archive/$(date '+%Y%m%d%H%M%S')/";
echo "stop container: $docker_container_name" && docker stop "$docker_container_name"
for rsync_source_path in $(docker inspect --format '{{ range .Mounts }}{{ if eq .Type "volume" }}{{ println .Destination }}{{ end }}{{ end }}' "$docker_container_name");
for source_path in $(docker inspect --format '{{ range .Mounts }}{{ if eq .Type "volume" }}{{ println .Destination }}{{ end }}{{ end }}' "$docker_container_name");
do
rsync_docker_destination_path="$docker_backup_folder_path""latest/""$docker_container_name$rsync_source_path";
rsync_docker_backup_dir_path="$backup_dir_base_path$docker_container_name$rsync_source_path";
echo "backup $rsync_source_path..."
docker run --rm --volumes-from "$docker_container_name" -v "$host_backup_folder_path:$docker_backup_folder_path" "kevinveenbirkenbach/alpine-rsync" sh -c "
test -d $rsync_source_path &&
mkdir -p $rsync_docker_destination_path &&
rsync -a --delete $rsync_source_path $rsync_docker_destination_path ||
mkdir -p $(dirname $rsync_docker_destination_path) &&
rsync -a --delete $rsync_source_path $(dirname $rsync_docker_destination_path)";
application_path="$docker_backups_mount$(cat /etc/machine-id)/docker/$docker_container_name"
first_destination_path="$application_path""first""$source_path";
latest_destination_path="$application_path""latest""$source_path";
backup_dir_path="$application_path""diffs/$backup_time/$source_path"
if [ -d "$native_backups_mount_prefix$application_path)" ]
then
echo "backup: $source_path"
destination_path="$latest_destination_path";
else
echo "first backup: $source_path"
destination_path="$first_destination_path";
fi
mkdir "$native_backups_mount_prefix$destination_path";
docker run --rm --volumes-from "$docker_container_name" -v "$native_backups_mount:$docker_backups_mount" "kevinveenbirkenbach/alpine-rsync" sh -c "
test -d $source_path &&
mkdir -p $destination_path &&
rsync -a --delete --backup-dir="$backup_dir_path" $source_path $destination_path ||
mkdir -p $(dirname $destination_path) &&
rsync -a --delete --backup-dir="$(dirname $backup_dir_path)" $source_path $(dirname $destination_path)";
done
echo "start container: $docker_container_name" && docker start "$docker_container_name"
done