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
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"
}
check_snapshot() {
[[ -n $1 && -d $1/repos && -f $1/MANIFEST ]] || die "not a snapshot: '$1'"
}
check_target() {
if [[ -e $1 ]] && [[ -n $(ls -A "$1") ]]; then die "$1 isn't empty"; fi
}
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
}
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]}
}
snapshot_repo() {
local dir=$1/repos/$owner
[[ -d $dir && ! -L $dir && -d $dir/$name.git && ! -L $dir/$name.git ]] && echo "$dir/$name.git"
}
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"
}
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)
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