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:=/usr/lib/util-vserver/util-vserver-vars}
40 test -e "$UTIL_VSERVER_VARS" || {
41 echo $"Can not find util-vserver installation (the file '$UTIL_VSERVER_VARS' would be expected); aborting..." >&2
44 . "$UTIL_VSERVER_VARS"
51 ### Helper functions ###
53 # Save stdin and stdout for later use
67 ! $quiet && echo "I: $me: $1" >&3
71 ! $quiet && echo "W: $me: $1" >&4
75 ! $quiet && echo "E: $me: $2" >&4
80 ### Usage/Info functions ###
84 Usage: $me [-hVvqidrRs] vserver newname
85 $me [-hVvqidrRs] vserver host:[newname]
93 $me uses rsync to make a copy of a vserver. If the destination
94 name contains a host specification the vserver will be synchronised to
95 the remote destination over ssh/rsh.
97 This can be used on a running vserver to make a warm backup. With the -s
98 flag a vserver can even be operationally moved to different hardware within
101 The -i and -d flags can be used to minimally reconfigure the destination
102 vserver (rewrites /etc/vservers/newname.conf and $__DEFAULT_VSERVERDIR/newname/etc/hosts)
106 -V, --version copyright and version information
107 -v, --verbose show all output
108 -q, --quiet direct all output to /dev/null (no password
109 prompt for logins on remote hosts!)
110 -d, --domain [string] new dns domain (must be used with -i)
111 -i, --ip [addr] new IP address (must be used with -d)
112 -r, --vsroot location of "/vserver/" directory
113 -R, --rsh use rsh (instead of default ssh) for
115 -s, --stopstart stop the local vserver before copying and start
116 it on the destination host afterwards
124 Copyright (c) 2002 Mark Lawrence <nomad@null.net>
126 This program is free software; you can redistribute it and/or modify
127 it under the terms of the GNU General Public License as published by
128 the Free Software Foundation; either version 2 of the License, or (at
129 your option) any later version.
135 ### Default values and Command line options ###
146 vsroot=$__DEFAULT_VSERVERDIR
148 if [ $# -eq 0 ]; then # Script invoked with no command-line args?
153 temp=$(getopt -o hVvqd:i:rRs --long help,version,verbose,quiet,domain:,ip:,vsroot,rsh,stopstart, -n $me -- "$@")
155 if [ $? -ne 0 ]; then
156 echo " (See -h for help)"
160 # Note the quotes around `$temp': they are essential!
165 -h|--help) full_usage
168 -V|--version) full_version
171 -v|--verbose) verbose=(true)
174 -q|--quiet) quiet=(true)
177 -d|--domain) domain="$2"
183 -r|--vsroot) vsroot="$2"
189 -s|--stopstart) stopstart=(true)
195 *) echo "Internal error!"
201 if [ $# -ne 2 ]; then
209 # By default we are reasonably quiet (ouput only via info, warn & error)
217 info "called on $(hostname) at $now"
221 vconf=/etc/vservers/$vserver.conf
222 vroot=$vsroot/$vserver
228 if (echo $2 | grep '^[a-z][a-z0-9]\+$'); then
235 warn "rsh is set but not used for a local copy"
237 elif (echo $2 | grep '^[a-z].*[a-z0-9]:$'); then
240 elif (echo $2 | grep '^[a-z].*[a-z0-9]:[a-z].*[a-z0-9]$'); then
244 error 1 "Second argument must be of the form \"[host:]name\" or \"host:\""
247 target=$vsroot/$newname
248 targetconf=/etc/vservers/$newname.conf
251 ### Perform some sanity checks ###
253 if [ ! -d $vroot ]; then
254 error 1 "Directory \"$vroot\" does not exist"
257 if [ ! -e $vconf ]; then
258 error 1 "Vserver file \"$vconf\" does not exist"
261 if [ -z "$dhost" ] && [ "$vserver" == "$newname" ]; then
262 error 1 "Source and destination names cannot be the same on the localhost"
265 if [ -n "$dhost" ] && ! (host $dhost | grep 'has address'); then
266 warn "$dhost does not resolve into an IP address"
269 if [ \( -n "$ip" -a -z "$domain" \) -o \
270 \( -z "$ip" -a -n "$domain" \) ]
272 error 1 "Both IP address and domain must be specified together"
276 ! (echo $ip | grep '^[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}$' ); then
277 error 1 "\"$ip\" is not a valid IP address"
280 # This works both locally and remote
281 if ($shcmd $dhost $__SBINDIR/vserver $newname running | grep 'is running'); then
282 warn "destination vserver \"$newname\" is running"
283 error 1 "Cannot copy over a running vserver"
289 info "Attempting to copy $vserver to $dhost$colon$newname"
292 info "Stopping virtual server \"$vserver\" on localhost"
293 $__SBINDIR/vserver $vserver stop
296 info "Syncing directories"
297 # trailing slashes very important in the rsync!
298 if ! rsync -Havxz --numeric-ids $rsflag $shcmd $vroot/ $dhost$colon$target/; then
299 error 1 "rsync failed"
302 if [ -n "$ip" -a -n "$domain" ]; then
303 # Insert the new IPROOT/S_HOSTNAME values into the config file
304 info "Modifying $targetconf"
306 if (sed -e "s/^S_HOSTNAME=.*/S_HOSTNAME=\"$newname\"/" \
307 -e "s/^IPROOT=.*/IPROOT=\"$ip\"/" $vconf > $tmpf)
309 if ! rsync -v $rsflag $shcmd $tmpf $dhost$colon$targetconf; then
310 error $? "vserver config file copy/change failed"
314 warn "Unable to reconfigure virtual server config file"
317 # create a new /etc/hostname
318 info "Creating hostname file"
319 echo $newname > $tmpf
320 if ! rsync -v $rsflag $shcmd $tmpf $dhost$colon$target/etc/hostname; then
321 error 1 "vserver /etc/hostname copy failed"
324 info "Creating /etc/hosts"
326 # /etc/hosts (automatically generated by $me)
329 $ip $newname.$domain $newname
331 # The following lines are desirable for IPv6 capable hosts
333 ::1 ip6-localhost ip6-loopback
335 ff00::0 ip6-mcastprefix
337 ff02::2 ip6-allrouters
342 if ! rsync -v $rsflag $shcmd $tmpf $dhost$colon$target/etc/hosts; then
343 error 1 "vserver /etc/hosts copy failed"
348 if ! $stopstart; then
349 # Make sure that this vserver doesn't start on the
350 # destination host if it reboots
352 sed -e 's/^ONBOOT=.*/ONBOOT=no/' $vconf > $tmpf
356 # copy newname.conf unchanged
357 info "Copying $targetconf"
358 if ! rsync -v $rsflag $shcmd $vconf $dhost$colon$targetconf; then
359 error 1 "vserver config file copy/change failed"
367 info "Starting virtual server \"$vserver\" on $dhost"
368 $shcmd $dhost $__SBINDIR/vserver $vserver start
369 if ($shcmd $dhost $__SBINDIR/vserver $vserver running | \
370 grep 'not running'); then
371 error 1 "Virtual server \"$vserver\" failed to start on $dhost"
374 # Make sure that we don't start the original on next boot
376 sed -e 's/^ONBOOT=.*/ONBOOT=no/' $vconf > $tmpf