summaryrefslogtreecommitdiff
path: root/fast-repair.in
blob: 9d5224a3742f45e8aa64399bb3095b61b7dc3acc (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
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash

# fast-repair version #VERSION#

[ -r "/etc/backup.conf" ] && \
  . "/etc/backup.conf"

dummy=false
if [ $# -eq 1 ] \
  && [ "$1" == "-d" ]
then
  shift
  dummy=true
fi

if [ $# -ne 0 ]
then
  >&2 echo "too many arguments: '$@'"
  exit 1
fi

for backupID in "${!backups[@]}"
do
  backupDir="${backups["${backupID}"]%% *}"
  lastDate="$(
    ls -1 "${backupDir}" | \
      sort -r | \
      grep -m1 '^[0-9]\{4\}\(_[0-9]\{2\}\)\{2\}$'
  )"

  dateList=$(
    ls -1 "${backupDir}" | \
      sort -r | \
      grep '^[0-9]\{4\}\(_[0-9]\{2\}\)\{2\}$' | \
      grep -v "^${lastDate}"
  )

  [ -z "${dateList}" ] \
    && continue

  set -e

  echo -n "${backupID}: "

  find "${backupDir}${lastDate}" -type f -links 1 | \
    wc -l

  find "${backupDir}${lastDate}" -type f -links 1 | \
    sed "s|^${backupDir}${lastDate}/||" | \
    while read -r datei
    do
      for date in ${dateList}
      do
        [ -f "${backupDir}${date}/${datei}" ] \
          || break
        diff -q "${backupDir}${lastDate}/${datei}" "${backupDir}${date}/${datei}" > /dev/null \
          || break
        nlinks=$(
          stat -c'%h' "${backupDir}${date}/${datei}"
        )
        [ ${nlinks} -eq 65000 ] \
          && continue
        rights=$(stat -c'%a' "${backupDir}${lastDate}/${datei}")
        uid=$(stat -c'%u' "${backupDir}${lastDate}/${datei}")
        gid=$(stat -c'%g' "${backupDir}${lastDate}/${datei}")
        timeModification=$(stat -c'%y' "${backupDir}${lastDate}/${datei}")

        echo rm \"${backupDir}${lastDate}/${datei}\"
        echo ln \"${backupDir}${date}/${datei}\" \"${backupDir}${lastDate}/${datei}\"
        echo chown ${uid}:${gid} \"${backupDir}${lastDate}/${datei}\"
        echo chmod ${rights} \"${backupDir}${lastDate}/${datei}\"
        echo touch -m -d \"${timeModification}\" \"${backupDir}${lastDate}/${datei}\"

        if ! ${dummy}
        then
          rm "${backupDir}${lastDate}/${datei}"
          ln "${backupDir}${date}/${datei}" "${backupDir}${lastDate}/${datei}"
          chown ${uid}:${gid} "${backupDir}${lastDate}/${datei}"
          chmod ${rights} "${backupDir}${lastDate}/${datei}"
          touch -m -d "${timeModification}" "${backupDir}${lastDate}/${datei}"
        fi
        break
      done
    done

  set +e

done