-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsync-instance
executable file
·143 lines (119 loc) · 3.03 KB
/
rsync-instance
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
#!/usr/bin/env bash
#
# Copyright 2013 Victor Penso
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# exit if an uninitialised variable is used
#set -o nonunset
# exit of statement returns a non-true return value
#set -o errexit
VERSION=0.2
# Filename of this script
SCRIPT=${0##*/}
# Help text for this script
HELP=\
"Usage:
$SCRIPT [-DhRr] [[user@hostname]:]src [[user@hostname]:]dest
Sync files with remote nodes. If a file called *ssh_config* is
present in the working directory it will be automatically used
if no remote hostname is defined. This script will add the
hostname *instance* to the source or destination path prefixed
with colon (:).
Options:
-D,--debug
Print debug output.
-h,--help
Show this information.
-R,--relative
Use relative paths.
-r, --root
Use the root account for login."
# enable line numbers for debug output
if [ "$_DEBUG" = "true" ] ; then
export PS4='(${BASH_SOURCE}:${LINENO}):${FUNCNAME[0]}-[${SHLVL},${BASH_SUBSHELL},$?] '
fi
function _debug() {
if [ "$_DEBUG" = "true" ]; then
echo 1>&2 "Debug: $@"
fi
}
function _error() {
echo 1>&2 "Error: $@"
echo "See \`$SCRIPT --help\` for usage."
}
user=
# Parse the command line options
ARGS=$(getopt -o DhRrV -l "help,debug,relative,root,version" -- "$@")
eval set -- "$ARGS"
while true; do
case "$1" in
-h|--help)
echo "$HELP"
exit 0
;;
-D|--debug)
_DEBUG=true
shift
;;
-R|--relative)
relative_path='--relative'
shift
;;
-r|--root)
user='-l root'
shift
;;
-V|--version)
echo $VERSION
exit 0
;;
--) shift; break ;;
*) break ;;
esac
done
if [ -e "$PWD/ssh_config" ]
then
RSYNC_RSH="ssh -q -F $PWD/ssh_config"
_debug "Using SSH configuration in ssh_config"
_debug "RSYNC_RSH=$RSYNC_RSH"
fi
RSYNC_RSH="$RSYNC_RSH $user"
paths=
for var in "$@"
do
(echo "$var" | grep '^:' >/dev/null)
if [ $? -eq 0 ]
then
paths="$paths instance$var "
export RSYNC_RSH
else
paths="$paths $var "
fi
done
# Ignore version control system directories
options="$options --exclude '.git' --exclude '.gitignore' --exclude '.svn'"
# Copy options...
options="$options $relative_path --omit-dir-times --recursive --copy-links --copy-dirlinks --delete"
# Verbose
options="$options --verbose"
command="$options $paths"
_debug "[sync] rsync$command"
rsync $command
if [ $? -ne 0 ]
then
_error "Couldn't rsync to $dst"
fi
unset RSYNC_RSH
exit 0