commit 31d5668e246b31bdc28696541f9db1d2f81238c2 Author: Yiyang Kang Date: Mon Sep 19 04:41:35 2022 +0800 initial commit diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..19376bf --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM alpine:latest + +RUN apk add --no-cache libarchive-tools zstd + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2ab83dc --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +image: + docker build --pull -t localhost/archiver . + +clean: + docker rmi localhost/archiver + +dump: + mkdir -p dumps + cd dumps && bash ../dump.sh + +.PHONY: image clean diff --git a/dump.sh b/dump.sh new file mode 100644 index 0000000..2217558 --- /dev/null +++ b/dump.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +set -e -o pipefail + +info() { + [ "$#" -gt 0 ] && echo -e "\033[1m>>> $*\033[0m" >&2 +} + +readarray -t volumes < <(docker volume ls --format "{{ .Name }}") + +for v in "${volumes[@]}"; do + info "Dumping \"$v\"" + docker run --rm -v "$v":/vol:ro localhost/archiver sh -c \ + "bsdtar -C /vol --numeric-owner -c . | zstdmt -15v" \ + > "$v".tar.zst + + docker volume inspect "$v" > "$v".json +done diff --git a/restore.sh b/restore.sh new file mode 100644 index 0000000..2bd6694 --- /dev/null +++ b/restore.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +set -e -o pipefail + +info() { + [ "$#" -gt 0 ] && echo -e "\033[1m>>> $*\033[0m" >&2 +} + +for i in *.json; do + [ -f "$i" ] || continue + vol_name=${i%.json} + archive=$vol_name.tar.zst + info "Recovering \"$vol_name\" from \"$archive\"" + + label_args=() + readarray -t labels < <(jq -r '.[0].Labels | to_entries | .[] | .key + "=" + .value' "$i") + for label in "${labels[@]}"; do + label_args+=(--label "$label") + done + + set -x + docker volume create "${label_args[@]}" "$vol_name" + docker run --rm -v "$vol_name":/vol -v "$(readlink -f "$archive")":/vol.tar.zst:ro \ + localhost/archiver bsdtar -C /vol --numeric-owner -xf /vol.tar.zst + set +x +done