-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer_once.sh
executable file
·142 lines (108 loc) · 3.46 KB
/
transfer_once.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
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
#!/bin/bash
# Transfer Once
# Version: 0.2.1
set -e
NEXPECTED_ARGS=2
SED_ESCAPES=(
'-e s/\[/\\[/g' # Escape left brackets
'-e s/\*/\\*/g' # Escape asterisks
'-e s/\?/\\?/g' # Escape question marks
)
TRANSFERRED_FILEPATH="./transferred"
USAGE="$(basename "$0") [-h] [-v] <source> <destination>
-h display this help and exit
-v verbose
-t specify transferred file
Transfer files, only once, even if the destination changes."
function clean_up_transferred_list {
if [ "$verbose" = true ]; then
echo "Cleaning up transferred file list"
fi
transferred_filepath="$1"
if [ -f "$transferred_filepath" ]; then
remove_directories_from_list "$1"
remove_duplicates_from_list "$1"
else
echo "Transferred file path doesn't exist"
exit 1
fi
}
function remove_directories_from_list {
if [ "$verbose" = true ]; then
echo "Removing directories from list"
fi
filepath="$1"
grep -v "\/$" "$filepath" \
> "$filepath"_intermediate \
|| [ $? -eq 1 ] # Prevent error if no match found
mv "${filepath}_intermediate" "$filepath"
}
function remove_duplicates_from_list {
if [ "$verbose" = true ]; then
echo "Removing duplicates from list"
fi
filepath="$1"
sort -u "$filepath" \
> "$filepath"_intermediate
mv "${filepath}_intermediate" "$filepath"
}
verbose=false
while getopts 't:vh' OPTION; do
case "$OPTION" in
h)
echo "$USAGE"
exit 0
;;
t)
TRANSFERRED_FILEPATH="$OPTARG"
;;
v)
verbose=true
;;
?)
echo "$USAGE"
exit 1
;;
esac
done
shift "$(($OPTIND -1))"
if [ ! $# -eq $NEXPECTED_ARGS ]; then
echo "Expect $NEXPECTED_ARGS args, received $# args"
echo "$USAGE"
exit 1
fi
source_dir="$1"
destination_dir="$2"
echo "Running transfer_once"
if [ "$verbose" = true ]; then
echo "source_dir: \"$source_dir\""
echo "destination_dir: \"$destination_dir\""
echo "transferred_filepath: \"$TRANSFERRED_FILEPATH\""
echo "\n"
fi
lock_filepath="$HOME/.transfer_once.lock"
touch "$TRANSFERRED_FILEPATH"
echo "Running rsync"
flock --nonblock $lock_filepath \
rsync \
--prune-empty-dirs \
--itemize-changes \
--archive \
--compress \
--human-readable \
--out-format="%n" \
--exclude-from="$TRANSFERRED_FILEPATH" \
-- \
"$source_dir/" \
"$destination_dir/" \
| sed "${SED_ESCAPES[@]}" \
| tee -a "$TRANSFERRED_FILEPATH"
rsync_exit_status=${PIPESTATUS[0]}
if [ "$rsync_exit_status" -ne "0" ]; then
echo "rsync exited with $rsync_exit_status"
exit $rsync_exit_status
fi
clean_up_transferred_list "$TRANSFERRED_FILEPATH"
if [ "$verbose" = true ]; then
echo "$0 done"
fi