#!/bin/bash
# restore test: restore a subset of a tag's latest snapshot, verify, compare with the B2 source hashes, delete scratch
# usage: restore-test.sh <tag> <include-subpath>  (run as root on azkaban)
set -u
export RESTIC_REPOSITORY=/tmpool/cloud-copy/restic RESTIC_PASSWORD_FILE=/etc/cloud-copy/restic.pw RCLONE_CONFIG=/etc/cloud-copy/rclone.conf XDG_CACHE_HOME=/var/cache/cloud-copy
tag=$1; sub=$2; T=/tmpool/cloud-copy/restore-test-$tag; rm -rf $T; mkdir -p $T
snap=$(restic snapshots --tag "$tag" --json --latest 1 | python3 -c 'import json,sys;print(json.load(sys.stdin)[-1]["short_id"])')
echo "snapshot $snap"
restic restore "$snap" --target $T --include "/run/cloud-copy/$tag/$sub" --verify 2>&1 | tail -3
R=$T/run/cloud-copy/$tag
case $tag in
  forgejo) rem=b2-forgejo:ctg-forgejo ;; storage) rem=b2-storage:ctg-storage ;; files) rem=b2-files:ctg-files ;;
  nextcloud-*) rem=nc-${tag#nextcloud-}: ;;
esac
echo "files restored: $(find $R -type f | wc -l), bytes: $(du -sb $R | cut -f1)"
rclone check "$R" "$rem" --one-way --include "/${sub#/}" --include "/${sub#/}/**" 2>&1 | tail -4
rm -rf $T
