-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync
More file actions
executable file
·307 lines (280 loc) · 10.6 KB
/
Copy pathsync
File metadata and controls
executable file
·307 lines (280 loc) · 10.6 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
#!/bin/bash
SCRIPT_VERSION=0.7.1
SCRIPT_MODIFIED=2026-05-20
SCRIPT_UUID=60dffac9-3444-41bb-bdf6-c9c63cdee89d
SCRIPT_PATH="$0"
[[ -L "$SCRIPT_PATH" ]] && SCRIPT_PATH="$(readlink --canonicalize "$SCRIPT_PATH")" # Allow linking, but find original for dependencies and tasks. FIXME: Assuming GNU readlink.
SCRIPT_NAME="$(basename "$SCRIPT_PATH")"
SCRIPT_DIR="$(dirname "$SCRIPT_PATH")"
PATH_SOURCE_READ_CONF="$SCRIPT_DIR/read-config.sh"
#SYNC_CONF_FILENAME='.sync.conf'
SYNC_CONF_PATTERN='.sync*.conf'
oIFS="$IFS"
[[ -z $HOSTNAME ]] && command -v hostname &>/dev/null && export HOSTNAME="$(hostname)"
shopt -s extglob
opt_assumeyes=0
opt_base=()
opt_dryrun=FALSE
opt_postprocess=TRUE
opt_showhelp=0
opt_verbose=0
userContexts=()
userContexts+=("host.$HOSTNAME")
isdry() { [[ ${opt_dryrun} == TRUE ]]; }
loglevel() { [[ ${opt_verbose} -ge $1 ]]; }
feedback() { loglevel $1 || return; shift; >&2 echo "$@"; }
showHelp()
{
echo "SYNOPSIS: Copies files from wherever a ${SYNC_CONF_PATTERN} file is found."
echo
echo "USAGE: sync [-h|--help] [-n|--dry-run] [-y|--no-prompt] [-c CONTEXT] [-v[v…]|--verbose|-q|--quiet] [--CONTEXT…] [PATH…]"
echo
echo "PATH arguments:"$'\t'"directory to search for ${SYNC_CONF_PATTERN} files."
echo $'\t\t'"Defaults to \$PWD."
echo
echo "CONFIG (see shell-utilities/read-config.sh):"
echo " destination=PATH"
echo $'\t'"# Empty PATH or '.' refers to source directory."
echo " subdir=DATEFORMAT"
echo $'\t'"# Ignored if destination is empty."
echo " exclude=PATTERN or ('PATTERN'…)"
echo $'\t'"# Applies before include (*regardless of CONFIG line order)."
echo " include=PATTERN or ('PATTERN'…)"
echo $'\t'"# Applies after exclude (^*)."
echo " keepcopy=true|false"
echo $'\t'"# Defaults to true."
echo " postprocess=COMMAND"
echo $'\t'"# Run COMMAND in the config directory."
echo $'\t'"# Runs after all sync operations."
echo $'\t'"# \$1 is the destination directory, or the source directory if no destination is defined."
echo " [CONTEXT]"
echo $'\t'"# Begin conditional section, per -c CONTEXT."
echo
echo "NOTE: Only one value is assigned per key in the CONFIG. Multiples overwrite earlier values."
echo " Some keys (include, exclude) are evaluated to support arrays, like key=('a' 'b')."
echo
echo "OPTIONS:"
echo " -c CONTEXT, --CONTEXT"$'\t'"Add CONTEXT for read-config. Can be used multiple times."
echo $'\t\t\t'"CONTEXT of \`[host.\$HOSTNAME]\` is provided automatically."
echo " -h,--help"$'\t\t'"Show this help."
echo " -n,--dry-run"$'\t\t'"Make no changes, move no files. Finds ${SYNC_CONF_PATTERN} files,"
echo $'\t\t\t'"and prints commands. Implies -y."
echo " -y,--no-prompt"$'\t'"Begin syncing without prompting the user first."
echo " -P,--no-post"$'\t\t'"Skip any configured postprocessing steps."
echo " -v,--verbose"$'\t\t'"Show more info. Can be used multiple times."
echo " -q,--quiet"$'\t\t'"Suppress most output."
local todoPath="${SCRIPT_DIR}/todo.${SCRIPT_NAME}.txt"
if loglevel 1 && [[ -s "$todoPath" ]]
then
echo
echo "TODO (${todoPath}):"
cat "$todoPath" \
| sed 's/^/ /'
fi
echo
echo "${SCRIPT_NAME} version ${SCRIPT_VERSION} (${SCRIPT_MODIFIED})"
}
while [[ $# -gt 0 ]]
do
case $1 in
-h|--help)
opt_showhelp=TRUE
;;
-n|--dry-run)
opt_dryrun=TRUE
opt_assumeyes=1
;;
-+(v))
let "opt_verbose += ${#1} - 1"
;;
--verbose)
let opt_verbose++
;;
-y|--no-prompt)
opt_assumeyes=1
;;
-q|--quiet)
opt_verbose=-1
;;
-c)
userContexts+=("$2")
shift
;;
-P|--no-post)
opt_postprocess=FALSE
;;
-d) # sunset 2026-03-13/2026-06
opt_base+=("$2")
shift
;;
--*)
userContexts+=("${1#--}")
;;
*)
opt_base+=("$1")
;;
esac
shift
done
feedback 1 "# [${SCRIPT_NAME}] Log level: ${opt_verbose}"
if [[ ${opt_showhelp} == TRUE ]]
then
showHelp
exit
else
feedback 1 "# [${SCRIPT_NAME}] version: ${SCRIPT_VERSION}"
fi
if isdry
then
feedback 0 "# [${SCRIPT_NAME}] DRY RUN: no changes to files; find only"
fi
baseFindArgs=()
baseFindArgs+=("${opt_base[@]}" -name "$SYNC_CONF_PATTERN")
feedback 0 "Finding sync config files:"
feedback 2 "# [${SCRIPT_NAME}] $ find ${baseFindArgs[*]} -print0"
configPaths=()
while IFS= read -r -d $'\0' path <&3
do
configPaths+=("$path")
feedback 0 " $path"
if loglevel 1
then
cat "$path" | sed 's/^/ │ /' # U=2502 Box Drawings Light Vertical
feedback 1
fi
done 3< <(find "${baseFindArgs[@]}" -print0)
baseFindStatus=$?
IFS="$oIFS"
if [[ $baseFindStatus -ne 0 || ${#configPaths[@]} -eq 0 ]]
then
feedback 0 "Failed to find any sync config files (status ${baseFindStatus}). Aborting."
exit
fi
# TODO Parse config before prompting, not only ^ displaying config text. todo.sync.txt#8b304e92
if [[ ${opt_assumeyes} -le 0 ]]
then
read -p "Proceed to sync these directories? (Y/n): " \
&& [[ -z $REPLY || $REPLY =~ ^[Yy] ]] \
|| exit
fi
feedback 0 -E "# [${SCRIPT_NAME}] BEGIN $(date --iso-8601=seconds)"
export SCRIPT_NAME
export PATH_SOURCE_READ_CONF
syncCommand=''
#syncCommand+="SCRIPT_NAME=\"${SCRIPT_NAME}\"; "
loglevel 2 && syncCommand+='declare -p PWD; '
syncCommand+=". \"\$PATH_SOURCE_READ_CONF\" || exit; "
syncCommand+='read-config '
loglevel 2 && syncCommand+='-v '
syncCommand+='-u '
for context in "${userContexts[@]}"
do
syncCommand+="-c \"${context}\" "
done
syncCommand+='-f {}; '
syncCommand+='destination="${config[destination]}"; '
syncCommand+='[[ -n ${config[subdir]} && -n $destination ]] && destination+="/$(date "+${config[subdir]}")"; '
syncCommand+='destination="${destination%%/}"; '
syncCommand+='if [[ -z $destination ]]; then >&2 echo "Empty destination. Skipping file transfer."; exit; fi; '
syncCommand+='if [[ $PWD == $destination ]]; then >&2 echo "Source and destination are identical. Skipping file transfer."; exit; fi; '
! isdry && syncCommand+='mkdir -p "$destination"; '
syncCommand+='rsyncArgs=(); '
isdry && syncCommand+='rsyncArgs+=(--dry-run); '
loglevel 0 && syncCommand+='rsyncArgs+=(--info=progress2); '
loglevel 1 && syncCommand+='rsyncArgs+=(-v); '
loglevel 2 && syncCommand+='rsyncArgs+=(--debug=FILTER); '
syncCommand+='rsyncArgs+=(-r); '
syncCommand+="rsyncArgs+=(--exclude=\"${SYNC_CONF_PATTERN}\"); "
syncCommand+='if [[ -n ${config[include]} ]]; then '
syncCommand+='includePatterns=(); '
syncCommand+="if [[ \${config[include]} =~ [\\\"\\'()] ]]; "
syncCommand+='then '
syncCommand+='eval "includePatterns+=${config[include]}"; '
syncCommand+='else '
syncCommand+='includePatterns+=("${config[include]}"); '
syncCommand+='fi; '
loglevel 2 && syncCommand+='>&2 echo "# [${SCRIPT_NAME}] includePatterns(${#includePatterns[@]}): ${includePatterns[*]}"; '
syncCommand+='for pattern in "${includePatterns[@]}"; '
syncCommand+='do '
syncCommand+='rsyncArgs+=(--include="${pattern}"); '
syncCommand+='done; '
#syncCommand+='if [[ ${#includePatterns[@]} -gt 0 ]]; then '
# syncCommand+='rsyncArgs+=(--exclude="*"); '
#syncCommand+='fi '
syncCommand+='fi; '
syncCommand+='if [[ -n ${config[exclude]} ]]; then '
syncCommand+='excludePatterns=(); '
syncCommand+="if [[ \${config[exclude]} =~ [\\\"\\'()] ]]; "
syncCommand+='then '
syncCommand+='eval "excludePatterns+=${config[exclude]}"; '
syncCommand+='else '
syncCommand+='excludePatterns+=("${config[exclude]}"); '
syncCommand+='fi; '
loglevel 2 && syncCommand+='>&2 echo "# [${SCRIPT_NAME}] excludePatterns(${#excludePatterns[@]}): ${excludePatterns[*]}"; '
syncCommand+='for pattern in "${excludePatterns[@]}"; '
syncCommand+='do '
syncCommand+='rsyncArgs+=(--exclude="${pattern}"); '
syncCommand+='done; '
syncCommand+='fi; '
syncCommand+='[[ ${config[keepcopy]} == false ]] && rsyncArgs+=(--remove-source-files); '
syncCommand+='rsyncArgs+=("$PWD/" "$destination/"); '
loglevel 1 && syncCommand+='>&2 echo "# [${SCRIPT_NAME}] \$ rsync ${rsyncArgs[*]}"; ';
syncCommand+='rsync "${rsyncArgs[@]}"; '
feedback 0
feedback 0 "# [${SCRIPT_NAME}] PHASE: sync"
# Higher loglevel because syncCommand prints rsync command if loglevel 1.
feedback 2 -E "# [${SCRIPT_NAME}] syncCommand: ${syncCommand}"
feedback 2 # newline for legibility
syncFindArgs=()
syncFindArgs+=("${baseFindArgs[@]}")
loglevel 0 && syncFindArgs+=(-print)
syncFindArgs+=(-execdir bash -c "$syncCommand" \;) # NOTE sync will be no-op if isdry
find "${syncFindArgs[@]}"
if [[ ${opt_postprocess} == TRUE ]]
then
#postprocessCommand+="SCRIPT_NAME=\"${SCRIPT_NAME}\"; "
loglevel 2 && postprocessCommand+='declare -p PWD; '
postprocessCommand+=". \"\$PATH_SOURCE_READ_CONF\" || exit; "
postprocessCommand+='read-config '
loglevel 2 && postprocessCommand+='-v '
postprocessCommand+='-u '
for context in "${userContexts[@]}"
do
postprocessCommand+="-c \"${context}\" "
done
postprocessCommand+='-f {}; '
postprocessCommand+='destination="${config[destination]}"; '
postprocessCommand+='[[ -n ${config[subdir]} && -n $destination ]] && destination+="/$(date "+${config[subdir]}")"; '
postprocessCommand+='destination="${destination%%/}"; '
postprocessCommand+='if [[ -z $destination ]]; then >&2 echo "Empty destination. Assuming source dir \"$PWD\"."; destination="$PWD"; fi; '
postprocessCommand+='[[ -n ${config[postprocess]} ]] && { '
postprocessCommand+='eval "postprocess() { ${config[postprocess]}'$'\n''}" && type postprocess '
! loglevel 1 && postprocessCommand+='&>/dev/null '
if isdry
then
postprocessCommand+='; '
else
postprocessCommand+='&& '
# LATER `destination` as arg $1 is going away 2026-06~. User commands should use . or $PWD instead, since `find -execdir` is being used. #626ed8d8-2cbd-42d1-8a65-4a430c04af2f
postprocessCommand+='if [[ ${config[background]} == true ]]; then '
postprocessCommand+='postprocess "$destination" & '
postprocessCommand+='else '
postprocessCommand+='postprocess "$destination"; '
postprocessCommand+='fi; '
fi
postprocessCommand+='}'
feedback 0
feedback 0 "# [${SCRIPT_NAME}] PHASE: post process"
feedback 2 -E "# [${SCRIPT_NAME}] postprocessCommand: ${postprocessCommand}"
feedback 2 # newline for legibility
postProcessFindArgs=()
postProcessFindArgs+=("${baseFindArgs[@]}")
postProcessFindArgs+=(-exec grep -q '^\s*postprocess=' {} \;)
postProcessFindArgs+=(-execdir bash -c "$postprocessCommand" \;) # NOTE postprocess will be no-op if isdry
loglevel 0 && postProcessFindArgs+=(-print)
find "${postProcessFindArgs[@]}"
else
feedback 0 "# [${SCRIPT_NAME}] Skipping postprocess"
fi
feedback 0
feedback 0 -E "# [${SCRIPT_NAME}] END $(date --iso-8601=seconds)"