-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnextscan.sh
executable file
·103 lines (92 loc) · 3.05 KB
/
nextscan.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
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
#!/bin/bash
local_external_share="/path/to/share"
exclude="Folder"
sharename="Nextcloudsharename"
modifications="/tmp/modifications"
mod_to_process="/tmp/mod_to_process"
fullscan=120
fullscandt=86400
monitor_changes(){
while read -r FILE; do
echo "${FILE}" >> "${modifications}"
done < <(inotifywait -rmq --format '%w' "${local_external_share}" --exclude "${exclude}" -e create,delete,modify,moved_to,moved_from)
}
nextcloudusers=()
get_nextcloud_users(){
nextcloudusers=()
external_storage=$(docker exec -i -u www-data nextcloud php occ files_external:list)
while read -r line; do
user=$(echo "${line}" | cut -d " " -f2 | tr -dc '[:alnum:]\n')
if [[ ${external_storage} =~ ${user} ]]; then
nextcloudusers+=("${user}")
fi
done < <(docker exec -i -u www-data nextcloud php occ user:list)
}
userstoscan=()
get_userstoscan(){
userstoscan=()
group=$(stat -c %G "$1")
users=$(members "$group")
IFS=' ' read -r -a systemusers <<< "$users"
for nextclouduser in "${nextcloudusers[@]}"
do
if [[ "${systemusers[*],,}" =~ ${nextclouduser,,} ]]; then
userstoscan+=("${nextclouduser}")
fi
done
}
parsed_mod=()
parse_modifications() {
mv ${modifications} ${mod_to_process}
mapfile -t parsed_mod < <(sort -u < ${mod_to_process})
}
do_periodic_fullscan(){
echo "$(date) Periodic Fullscan"
docker exec -i -u www-data nextcloud php occ files:scan --all
((fullscan=SECONDS+fullscandt))
}
do_delayed_fullscan(){
sleep 60
parse_modifications
echo "$(date) Fullscan because multiple files modifications"
docker exec -i -u www-data nextcloud php occ files:scan --all
}
do_scan(){
nextcloudfilepath=${1#"$local_external_share"}
for user in "${userstoscan[@]}"
do
nextcloudpath="${user}/files/${sharename}${nextcloudfilepath}"
echo "$(date) scan ${nextcloudpath}"
docker exec -i -u www-data nextcloud php occ files:scan --path="${nextcloudpath}"
done
}
main(){
sleep 60
monitor_changes &
get_nextcloud_users
echo "${nextcloudusers[@]}"
while true
do
if [ $SECONDS -ge $fullscan ]; then
do_periodic_fullscan
continue
fi
if [ ! -f ${modifications} ]; then
echo "Nothing found"
sleep 15
continue
fi
parse_modifications
if [ "${#parsed_mod[@]}" -ge 10 ]; then
do_delayed_fullscan
continue
fi
for path in "${parsed_mod[@]}"
do
get_userstoscan "${path}"
do_scan "${path}"
done
sleep 15
done
}
main