Files
MemRelay/scripts/restore.sh
T

41 lines
1.3 KiB
Bash

#!/bin/sh
set -eu
if [ "$#" -lt 1 ]; then
echo "Usage: $0 <archive.tar.gz> [--force]" >&2
exit 2
fi
archive="$1"
force="${2:-}"
compose_file="${COMPOSE_FILE:-compose.yaml}"
data_path="${MEMRELAY_DATA_PATH:-./data}"
archive="$(cd "$(dirname "$archive")" && pwd -P)/$(basename "$archive")"
if [ -f "$archive.sha256" ]; then
(cd "$(dirname "$archive")" && sha256sum -c "$(basename "$archive").sha256")
fi
mkdir -p "$data_path"
data_path="$(cd "$data_path" && pwd -P)"
if [ "$data_path" = "/" ]; then
echo "Refusing to restore into the filesystem root." >&2
exit 1
fi
if [ -n "$(find "$data_path" -mindepth 1 -maxdepth 1 -print -quit)" ] && [ "$force" != "--force" ]; then
echo "Target data directory is not empty. Re-run with --force after verifying the archive." >&2
exit 1
fi
docker compose -f "$compose_file" stop
trap 'docker compose -f "$compose_file" up -d' EXIT INT TERM
if [ "$force" = "--force" ]; then
find "$data_path" -mindepth 1 -maxdepth 1 -exec rm -rf -- {} +
fi
docker run --rm --user "$(id -u):$(id -g)" \
--mount "type=bind,source=$(dirname "$archive"),target=/backup,readonly" \
--mount "type=bind,source=$data_path,target=/target" \
alpine:3.22 tar -xzf "/backup/$(basename "$archive")" -C /target
echo "Restore completed: $data_path"