mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2024-10-18 10:13:10 +02:00
34 lines
1.9 KiB
Bash
34 lines
1.9 KiB
Bash
#!/bin/bash
|
|
# @todo rethink optional parameter
|
|
# @param $1 [optional] : The path for the backups folder. DEFAULT $HOME/Backups/
|
|
# @see https://www.freedesktop.org/software/systemd/man/machine-id.html
|
|
|
|
backup_time="$(date '+%Y%m%d%H%M%S')"
|
|
docker_backups_mount="$(test -z "$1" && echo "/Backups/" || echo "/$(basename "$1")/")"
|
|
native_backups_mount_prefix="$(test -z "$1" && echo "$HOME" || echo "/$(dirname "$1")/")"
|
|
native_backups_mount="$native_backups_mount_prefix$docker_backups_mount"
|
|
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="$(basename -s .git `git config --get remote.origin.url`)";
|
|
machine_id="$(sha256sum /etc/machine-id | head -c 64)";
|
|
backup_repository_folder="$docker_backups_mount$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" ]
|
|
then
|
|
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";
|
|
fi
|
|
docker run --rm --volumes-from "$docker_container_name" -v "$native_backups_mount:$docker_backups_mount" "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"
|
|
done
|