-
Notifications
You must be signed in to change notification settings - Fork 2
/
ctimer_fetch
executable file
·60 lines (44 loc) · 1.6 KB
/
ctimer_fetch
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
#!/usr/bin/env bash
# Fetch files specified by path from remote site
#set -x
if [ ! -f "$1" ] || [ ! -f "$1" ]; then
echo "Please provide host-named file with list of paths (or directory with such fiels) to rsync as command line argument."
exit 1
fi
host=$(basename $1)
dir="./${host}_files_$(date +%Y-%m-%d_%H-%M)"
mkdir -p "${dir}/tree/"
mkdir -p "${dir}/_meta/"
rm -f "${dir}/_meta/paths.txt"
rsync -azh --files-from="$1" "${host}":/ "${dir}/tree/"
# copy files to flat folder and add numerical suffix
function safecp() {
idx=1 # set the copy index to 1
fn="${file##*/}" # destination file name (dfn) w/path stripped
dfn="$fn"
while [ -f "${dir}/${dfn}" ]; do # test if $dfn exist in output_dir
dfn="${fn%.*}_$((idx++)).${fn##*.}" # if so, add copy index "_#" before extenison (increment until unique)
done
cp "$file" "${dir}/${dfn}"
echo "${dfn} - ${file}" >> "${dir}/_meta/paths.txt"
}
# copy files to flat folder and add md5 hash suffix, if unique
function hashcp() {
hash=$(md5sum -b "$file" | cut -f1 -d' ')
if [[ ! ${hashes[*]} =~ $hash ]]; then
hashes+=($hash)
fn="${file##*/}"
dfn="${fn%.*}_${hash::8}.${fn##*.}"
cp "$file" "${dir}/${dfn}"
echo "${dfn} - ${file}" >> "${dir}/_meta/paths.txt"
else
echo "${file} DUPLICATE with ${hash}" >> "${dir}/_meta/paths.txt"
fi
}
hashes=()
while read -d '' file; do
hashcp </dev/null
done < <(find "${dir}/tree/" -type f -print0)
# printf "%s\n" "${hashes[@]}" >> "${dir}/_meta/paths.txt"
rm -rf "${dir}/tree/"
mv "$1" "${dir}"/_meta/"$host"