#!/bin/bash # Restore a gitv-backup snapshot into an empty directory and verify it there. It never writes # into the live tree: move what you need into place afterwards (see the README's recovery steps). # # gitv-restore list the snapshots, newest last # gitv-restore find OWNER/NAME the snapshots holding that repository, newest last # gitv-restore DIR copy it to DIR (which must be empty or absent) and check it # gitv-restore DIR OWNER/NAME # copy just that repository to DIR/NAME.git and check it; # put it back with: gitv import OWNER NAME DIR/NAME.git # gitv-restore check restore latest to a scratch folder on the volume, check # it, and remove it: a drill proving backups restore # # GITV_BACKUP_DIR as for gitv-backup. set -euo pipefail umask 077 base=${GITV_BACKUP_DIR:-/mnt/gitv-backup}/gitv say() { echo "gitv-restore: $*" >&2; } die() { say "$*"; exit 1; } snapshot_dir() { local name=$1 [[ $name == latest ]] && name=$(basename "$(readlink -f "$base/latest" 2>/dev/null || true)") [[ $name =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{6}Z$ ]] || die "no such snapshot: $1 (see: gitv-restore list)" [[ -d $base/snapshots/$name ]] || die "no such snapshot: $name" echo "$base/snapshots/$name" } # Never copy anything but a snapshot: an empty or wrong source must not become `rsync /`. check_snapshot() { [[ -n $1 && -d $1/repos && -f $1/MANIFEST ]] || die "not a snapshot: '$1'" } # A restore goes only into a folder that is empty or absent, so it never mixes with anything. check_target() { if [[ -e $1 ]] && [[ -n $(ls -A "$1") ]]; then die "$1 isn't empty"; fi } # Whether git can read the whole repository, with no user or system configuration. verify() { env -i PATH=/usr/bin:/bin HOME=/nonexistent GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_GLOBAL=/dev/null \ git -c safe.directory='*' --git-dir="$1" fsck --no-progress --full >/dev/null } # OWNER/NAME, checked against gitv's name rule, into $owner and $name. parse_repo() { local part='[a-z0-9][a-z0-9_-]{0,62}' [[ $1 =~ ^($part)/($part)$ ]] || die "name the repository as OWNER/NAME: $1" owner=${BASH_REMATCH[1]} name=${BASH_REMATCH[2]} } # The repository in a snapshot, if it holds one: real directories only, never links. snapshot_repo() { local dir=$1/repos/$owner [[ -d $dir && ! -L $dir && -d $dir/$name.git && ! -L $dir/$name.git ]] && echo "$dir/$name.git" } # Copy a snapshot to an empty target with owners and modes kept, then fsck every repository. restore() { local from=$1 to=$2 check_snapshot "$from" check_target "$to" mkdir -p "$to" rsync -a "$from/" "$to/" local count=0 failed=0 repo while IFS= read -r -d '' repo; do count=$((count + 1)) if ! verify "$repo"; then say "fsck failed for ${repo#"$to"/repos/}" failed=$((failed + 1)) fi done < <(find "$to/repos" -mindepth 2 -maxdepth 2 -type d -name '*.git' -print0) ((failed == 0)) || die "$failed of $count repositories failed fsck" say "restored $(basename "$from") to $to: $count repositories verified" } # Copy one repository out of a snapshot to TARGET/NAME.git, owners and modes kept, and fsck it. restore_one() { local from=$1 to=$2 source check_snapshot "$from" source=$(snapshot_repo "$from") || die "$(basename "$from") doesn't hold $owner/$name (see: gitv-restore find $owner/$name)" check_target "$to" mkdir -p "$to" rsync -a "$source" "$to/" verify "$to/$name.git" || die "fsck failed for the restored $owner/$name" say "restored $owner/$name from $(basename "$from") to $to/$name.git and verified it" say "put it back with: gitv import $owner $name $to/$name.git (or another name)" } if (($# == 0)); then sed -n '2,13p' "$0" exit 0 fi case $1 in list) ls -1 "$base/snapshots" ;; check) # A failed lookup must stop here: `die` inside $(...) only ends the subshell. from=$(snapshot_dir latest) || exit 1 scratch=$base/restore-check rm -rf "$scratch" trap 'rm -rf "$scratch"' EXIT restore "$from" "$scratch" ;; find) [[ $# == 2 ]] || die "usage: gitv-restore find OWNER/NAME" parse_repo "$2" found=0 for snapshot in "$base"/snapshots/*/; do snapshot=${snapshot%/} if snapshot_repo "$snapshot" >/dev/null; then basename "$snapshot" found=1 fi done ((found)) || die "no snapshot holds $owner/$name" ;; -h | --help) sed -n '2,13p' "$0" ;; *) [[ $# == 2 || $# == 3 ]] || die "usage: gitv-restore DIR [OWNER/NAME]" from=$(snapshot_dir "$1") || exit 1 if [[ $# == 3 ]]; then parse_repo "$3" restore_one "$from" "$2" else restore "$from" "$2" fi ;; esac