-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-parity.sh
executable file
·76 lines (62 loc) · 1.76 KB
/
create-parity.sh
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/bash
# set -x
# DEBUG=true
# path to par2
PAR2_DIR=par2
# percentage redundancy desired
PERCENT=5
# oldest file for which to create parchive files (-3 = 3 days before present)
# FIND_OPTS="-mtime -1"
create_par2(){
FILE="${1}"
DIR=$(dirname "${FILE}")
FILENAME=$(basename "${FILE}")
PARITY_DIR="${PAR2_DIR}/${DIR}"
PARITY_FILENAME="${PARITY_DIR}/${FILENAME}"
# create directory if it doesn't exist
if [[ ! -d "${PARITY_DIR}" ]]; then
echo making directory "${PARITY_DIR}"
mkdir -p "${PARITY_DIR}"
fi
# create par file if it doesn't exist
if [ ! -f "${PARITY_FILENAME}-${PERCENT}.par2" ]; then
[ -n "${DEBUG}" ] && echo "run: par2 create -q -B ./ -n1 -r${PERCENT} ${PARITY_FILENAME} ..."
[ -n "${DEBUG}" ] && echo "in $(pwd)"
par2 create -q -B ./ -n1 -r${PERCENT} "${PARITY_FILENAME}" "${FILE}"
# remove index; rename main file
rm "${PARITY_FILENAME}".par2
mv "${PARITY_FILENAME}".vol*par2 "${PARITY_FILENAME}-${PERCENT}".par2
else
[ -n "${VERIFY}" ] || return
par2 verify -qq -B ./ "${PARITY_FILENAME}-${PERCENT}".par2 || \
{ echo "${FILE} needs REPAIR" && \
par2 repair -B ./ "${PARITY_FILENAME}-${PERCENT}".par2
}
fi
}
repo_scan(){
create_par2 ./config
# find any files changed within $mtime days and pipe them into the do loop
# shellcheck disable=SC2086
find . ${FIND_OPTS} \
-type f \
-not -name "*.sh" \
\( -path "./data/*" -o \
-path "./index/*" -o \
-path "./keys/*" -o \
-path "./snapshots/*" \) \
-print0 | while IFS= read -r -d '' FIND_FILE
do
[ -n "${DEBUG}" ] && echo "${FIND_FILE}"
create_par2 "${FIND_FILE}"
done
# du -hs "${PAR2_DIR}"
}
verify_file(){
VERIFY=true
repo_scan
}
delete_parity(){
rm -rf "${PAR2_DIR}"
}
repo_scan