Repository navigation
Expand file tree
/
Copy pathrsync
More file actions
executable file
·137 lines (124 loc) · 2.82 KB
/
Copy pathrsync
File metadata and controls
executable file
·137 lines (124 loc) · 2.82 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
#!/bin/sh
#
# Copyright 2026 Samvel Khalatyan. All rights reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.
# Rsync pulls cluster configurations.
#
# SYNOPSIS
#
# ./rsync [-h|-n] path
#
# DESCRIPTION
#
# Rsync pulls configuration files for each node that has `rsync.files-from`
# file, with a list of files to pull. The last component of the path's dirname
# identifies the host starting with top-level domain, e.g. nat.lab.bsd.
#
# OPTIONS
#
# -h print help and exit
# -n dry-run, print commands but do not run them
NOOP=false
# usage prints help message.
usage() {
local cmd=${0##*/}
cat <<EOF
usage: ${cmd} [options] [path]
options:
-h print help and exit
-n dry-run, print commands but do not run them
arguments:
path path to search for rsync.files-from
EOF
}
# run executes the command using exec. If NOOP environment variable is set
# to true, then it prints the command to be executed but does not run it.
run() {
if command -p "${NOOP}"; then
echo ".. $@"
return
fi
eval "$@"
}
# handle_rsync_files_from handles a file rsync.files-from. It expects the file
# to be located in a folder, named after the cluster node, e.g.
# .../net.lab.dns/rsync.files-from
handle_rsync_files_from() {
local file=${1:?'handle_rsync_files_from: missing file'}
local path=${file%/*}
local remote=${path##*/}
local jid src
case "${remote}" in
# keep-sorted start
"laptop") # macOS
case $(hostname) in
*.lab.net)
echo >&2 "skip ${remote} -- running in Lab cluster"
return
;;
esac
src=/
;;
"net.lab.nuc") # server
src=op@nuc.lab.net:/
;;
# keep-sorted end
*) # jail
jid=${remote##*.}
src=op@nuc.lab.net:/usr/jail/${jid}/
;;
esac
echo "-- rsync ${remote}"
run rsync -arvz --files-from="${file}" "${src}" ${path}/
}
# handle_path searches for rsync.files-from at path and runs rsync if the
# file is found.
handle_path() {
local path=${1:?'handle_path: missing path'}
find "${path}" -type f -name rsync.files-from -maxdepth 2 \
| sort \
| {
while read f; do
handle_rsync_files_from "$f"
done
}
}
# main handles command options and runs path handler for every provided path
# or ./ if no path is set.
main() {
local opt
while getopts "hn" opt; do
case "$opt" in
h)
usage
exit 1
;;
n)
NOOP=true
export NOOP
;;
*)
usage
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [ $# -eq 0 ]; then
set -- "$PWD"
fi
local p seen=""
for p; do
case "$seen" in
*":$p:"*)
echo >&2 "skip duplicate path $p"
continue
;;
esac
seen=":$p:$seen"
handle_path "$p"
done
}
main "$@"