File: vserver-copy

package info (click to toggle)
util-vserver 0.30.212-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 6,648 kB
  • ctags: 3,145
  • sloc: ansic: 19,781; sh: 13,258; xml: 1,618; makefile: 364; perl: 85
file content (380 lines) | stat: -rwxr-xr-x 9,116 bytes parent folder | download | duplicates (6)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/bin/bash

# Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
# based on vserver-copy by Mark Lawrence <nomad@null.net>
#  
# 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 2, 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

# Copy/Sync a virtual host from one machine to another
#
# History:
#
# 2003-04-04: Version 0.4 - Mark lawrence
# - Set "ONBOOT=no" in the destination .conf file when --startstop
#   is not used, in case the destination roothost reboots. We don't
#   want two copies of a vserver running at the same time.
#
# 2003-03-04: Version 0.3 - Mark lawrence
# - Changed all checks for [ "" != "$var" ] into [ -n|-z "$var" ]. "" doesn't
#   seem to work for bash on the Sparc architecture.
# - Changed $ssh variable into $shcmd.
#
# 2003-02-23: Version 0.2 - Mark Lawrence
# - Set ONBOOT to "no" in the original .conf file when the "-s" flag is 
#   used so that if/when you reboot the source roothost you don't have the
#   same vserver and IP address running on two machines.

: ${UTIL_VSERVER_VARS:=/usr/lib/util-vserver/util-vserver-vars}
test -e "$UTIL_VSERVER_VARS" || {
    echo $"Can not find util-vserver installation (the file '$UTIL_VSERVER_VARS' would be expected); aborting..." >&2
    exit 1
}
. "$UTIL_VSERVER_VARS"

VERSION="0.4"
umask 022
me=${0##*/}


### Helper functions ###

# Save stdin and stdout for later use
exec 3>&1
exec 4>&2

noninteractive () {
	exec &> /dev/null
}

interactive () {
	exec 1>&3
	exec 2>&4
}

info () {
	! $quiet && echo "I: $me: $1" >&3
}

warn () {
	! $quiet && echo "W: $me: $1" >&4
}

error () {
	! $quiet && echo "E: $me: $2" >&4
	exit $1
}


### Usage/Info functions ###

usage () {
    cat <<EOF 1>&2
Usage:	$me [-hVvqidrRs] vserver newname
	$me [-hVvqidrRs] vserver host:[newname]
EOF
}

full_usage () {
	usage
        cat <<EOF

$me uses rsync to make a copy of a vserver. If the destination
name contains a host specification the vserver will be synchronised to
the remote destination over ssh/rsh.

This can be used on a running vserver to make a warm backup. With the -s
flag a vserver can even be operationally moved to different hardware within
seconds.

The -i and -d flags can be used to minimally reconfigure the destination
vserver (rewrites /etc/vservers/newname.conf and $__DEFAULT_VSERVERDIR/newname/etc/hosts)

Options:
	-h, --help		this help
	-V, --version		copyright and version information
	-v, --verbose		show all output
	-q, --quiet		direct all output to /dev/null (no password
				prompt for logins on remote hosts!)
	-d, --domain [string]	new dns domain (must be used with -i)
	-i, --ip [addr]		new IP address (must be used with -d)
	-r, --vsroot		location of "/vserver/" directory
	-R, --rsh		use rsh (instead of default ssh) for
				network transport
	-s, --stopstart		stop the local vserver before copying and start
				it on the destination host afterwards

EOF
}

full_version () {
    cat <<EOF
$me version $VERSION
Copyright (c) 2002 Mark Lawrence   <nomad@null.net>

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 2 of the License, or (at
your option) any later version.

EOF
}


### Default values and Command line options ###

stopstart=(false)
verbose=(false)
quiet=(false)
shcmd="ssh"
rsflag="-e"
rsh=(false)
colon=":"
domain=""
ip=""
vsroot=$__DEFAULT_VSERVERDIR

if [ $# -eq 0 ]; then  # Script invoked with no command-line args?
	usage
	exit 1
fi  

temp=$(getopt -o hVvqd:i:rRs --long help,version,verbose,quiet,domain:,ip:,vsroot,rsh,stopstart, -n $me -- "$@")

if [ $? -ne 0 ]; then
	echo "	(See -h for help)"
        exit 1
fi

# Note the quotes around `$temp': they are essential!
eval set -- "$temp"

while true; do
        case "$1" in
		-h|--help)	full_usage
				exit 1
				;;
		-V|--version)	full_version
				exit 1
				;;
		-v|--verbose)	verbose=(true)
				shift
				;;
		-q|--quiet)	quiet=(true)
				shift
				;;
		-d|--domain)	domain="$2"
				shift 2
				;;
		-i|--ip)	ip="$2"
				shift 2
				;;
		-r|--vsroot)	vsroot="$2"
				shift 2
				;;
		-R|--rsh)	rsh=(true)
				shift
				;;
		-s|--stopstart)	stopstart=(true)
				shift
				;;
                --)             shift
				break
				;;
		*)		echo "Internal error!"
				exit 1
				;;
	esac
done

if [ $# -ne 2 ]; then
	usage
	exit 1
fi


### ###

# By default we are reasonably quiet (ouput only via info, warn & error)
if $verbose; then
	interactive
else
	noninteractive
fi

now=$(date)
info "called on $(hostname) at $now"


vserver=$1
vconf=/etc/vservers/$vserver.conf
vroot=$vsroot/$vserver

if $rsh; then
	shcmd="rsh"
fi

if (echo $2 | grep '^[a-z][a-z0-9]\+$'); then
	dhost=""
	newname=$2
	shcmd=""
	rsflag=""
	colon=""
	if $rsh; then
		warn "rsh is set but not used for a local copy"
	fi
elif (echo $2 | grep '^[a-z].*[a-z0-9]:$'); then
	dhost=${2/:/}
	newname=$vserver
elif (echo $2 | grep '^[a-z].*[a-z0-9]:[a-z].*[a-z0-9]$'); then
	dhost=${2/:*/}
	newname=${2/*:/}
else
	error 1 "Second argument must be of the form \"[host:]name\" or \"host:\""
fi

target=$vsroot/$newname
targetconf=/etc/vservers/$newname.conf


### Perform some sanity checks ###

if [ ! -d $vroot ]; then
	error 1 "Directory \"$vroot\" does not exist"
fi

if [ ! -e $vconf ]; then
	error 1 "Vserver file \"$vconf\" does not exist"
fi

if [ -z "$dhost" ] && [ "$vserver" == "$newname" ]; then
	error 1 "Source and destination names cannot be the same on the localhost"
fi

if [ -n "$dhost" ] && ! (host $dhost | grep 'has address'); then
	warn "$dhost does not resolve into an IP address"
fi

if [ \( -n "$ip" -a -z "$domain" \) -o \
     \( -z "$ip" -a -n "$domain" \) ]
then
	error 1 "Both IP address and domain must be specified together"
fi

if [ -n "$ip" ] && \
! (echo $ip | grep '^[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}$' ); then
	error 1 "\"$ip\" is not a valid IP address"
fi

# This works both locally and remote
if ($shcmd $dhost $__SBINDIR/vserver $newname running | grep 'is running'); then
	warn "destination vserver \"$newname\" is running" 
	error 1 "Cannot copy over a running vserver"
fi


### Do the copy ###

info "Attempting to copy $vserver to $dhost$colon$newname"

if $stopstart; then
	info "Stopping virtual server \"$vserver\" on localhost"
	$__SBINDIR/vserver $vserver stop
fi

info "Syncing directories"
# trailing slashes very important in the rsync!
if ! rsync -Havxz --numeric-ids $rsflag $shcmd $vroot/ $dhost$colon$target/; then
	error 1 "rsync failed"
fi

if [ -n "$ip" -a -n "$domain" ]; then
	# Insert the new IPROOT/S_HOSTNAME values into the config file
	info "Modifying $targetconf"
	tmpf=$(tempfile)
	if (sed -e "s/^S_HOSTNAME=.*/S_HOSTNAME=\"$newname\"/" \
		-e "s/^IPROOT=.*/IPROOT=\"$ip\"/" $vconf > $tmpf)
	then
		if ! rsync -v $rsflag $shcmd $tmpf $dhost$colon$targetconf; then
			error $? "vserver config file copy/change failed"
		fi

	else
		warn "Unable to reconfigure virtual server config file"
	fi

	# create a new /etc/hostname
	info "Creating hostname file"
	echo $newname > $tmpf
	if ! rsync -v $rsflag $shcmd $tmpf $dhost$colon$target/etc/hostname; then
		error 1 "vserver /etc/hostname copy failed"
	fi

	info "Creating /etc/hosts"
	cat << EOF > $tmpf
# /etc/hosts (automatically generated by $me)

127.0.0.1       localhost
$ip	$newname.$domain	$newname

# The following lines are desirable for IPv6 capable hosts

::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
EOF

	# copy /etc/hosts
	if ! rsync -v $rsflag $shcmd $tmpf $dhost$colon$target/etc/hosts; then
		error 1 "vserver /etc/hosts copy failed"
	fi
	rm -f $tmpf

else
	if ! $stopstart; then
		# Make sure that this vserver doesn't start on the 
		# destination host if it reboots
		tmpf=$(tempfile)
		sed -e 's/^ONBOOT=.*/ONBOOT=no/' $vconf > $tmpf
		vconf=$tmpf
	fi

	# copy newname.conf unchanged
	info "Copying $targetconf"
	if ! rsync -v $rsflag $shcmd $vconf $dhost$colon$targetconf; then
		error 1 "vserver config file copy/change failed"
	fi

	rm -f $tmpf
fi


if $stopstart; then
	info "Starting virtual server \"$vserver\" on $dhost"
	$shcmd $dhost $__SBINDIR/vserver $vserver start
	if ($shcmd $dhost $__SBINDIR/vserver $vserver running | \
	grep 'not running'); then
		error 1 "Virtual server \"$vserver\" failed to start on $dhost"
	fi

	# Make sure that we don't start the original on next boot
	tmpf=$(tempfile)
	sed -e 's/^ONBOOT=.*/ONBOOT=no/' $vconf > $tmpf
	mv $tmpf $vconf
fi

exit 0