-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrsyncr
More file actions
executable file
·87 lines (73 loc) · 2.18 KB
/
Copy pathrsyncr
File metadata and controls
executable file
·87 lines (73 loc) · 2.18 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
#!/bin/bash
# A cron job to synchronise filesystems between multiple nodes.
# Soft link this script under /etc/cron.{daily,hourly,weekly} as appropriate.
set -eo pipefail
shopt -s nullglob
err_report() {
echo "errexit on line $(caller)" >&2
}
trap err_report ERR
CONFIG=/etc/rsyncr.conf
YQ=$(which yq || echo "/usr/local/bin/yq")
if [[ ! -f /etc/rsyncr.conf ]]; then
echo "No configuration"
exit 0
fi
# If another rsyncr job is running, complain and crash out.
if ps ax|grep '[l]ogger -t rsyncr' >/dev/null; then
echo "An rsyncr process is already running; aborting!"
exit 1
fi
# Only run if we are the designated master. This allows us to sync our own
# configuration.
master=$($YQ -r ".master" < $CONFIG)
[[ $master && "$master" == $(hostname --fqdn) ]] || exit 0
nodes=($($YQ -r ".nodes + .slaves|.[]" < $CONFIG))
includes=($($YQ -r ".includes|.[]" < $CONFIG))
excludes=($($YQ -r ".excludes|.[]?" < $CONFIG))
options=$($YQ -r ".options|.[]?" < $CONFIG)
sshkey=$($YQ -r ".sshkey" < $CONFIG)
include_list=()
for include in "${includes[@]}"; do
case "$include" in
"")
# Strip empty strings, as rsync maps these to "/" (!!!)
;;
*)
include_list=("${include_list[@]}" "$include")
;;
esac
done
if [[ ! ${include_list[0]:-} ]]; then
echo "Must include at least one source"
exit 2
fi
exclude_flags=()
for exclude in "${excludes[@]}"; do
exclude_flags=("${exclude_flags[@]}" "--exclude" "$exclude")
done
option_flags=()
for option in "${options[@]}"; do
case "$option" in
"delete")
option_flags=("${exclude_flags[@]}" "--$option")
;;
"")
# If yq finds no options, it returns the empty string; ignore this
;;
*)
echo "Unsupported option '$option'; aborting"
exit 99
;;
esac
done
# Also sync our own configuration, including ssh keys
include_list=("${include_list[@]}" "$CONFIG" "$sshkey" "$sshkey.pub")
# fire in the hole
cd /
for node in "${nodes[@]}"; do
[[ $node && $node != $master ]] || continue
rsync -a --relative -e "ssh -i $sshkey" "${option_flags[@]}" \
"${exclude_flags[@]}" "${include_list[@]}" \
${node}:/ 2>&1 | logger -t rsyncr
done