3 # Copyright (C) 2003 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
4 # based on vserver-copy by Mark Lawrence <nomad@null.net>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 # Copy/Sync a virtual host from one machine to another
24 # 2003-04-04: Version 0.4 - Mark lawrence
25 # - Set "ONBOOT=no" in the destination .conf file when --startstop
26 # is not used, in case the destination roothost reboots. We don't
27 # want two copies of a vserver running at the same time.
29 # 2003-03-04: Version 0.3 - Mark lawrence
30 # - Changed all checks for [ "" != "$var" ] into [ -n|-z "$var" ]. "" doesn't
31 # seem to work for bash on the Sparc architecture.
32 # - Changed $ssh variable into $shcmd.
34 # 2003-02-23: Version 0.2 - Mark Lawrence
35 # - Set ONBOOT to "no" in the original .conf file when the "-s" flag is
36 # used so that if/when you reboot the source roothost you don't have the
37 # same vserver and IP address running on two machines.
39 : ${UTIL_VSERVER_VARS:=$(dirname $0)/util-vserver-vars}
40 test -e "$UTIL_VSERVER_VARS" || {
41 echo "Can not find util-vserver installation; aborting..."
44 . "$UTIL_VSERVER_VARS"
50 mktemp=$(which mktemp 2>/dev/null) || mktemp=$(which tempfile 2>/dev/null) || {
51 echo $"Can not find mktemp or tempfile" >&2
55 ### Helper functions ###
57 # Save stdin and stdout for later use
71 ! $quiet && echo "I: $me: $1" >&3
75 ! $quiet && echo "W: $me: $1" >&4
79 ! $quiet && echo "E: $me: $2" >&4
84 ### Usage/Info functions ###
88 Usage: $me [-hVvqidrRs] vserver newname
89 $me [-hVvqidrRs] vserver host:[newname]
97 $me uses rsync to make a copy of a vserver. If the destination
98 name contains a host specification the vserver will be synchronised to
99 the remote destination over ssh/rsh.
101 This can be used on a running vserver to make a warm backup. With the -s
102 flag a vserver can even be operationally moved to different hardware within
105 The -i and -d flags can be used to minimally reconfigure the destination
106 vserver (rewrites /etc/vservers/newname.conf and $VROOTDIR/newname/etc/hosts)
110 -V, --version copyright and version information
111 -v, --verbose show all output
112 -q, --quiet direct all output to /dev/null (no password
113 prompt for logins on remote hosts!)
114 -d, --domain [string] new dns domain (must be used with -i)
115 -i, --ip [addr] new IP address (must be used with -d)
116 -r, --vsroot location of "/vserver/" directory
117 -R, --rsh use rsh (instead of default ssh) for
119 -s, --stopstart stop the local vserver before copying and start
120 it on the destination host afterwards
128 Copyright (c) 2002 Mark Lawrence <nomad@null.net>
130 This program is free software; you can redistribute it and/or modify
131 it under the terms of the GNU General Public License as published by
132 the Free Software Foundation; either version 2 of the License, or (at
133 your option) any later version.
139 ### Default values and Command line options ###
152 if [ $# -eq 0 ]; then # Script invoked with no command-line args?
157 temp=$(getopt -o hVvqd:i:rRs --long help,version,verbose,quiet,domain:,ip:,vsroot,rsh,stopstart, -n $me -- "$@")
159 if [ $? -ne 0 ]; then
160 echo " (See -h for help)"
164 # Note the quotes around `$temp': they are essential!
169 -h|--help) full_usage
172 -V|--version) full_version
175 -v|--verbose) verbose=(true)
178 -q|--quiet) quiet=(true)
181 -d|--domain) domain="$2"
187 -r|--vsroot) vsroot="$2"
193 -s|--stopstart) stopstart=(true)
199 *) echo "Internal error!"
205 if [ $# -ne 2 ]; then
213 # By default we are reasonably quiet (ouput only via info, warn & error)
221 info "called on $(hostname) at $now"
225 vconf=/etc/vservers/$vserver.conf
226 vroot=$vsroot/$vserver
232 if (echo $2 | grep '^[a-z][a-z0-9]\+$'); then
239 warn "rsh is set but not used for a local copy"
241 elif (echo $2 | grep '^[a-z].*[a-z0-9]:$'); then
244 elif (echo $2 | grep '^[a-z].*[a-z0-9]:[a-z].*[a-z0-9]$'); then
248 error 1 "Second argument must be of the form \"[host:]name\" or \"host:\""
251 target=$vsroot/$newname
252 targetconf=/etc/vservers/$newname.conf
255 ### Perform some sanity checks ###
257 if [ ! -d $vroot ]; then
258 error 1 "Directory \"$vroot\" does not exist"
261 if [ ! -e $vconf ]; then
262 error 1 "Vserver file \"$vconf\" does not exist"
265 if [ -z "$dhost" ] && [ "$vserver" == "$newname" ]; then
266 error 1 "Source and destination names cannot be the same on the localhost"
269 if [ -n "$dhost" ] && ! (host $dhost | grep 'has address'); then
270 warn "$dhost does not resolve into an IP address"
273 if [ \( -n "$ip" -a -z "$domain" \) -o \
274 \( -z "$ip" -a -n "$domain" \) ]
276 error 1 "Both IP address and domain must be specified together"
280 ! (echo $ip | grep '^[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}$' ); then
281 error 1 "\"$ip\" is not a valid IP address"
284 # This works both locally and remote
285 if ($shcmd $dhost $SBINDIR/vserver $newname running | grep 'is running'); then
286 warn "destination vserver \"$newname\" is running"
287 error 1 "Cannot copy over a running vserver"
293 info "Attempting to copy $vserver to $dhost$colon$newname"
296 info "Stopping virtual server \"$vserver\" on localhost"
297 $SBINDIR/vserver $vserver stop
300 info "Syncing directories"
301 # trailing slashes very important in the rsync!
302 if ! rsync -avxz $rsflag $shcmd $vroot/ $dhost$colon$target/; then
303 error 1 "rsync failed"
306 if [ -n "$ip" -a -n "$domain" ]; then
307 # Insert the new IPROOT/S_HOSTNAME values into the config file
308 info "Modifying $targetconf"
310 if (sed -e "s/^S_HOSTNAME=.*/S_HOSTNAME=\"$newname\"/" \
311 -e "s/^IPROOT=.*/IPROOT=\"$ip\"/" $vconf > $tmpf)
313 if ! rsync -v $rsflag $shcmd $tmpf $dhost$colon$targetconf; then
314 error $? "vserver config file copy/change failed"
318 warn "Unable to reconfigure virtual server config file"
321 # create a new /etc/hostname
322 info "Creating hostname file"
323 echo $newname > $tmpf
324 if ! rsync -v $rsflag $shcmd $tmpf $dhost$colon$target/etc/hostname; then
325 error 1 "vserver /etc/hostname copy failed"
328 info "Creating /etc/hosts"
330 # /etc/hosts (automatically generated by $me)
333 $ip $newname.$domain $newname
335 # The following lines are desirable for IPv6 capable hosts
337 ::1 ip6-localhost ip6-loopback
339 ff00::0 ip6-mcastprefix
341 ff02::2 ip6-allrouters
346 if ! rsync -v $rsflag $shcmd $tmpf $dhost$colon$target/etc/hosts; then
347 error 1 "vserver /etc/hosts copy failed"
352 if ! $stopstart; then
353 # Make sure that this vserver doesn't start on the
354 # destination host if it reboots
356 sed -e 's/^ONBOOT=.*/ONBOOT=no/' $vconf > $tmpf
360 # copy newname.conf unchanged
361 info "Copying $targetconf"
362 if ! rsync -v $rsflag $shcmd $vconf $dhost$colon$targetconf; then
363 error 1 "vserver config file copy/change failed"
371 info "Starting virtual server \"$vserver\" on $dhost"
372 $shcmd $dhost $SBINDIR/vserver $vserver start
373 if ($shcmd $dhost $SBINDIR/vserver $vserver running | \
374 grep 'not running'); then
375 error 1 "Virtual server \"$vserver\" failed to start on $dhost"
378 # Make sure that we don't start the original on next boot
380 sed -e 's/^ONBOOT=.*/ONBOOT=no/' $vconf > $tmpf