summaryrefslogtreecommitdiff
path: root/bin/bisect-database-dumps
blob: 700e3321aa9abd837d44643cba0ed5dc1897f2fe (plain)
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
#!/bin/sh

# bisect through the database dumps to see when a specific thing
# appeared first

dumps=$(
  find '/data/backup/mysql' -mindepth 1 -maxdepth 1 -name 'database-*.xz' -exec ls -tr {} +
)

has=$(
  printf '%s\n' "$dumps" | \
    tail -n1
)

if ! xzgrep -q "$@" "${has}"; then
  >&2 printf 'cannot find "%s" in the latest dump - swapping directions\n' "$*"
  dumps=$(
    printf '%s\n' "${dumps}" | \
      tac
  )
  has=$(
    printf '%s\n' "$dumps" | \
      tail -n1
  )
  if ! xzgrep -q "$@" "${has}"; then
    >&2 printf 'cannot find "%s" in the earliest dump either - swapping directions did not help\n' "$*"
    exit 1
  fi
fi

has_not=$(
  printf '%s\n' "$dumps" | \
    head -n1
)

if xzgrep -q "$@" "${has_not}"; then
  >&2 printf 'first dump does also have "%s"\n' "$*"
  exit 1
fi

count=$(
  printf '%s\n' "${dumps}" | \
    wc -l
)

>&2 printf '%s dumps to check\n' "${count}"

while [ "${count}" -gt 2 ]; do
  pivot=$(
    printf '%s\n' "${dumps}" | \
      sed -n '
        \@^'"${has_not}"'$@,\@^'"${has}"'$@p
      ' | \
      sed -n '
        '"$((count/2+1))"' p
      '
  )
  >&2 printf 'looking into "%s" ... ' "${pivot}"
  if xzgrep -q "$@" "${pivot}"; then
    >&2 printf 'has'
    has="${pivot}"
  else
    >&2 printf 'has not'
    has_not="${pivot}"
  fi
  count=$(
    printf '%s\n' "${dumps}" | \
      sed -n '
        \@^'"${has_not}"'$@,\@^'"${has}"'$@p
      ' | \
      wc -l
  )
  >&2 printf ' (%s dumps remaining)\n' "${count}"
done

printf '%s has not\n%s has\n' "${has_not}" "${has}"