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.
44 ### Helper functions ###
46 # Save stdin and stdout for later use
60 ! $quiet && echo "I: $me: $1" >&3
64 ! $quiet && echo "W: $me: $1" >&4
68 ! $quiet && echo "E: $me: $2" >&4
73 ### Usage/Info functions ###
77 Usage: $me [-hVvqidrRs] vserver newname
78 $me [-hVvqidrRs] vserver host:[newname]
86 $me uses rsync to make a copy of a vserver. If the destination
87 name contains a host specification the vserver will be synchronised to
88 the remote destination over ssh/rsh.
90 This can be used on a running vserver to make a warm backup. With the -s
91 flag a vserver can even be operationally moved to different hardware within
94 The -i and -d flags can be used to minimally reconfigure the destination
95 vserver (rewrites /etc/vservers/newname.conf and /vservers/newname/etc/hosts)
99 -V, --version copyright and version information
100 -v, --verbose show all output
101 -q, --quiet direct all output to /dev/null (no password
102 prompt for logins on remote hosts!)
103 -d, --domain [string] new dns domain (must be used with -i)
104 -i, --ip [addr] new IP address (must be used with -d)
105 -r, --vsroot location of "/vserver/" directory
106 -R, --rsh use rsh (instead of default ssh) for
108 -s, --stopstart stop the local vserver before copying and start
109 it on the destination host afterwards
117 Copyright (c) 2002 Mark Lawrence <nomad@null.net>
119 This program is free software; you can redistribute it and/or modify
120 it under the terms of the GNU General Public License as published by
121 the Free Software Foundation; either version 2 of the License, or (at
122 your option) any later version.
128 ### Default values and Command line options ###
141 if [ $# -eq 0 ]; then # Script invoked with no command-line args?
146 temp=$(getopt -o hVvqd:i:rRs --long help,version,verbose,quiet,domain:,ip:,vsroot,rsh,stopstart, -n $me -- "$@")
148 if [ $? -ne 0 ]; then
149 echo " (See -h for help)"
153 # Note the quotes around `$temp': they are essential!
158 -h|--help) full_usage
161 -V|--version) full_version
164 -v|--verbose) verbose=(true)
167 -q|--quiet) quiet=(true)
170 -d|--domain) domain="$2"
176 -r|--vsroot) vsroot="$2"
182 -s|--stopstart) stopstart=(true)
188 *) echo "Internal error!"
194 if [ $# -ne 2 ]; then
202 # By default we are reasonably quiet (ouput only via info, warn & error)
210 info "called on $(hostname) at $now"
214 vconf=/etc/vservers/$vserver.conf
215 vroot=$vsroot/$vserver
221 if (echo $2 | grep '^[a-z][a-z0-9]\+$'); then
228 warn "rsh is set but not used for a local copy"
230 elif (echo $2 | grep '^[a-z].*[a-z0-9]:$'); then
233 elif (echo $2 | grep '^[a-z].*[a-z0-9]:[a-z].*[a-z0-9]$'); then
237 error 1 "Second argument must be of the form \"[host:]name\" or \"host:\""
240 target=$vsroot/$newname
241 targetconf=/etc/vservers/$newname.conf
244 ### Perform some sanity checks ###
246 if [ ! -d $vroot ]; then
247 error 1 "Directory \"$vroot\" does not exist"
250 if [ ! -e $vconf ]; then
251 error 1 "Vserver file \"$vconf\" does not exist"
254 if [ -z "$dhost" ] && [ "$vserver" == "$newname" ]; then
255 error 1 "Source and destination names cannot be the same on the localhost"
258 if [ -n "$dhost" ] && ! (host $dhost | grep 'has address'); then
259 warn "$dhost does not resolve into an IP address"
262 if [ \( -n "$ip" -a -z "$domain" \) -o \
263 \( -z "$ip" -a -n "$domain" \) ]
265 error 1 "Both IP address and domain must be specified together"
269 ! (echo $ip | grep '^[0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{3\}$' ); then
270 error 1 "\"$ip\" is not a valid IP address"
273 # This works both locally and remote
274 if ($shcmd $dhost /usr/sbin/vserver $newname running | grep 'is running'); then
275 warn "destination vserver \"$newname\" is running"
276 error 1 "Cannot copy over a running vserver"
282 info "Attempting to copy $vserver to $dhost$colon$newname"
285 info "Stopping virtual server \"$vserver\" on localhost"
286 /usr/sbin/vserver $vserver stop
289 info "Syncing directories"
290 # trailing slashes very important in the rsync!
291 if ! rsync -avxz $rsflag $shcmd $vroot/ $dhost$colon$target/; then
292 error 1 "rsync failed"
295 if [ -n "$ip" -a -n "$domain" ]; then
296 # Insert the new IPROOT/S_HOSTNAME values into the config file
297 info "Modifying $targetconf"
299 if (sed -e "s/^S_HOSTNAME=.*/S_HOSTNAME=\"$newname\"/" \
300 -e "s/^IPROOT=.*/IPROOT=\"$ip\"/" $vconf > $tmpf)
302 if ! rsync -v $rsflag $shcmd $tmpf $dhost$colon$targetconf; then
303 error $? "vserver config file copy/change failed"
307 warn "Unable to reconfigure virtual server config file"
310 # create a new /etc/hostname
311 info "Creating hostname file"
312 echo $newname > $tmpf
313 if ! rsync -v $rsflag $shcmd $tmpf $dhost$colon$target/etc/hostname; then
314 error 1 "vserver /etc/hostname copy failed"
317 info "Creating /etc/hosts"
319 # /etc/hosts (automatically generated by $me)
322 $ip $newname.$domain $newname
324 # The following lines are desirable for IPv6 capable hosts
326 ::1 ip6-localhost ip6-loopback
328 ff00::0 ip6-mcastprefix
330 ff02::2 ip6-allrouters
335 if ! rsync -v $rsflag $shcmd $tmpf $dhost$colon$target/etc/hosts; then
336 error 1 "vserver /etc/hosts copy failed"
341 if ! $stopstart; then
342 # Make sure that this vserver doesn't start on the
343 # destination host if it reboots
345 sed -e 's/^ONBOOT=.*/ONBOOT=no/' $vconf > $tmpf
349 # copy newname.conf unchanged
350 info "Copying $targetconf"
351 if ! rsync -v $rsflag $shcmd $vconf $dhost$colon$targetconf; then
352 error 1 "vserver config file copy/change failed"
360 info "Starting virtual server \"$vserver\" on $dhost"
361 $shcmd $dhost /usr/sbin/vserver $vserver start
362 if ($shcmd $dhost /usr/sbin/vserver $vserver running | \
363 grep 'not running'); then
364 error 1 "Virtual server \"$vserver\" failed to start on $dhost"
367 # Make sure that we don't start the original on next boot
369 sed -e 's/^ONBOOT=.*/ONBOOT=no/' $vconf > $tmpf