GITv
master
gitv-restore134 lines, 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/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 <snapshot|latest> DIR copy it to DIR (which must be empty or absent) and check it
#   gitv-restore <snapshot|latest> 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 <snapshot|latest> 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