blob: 8865af50209bc51839aa635273ada1ece5424725 (
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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
#!/bin/bash
# backupStatistics version #VERSION#
set -e
[ -r "#ETCDIR#/backup.conf" ] && \
. "#ETCDIR#/backup.conf"
do_stage()
{
case $1 in
1)
if [ "$2" == '##DESCRIBE##' ]
then
echo 'generate lists $filename -> $inode'
return 0
fi
dest="${backups["${backupID}"]%% *}"
dest="${dest%/}"
while [ ! -d "${dest}" ] && [ ${maxWait} -gt 0 ]
do
sleep 1
maxWait=$[${maxWait}-1]
done
rm -f "${cacheDir}/${backupID}.inodes"
for dat in $(ls "${dest}")
do
echo "${dat}:"
find "${dest}/${dat}" -type f -exec \
stat -c'%i %n' {} \; >> \
"${cacheDir}/${backupID}.inodes"
done
;;
2)
if [ "$2" == '##DESCRIBE##' ]
then
echo 'sort and partition previous lists by $inode'
return 0
fi
tmpDirA="$(mktemp -d)"
tmpDirB="$(mktemp -d "${cacheDir}/tmp.XXXXXX")"
rm -rf "${cacheDir}/${backupID}.inodes.sorted"
mkdir "${cacheDir}/${backupID}.inodes.sorted"
sort -T "${tmpDirA}" -T "${tmpDirB}" -u "${cacheDir}/${backupID}.inodes" | \
while read -r line
do
part="${line:0:4}"
part="${part%% *}"
echo "${line}" >> \
"${cacheDir}/${backupID}.inodes.sorted/part.${part}"
done
rmdir "${tmpDirA}" "${tmpDirB}"
;;
3)
if [ "$2" == '##DESCRIBE##' ]
then
echo 'generate lists $inode -> $contentHash'
return 0
fi
cat "${cacheDir}/${backupID}.inodes.sorted/"part.* | \
uniq -m1 | \
parallel \
sha512sum {=s/^[[:digit:]]\+ //=} \| \
sed "\"s|^\([0-9a-f]\{128\}\) .*\$|\1 "{=s/^\([[:digit:]]\+\) .*/\\1/=}"|\"" \
\; > \
"${cacheDir}/${backupID}.content"
;;
4)
if [ "$2" == '##DESCRIBE##' ]
then
echo 'sort previous lists by $contentHash'
return 0
fi
tmpDirA="$(mktemp -d)"
tmpDirB="$(mktemp -d "${cacheDir}/tmp.XXXXXX")"
sort -T "${tmpDirA}" -T "${tmpDirB}" "${cacheDir}/${backupID}.content" > \
"${cacheDir}/${backupID}.content.sorted"
rmdir "${tmpDirA}" "${tmpDirB}"
;;
5)
if [ "$2" == '##DESCRIBE##' ]
then
echo 'find duplicate hashes'
return 0
fi
(
uniq -m1 --all-repeated=separate "${cacheDir}/${backupID}.content.sorted"
echo ""
) | \
sed 's|^\S\+ ||' | \
while read s
do
if [ -z "${s}" ]
then
echo ""
else
echo -n "${s} "
fi
done | \
sed 's| $||' > \
"${cacheDir}/${backupID}.duplicates"
;;
6)
if [ "$2" == '##DESCRIBE##' ]
then
echo 'remove inodes with duplicate hashes'
return 0
fi
if [ -r "${cacheDir}/next.action" ]
then
startInode="$(cat "${cacheDir}/next.action")"
sed "
:vor;
/ ${startInode}\( \|$\)/{
s@^\(\S\+ \)\(.* \)\?${startInode}\( \|$\)@\1${startInode}\3@;
bnach
};
d;
bvor;
:nach;
n;
bnach
" "${cacheDir}/${backupID}.duplicates"
else
cat "${cacheDir}/${backupID}.duplicates"
fi | \
while read line
do
originalInode="${line%% *}"
original="$(
grep -m1 "^${originalInode} " "${cacheDir}/${backupID}.inodes.sorted/part.${originalInode:0:4}" | \
sed 's|^\S\+ ||'
)"
for kopieInode in ${line#* }
do
echo "${kopieInode}" > "${cacheDir}/next.action2"
mv "${cacheDir}/next.action2" "${cacheDir}/next.action"
OIFS="${IFS}"
IFS="$(printf '\n\t')"
for kopie in $(
grep "^${kopieInode} " "${cacheDir}/${backupID}.inodes.sorted/part.${kopieInode:0:4}" | \
sed 's|^\S\+ ||'
)
do
IFS="${OIFS}"
if ${paranoid}
then
diff "${original}" "${kopie}"
fi
if [ $(stat -c'%h' "${original}") -ge 65000 ]
then
echo "rm \"${original}\""
echo "ln \"${kopie}\" \"${original}\""
if ! ${dummy}
then
rm "${original}"
ln "${kopie}" "${original}"
fi
else
echo "rm \"${kopie}\""
echo "ln \"${original}\" \"${kopie}\""
if ! ${dummy}
then
rm "${kopie}"
ln "${original}" "${kopie}"
fi
fi
done
done
done
if [ -r "${cacheDir}/next.action" ] && \
grep -q " $(cat "${cacheDir}/next.action")\( \|$\)" "${cacheDir}/${backupID}.duplicates"
then
rm -f "${cacheDir}/next.action" "${cacheDir}/next.action2"
fi
;;
esac
}
usage()
{
>&2 echo \
'Usage: backupStatistics [OPTION]
Search and tidy duplicate and not-hardlinked files in the backups.
With no options, tidy up all backups. THIS CAN BE VERY TIME CONSUMING.
Mandatory arguments to long options are mandatory for short options too.
-d, --dummy only generate lists, do not modify backupfiles
-m, --max=maxNum stop execution after step maxNum
-p, --paranoid test for file differences before relinking (test _should_ be obsolete)
-s, --skip=skipNum skip first skipNum steps
#HELPTEXT# #
the executed steps are:'
for ((stage=1; stage<=#NUMSTAGES#; stage++))
do
>&2 echo ''
>&2 echo " ${stage}. $(do_stage ${stage} '##DESCRIBE##')"
done
>&2 echo ''
[ -z "$1" ] && exit 1
exit $1
}
eval set -- "$(
getopt -o dm:ps: \
--long dummy \
--long help \
--long max: \
--long paranoid \
--long skip: \
--long version \
-n "$(basename "$0")" -- "$@" || \
echo usage
)"
dummy=false
maxNum=#NUMSTAGES#
paranoid=false
skipNum=0
while true; do
case "$1" in
-d|--dummy)
dummy=true
;;
--help)
usage 0
;;
-m|--max)
shift
maxNum=$1
;;
-p|--paranoid)
paranoid=true
;;
-s|--skip)
shift
skipNum=$1
;;
--version)
>&2 echo '#VERSION#'
exit 0
;;
--)
shift
[ $# -gt 0 ] && echo 'ERROR: Unknown parameter: '"$#" && usage
break
;;
*)
>&2 echo 'That should not happen, '"$1"' unknown though ...'
exit -1
;;
esac
shift
done
if [ ! -d "${cacheDir}" ] || [ -z "${cacheDir}" ]
then
>&2 "ERROR: Cache directory must exist, '${cacheDir}' does not! Closing."
exit 1
fi
if [ ! "${skipNum}" -ge 0 ] || \
[ ! "${skipNum}" -le #NUMSTAGES# ] || \
[ ! "${maxNum}" -ge 0 ] || \
[ ! "${maxNum}" -le #NUMSTAGES# ]
then
usage
fi
for ((stage=${skipNum}+1; stage<=${maxNum}; stage++))
do
echo "entering stage ${stage} ($(do_stage ${stage} '##DESCRIBE##')) ..."
for backupID in "${!backups[@]}"
do
echo "${backupID}:"
do_stage ${stage} "${backupID}"
done
echo "... stage ${stage} completed."
done
|