Repository navigation
Expand file tree
/
Copy pathsync.sh
More file actions
executable file
·318 lines (266 loc) · 9.16 KB
/
Copy pathsync.sh
File metadata and controls
executable file
·318 lines (266 loc) · 9.16 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/bin/bash
# ---------------------------------------------------------------------
# --- Paths and Constants ---------------------------------------------
# ---------------------------------------------------------------------
IP_TEMPLATE="192.168.2.2xx"
PORT=32400
DEVICES_FROM=1
DEVICES_TO=15
REMOTE_USER="schueler"
EXCHANGE_PATH="/home/$REMOTE_USER/Schreibtisch/Austausch"
LOCAL_USER=$(whoami)
FETCH_PATH="/home/$LOCAL_USER/Schreibtisch/Eingesammelt"
SHARE_PATH="/home/$LOCAL_USER/Schreibtisch/Austeilen"
SHARE_ALL_PATH="$SHARE_PATH/Alle"
CONNECTIONS_FILE="/home/$LOCAL_USER/.cache/sync_devices.txt"
# GLOBALS
# Calculate string lengths for IP_TEMPLATE substring replace
IP_PREFIX=${IP_TEMPLATE%%x*}
SUFFLEN=$(( ${#IP_TEMPLATE} - ${#IP_PREFIX} ))
JOBS=()
DEVICE_LIST=()
# ---------------------------------------------------------------------
# --- Utility Functions -----------------------------------------------
# ---------------------------------------------------------------------
# Return $1 as string with $2 digits
get_n_digits() {
NUM=$1
while [ ${#NUM} -lt $2 ]; do
NUM="0${NUM}"
done
echo $NUM
}
# Return local directory name for device $1
get_dir_name () {
echo "S$( get_n_digits $1 $SUFFLEN)"
}
# Return IP for device $1
get_ip() {
echo "${IP_TEMPLATE%%x*}$(get_n_digits $1 $SUFFLEN)"
}
# Return SSH login for device $1
get_ssh_login () {
echo "${REMOTE_USER}@$( get_ip $1 )"
}
device_reachable () {
IP=$( get_ip $1 )
# netcat actually hangs sometimes if the ip address does not belong to the same local network
#nc -z -v -w1 $IP $PORT &>/dev/null
# Alternative: use ping
ping -c1 -W1 -q $IP &>/dev/null
if [ $? = "0" ]; then
echo "true"
else
echo "false"
fi
}
get_connected_devices () {
# wipe temporary file
echo -n "" > ~/.cache/sync_devices.txt
for n in $( seq $DEVICES_FROM $DEVICES_TO ); do
$(
if [ $( device_reachable $n) = "true" ]; then
echo "$n" >> ~/.cache/sync_devices.txt
fi
) &
done
wait
cat ~/.cache/sync_devices.txt
rm ~/.cache/sync_devices.txt
}
directory_empty () {
if [ -z "$(ls -A $1)" ]; then
echo "true"
else
echo "false"
fi
}
# Copy files via SSH from $1 to $2
copy_via_ssh () {
scp -r -P ${PORT} ${1}/* ${2}
}
# ---------------------------------------------------------------------
# --- Actual sharing/fetching -----------------------------------------
# ---------------------------------------------------------------------
# Share with devices (optional: from $1, else individual directory)
share () {
# setup default directory (e.g. $SHARE_ALL_PATH)
SRC=$1
DEVICE_LIST=$( get_connected_devices )
i=0
JOBS=()
for DEVICE in ${DEVICE_LIST[@]}; do
LOGIN=$( get_ssh_login $DEVICE )
DIR=$( get_dir_name $DEVICE )
if [ -z "$1" ]; then
# share from individual directory
SRC="${SHARE_PATH}/${DIR}"
fi
DST="${LOGIN}:${EXCHANGE_PATH}"
if [ $( directory_empty $SRC ) = "false" ]; then
copy_via_ssh $SRC $DST &
# Catch process ids in global variable
JOBS[$i]=$!
i=$((i + 1))
fi
done
}
# Fetch from all devices
fetch () {
DEVICE_LIST=$( get_connected_devices )
i=0
JOBS=()
for DEVICE in ${DEVICE_LIST[@]}; do
LOGIN=$( get_ssh_login $DEVICE )
DIR=$( get_dir_name $DEVICE )
SRC="${LOGIN}:${EXCHANGE_PATH}"
DST="${FETCH_PATH}/${DIR}"
mkdir -p $DST
copy_via_ssh $SRC "$DST" &
# Catch process ids in global variable
JOBS[$i]=$!
i=$((i + 1))
done
}
# Clear individual directories
clear_directories () {
for k in $(seq $DEVICES_FROM $DEVICES_TO); do
DIR=$( get_dir_name $DEVICE )
SRC="${SHARE_PATH}/${DIR}"
if [ -d "$SRC" ]; then
if [ "$(ls -A $SRC)" ]; then
rm -r $SRC/*
fi
fi
done
}
# Create ZIP-file from fetched data
create_zip () {
# create filename suggestion
FILENAME=`date +"%Y-%m-%d_%H-%M-%S"`
# query actual filename
FILENAME=$( query_zip_name ${FILENAME} )
if [[ "$FILENAME" != *.zip ]]; then
FILENAME="${FILENAME}.zip"
fi
# create zip in a subshell to trim unnessacary parts of the paths
(
if [ -d ${FETCH_PATH} ]; then
cd ${FETCH_PATH};
zip -r "${FILENAME}" .
else
notify_error "Beim Erstellen der ZIP-Archivs ist ein Fehler aufgetreten.\n\nDer Ordner ${FETCH_PATH} konnte nicht gelesen werden."
fi
)
}
# ---------------------------------------------------------------------
# --- UI Utilities ----------------------------------------------------
# ---------------------------------------------------------------------
confirm_share_each () {
QUESTION="Die Dateien in in\n\n ${SHARE_PATH}/$( get_dir_name $DEVICES_FROM ) bis …/$( get_dir_name $DEVICES_TO )\n\nwerden ausgeteilt. Dies kann einen Moment dauern. Sie werden benachrichtigt, wenn das Austeilen abgeschlossen ist.\n\nWARNUNG: Die genannten Ausgabe-Ordner werden anschließend VOLLSTÄNDIG GELEERT. Stellen Sie sicher, dass Sie eine KOPIE der Daten besitzen.\n\nFortfahren?"
zenity --question --title="Individuelles Austeilen" --text="$QUESTION" --width=500
echo "$?"
}
confirm_share_all () {
QUESTION="Die Dateien in\n\n ${SHARE_ALL_PATH}\n\nwerden ausgeteilt.\n\n Dies kann einen Moment dauern. Sie werden benachrichtigt, wenn das Austeilen abgeschlossen ist."
zenity --question --title="Einheitliches Austeilen" --text="$QUESTION" --width=500
echo "$?"
}
confirm_fetch () {
QUESTION="Die Dateien der Schüler werden eingesammelt und in\n\n ${FETCH_PATH}/$( get_dir_name $DEVICES_FROM ) bis …/$( get_dir_name $DEVICES_TO )\n\nabgespeichert.\n\nDies kann einen Moment dauern. Sie werden benachrichtigt, wenn das Austeilen abgeschlossen ist."
zenity --question --title="Einsammeln" --text="$QUESTION" --width=500
echo "$?"
}
ask_to_zip () {
QUESTION="Sollen die eingesammelten Dateien als ZIP-Archiv gespeichert werden?"
zenity --question --title="ZIP-Datei erstellen" --width=500 --text="$QUESTION"
echo "$?"
}
notify_success() {
notify-send -i /usr/share/icons/$1 -t 20000 "Abgeschlossen" "Das $2 wurde abgeschlossen."
}
notify_abort () {
notify-send -i /usr/share/icons/$1 "Abgebrochen" "Der Benutzer hat die Aktion abgebrochen"
}
notify_error() {
notify-send -c "im.error" "Fehler" "$1"
}
query_zip_name () {
zenity --file-selection --title="ZIP-Datei erstellen" --filename=$1 --save --confirm-overwrite --file-filter=*.zip
}
progess_bar () {
RUNNING=${#JOBS[@]}
(
while [ $RUNNING -gt 0 ]; do
RUNNING=0
for PID in ${JOBS[@]}; do
if kill -0 "$PID" > /dev/null 2>&1; then
RUNNING=$(($RUNNING + 1))
fi
done
JOBS_FINISHED=$((${#JOBS[@]} - $RUNNING))
PERCENTAGE=$(bc <<< "$JOBS_FINISHED * 100 / ${#JOBS[@]}")
echo "# ${JOBS_FINISHED}/${#JOBS[@]} abgeschlossen (${PERCENTAGE}%)."
echo $PERCENTAGE
sleep 0.5
done
) | zenity --progress --title "${1}-Fortschritt" --text "Warte auf ${1} …" --percentage=0 --auto-close --width=500
}
# ---------------------------------------------------------------------
# --- Main Program ----------------------------------------------------
# ---------------------------------------------------------------------
if [ "$1" == "--share-each" ]; then
ANSWER=$( confirm_share_each )
if [ $ANSWER = "0" ]; then
# UGLY: Working with global variables
# Didn't find a way to return the PIDs from a subprocess without
# breaking the multithreading
share
progess_bar "Upload"
clear_directories
notify_success "up" "individuelle Austeilen"
else
notify_abort "up"
fi
elif [ "$1" == "--share-all" ]; then
ANSWER=$( confirm_share_all )
if [ $ANSWER = "0" ]; then
# UGLY: Working with global variables
# Didn't find a way to return the PIDs from a subprocess without
# breaking the multithreading
share "$SHARE_ALL_PATH"
progess_bar "Upload"
notify_success "up" "einheitliche Austeilen"
else
notify_abort "up"
fi
elif [ "$1" == "--fetch" ]; then
ANSWER=$( confirm_fetch )
if [ $ANSWER = "0" ]; then
# UGLY: Working with global variables
# Didn't find a way to return the PIDs from a subprocess without
# breaking the multithreading
fetch
progess_bar "Download"
notify_success "down" "Einsammeln"
# ask to zip if directory is not empty
if [ $( directory_empty $FETCH_PATH ) = "false" ]; then
ANSWER=$( ask_to_zip )
if [ $ANSWER = "0" ]; then
create_zip
fi
fi
else
notify_abort "down"
fi
else
cat "$(dirname $BASH_SOURCE[0])/USAGE.md"
echo ""
echo "Hardcoded paths:"
echo "----------------"
echo "Local fetch base path : $FETCH_PATH"
echo "Local share base path : $SHARE_PATH"
echo "General share path : $SHARE_ALL_PATH"
echo "Remote exchange path : $EXCHANGE_PATH"
echo ""
fi