Merge branch 'main' of github.com:kevinveenbirkenbach/docker-volume-backup

This commit is contained in:
Kevin Veen-Birkenbach 2021-08-20 09:16:05 +02:00
commit f3979ce611
2 changed files with 36 additions and 16 deletions

View File

@ -16,6 +16,13 @@ Execute:
./docker-volume-backup.sh
```
## Recover
Execute:
```bash
./docker-volume-recover.sh {{volume_name}} {{backup_path}}
```
## Debug
To checkout what's going on in the mount container type in the following command:

View File

@ -1,29 +1,42 @@
#!/bin/bash
# If rsync stucks consider:
# Just backups volumes of running containers
# If rsync stucks consider:
# @see https://stackoverflow.com/questions/20773118/rsync-suddenly-hanging-indefinitely-during-transfers
#
backup_time="$(date '+%Y%m%d%H%M%S')";
backups_folder="/Backups/";
for docker_container_name in $(docker ps --format '{{.Names}}');
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="$backups_folder$machine_id/$repository_name/";
for volume_name in $(docker volume ls --format '{{.Name}}');
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");
echo "start backup routine: $volume_name";
containers="$(docker ps --filter volume="$volume_name" --format '{{.Names}}')";
containers_array=($containers)
container=${containers_array[0]}
echo "stop containers:" && docker stop $containers
for source_path in $(docker inspect --format "{{ range .Mounts }}{{ if eq .Type \"volume\"}}{{ if eq .Name \"$volume_name\"}}{{ println .Destination }}{{ end }}{{ end }}{{ end }}" "$container");
do
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="$backups_folder$machine_id/$repository_name/";
destination_path="$backup_repository_folder""latest/$docker_container_name$source_path";
destination_path="$backup_repository_folder""latest/$volume_name";
raw_destination_path="$destination_path/raw"
prepared_destination_path="$destination_path/prepared"
log_path="$backup_repository_folder""log.txt";
backup_dir_path="$backup_repository_folder""diffs/$backup_time/$docker_container_name$source_path";
backup_dir_path="$backup_repository_folder""diffs/$backup_time/$volume_name";
raw_backup_dir_path="$backup_dir_path/raw";
prepared_backup_dir_path="$backup_dir_path/prepared";
if [ -d "$destination_path" ]
then
echo "backup: $source_path";
echo "backup volume: $volume_name";
else
echo "first backup: $source_path"
mkdir -vp "$destination_path";
mkdir -vp "$backup_dir_path";
echo "first backup volume: $volume_name"
mkdir -vp "$raw_destination_path";
mkdir -vp "$raw_backup_dir_path";
mkdir -vp "$prepared_destination_path";
mkdir -vp "$prepared_backup_dir_path";
fi
docker run --rm --volumes-from "$docker_container_name" -v "$backups_folder:$backups_folder" "kevinveenbirkenbach/alpine-rsync" sh -c "
rsync -abP --delete --delete-excluded --log-file=$log_path --backup-dir=$backup_dir_path '$source_path/' $destination_path";
docker run --rm --volumes-from "$container" -v "$backups_folder:$backups_folder" "kevinveenbirkenbach/alpine-rsync" sh -c "
rsync -abP --delete --delete-excluded --log-file=$log_path --backup-dir=$raw_backup_dir_path '$source_path/' $raw_destination_path";
echo "start containers:" && docker start $containers;
done
echo "start container: $docker_container_name" && docker start "$docker_container_name";
echo "end backup routine: $volume_name";
done