[Users] Re: vzmigrate modifications

Aleksandar Ivanisevic aleksandar at ivanisevic.de
Tue Jul 20 09:29:18 EDT 2010


Ismael Zarco <isma at galiciadigital.com>
writes:

> Hi,
> I'll be running 1 VPS on each server normally.
> I'd like to be able to replicate each VPS to its backup server(s). For
> example:
>
>    * SERVER1 = VPS1 (VPS2's backup server)
>    * SERVER2 = VPS2 (VPS1's backup server)
>
> I thought to user a modified vzmigrate script for mantain a copy of
> VPS2 (in the case of SERVER 1). If SERVER 2 fails, i can get up the
> VPS 2 quickly.
>
> What modifications should i do in the vzmigrate?

No need to modify vzmigrate, just rsync the private area and the
config file. I wrote a script couple of days ago to do just that. It
even has support for multiple servers, they will all sync from the one
which is currently running the VE.

for this to work you will need rsync set up and two modules defined,
one for private and another for conf. Or you can rsync via ssh which
is slower. Comments welcome.


sample rsync.conf

[vzprivate]
path      = /vz/private
read only = yes
hosts allow = 1.2.3.4 5.6.7.8
uid       = 0
gid       = 0

[vzconf]
path      = /etc/vz/conf
read only = yes
hosts allow = 1.2.3.4 5.6.7.8
uid       = 0
gid       = 0



the script:


#!/bin/bash

# sync and fail over an openvz VE
#
# params:
#
#     --check    silently check if sync should be run here
#     --sync     sync private area and config

# sample crontab entry
#
# */15 * * * * $HOME/bin/mon.failover.sh --check && $HOME/bin/mon.failover.sh --sync

# config section

root_user=root
hosts="ovzserver1 ovzserver2"
VEID=1001
sip=1.2.3.4
RSYNC_OPTIONS="-aH --delete --bwlimit 25000"

Log() {
 if [[ -t 0 ]]; then
  echo "$(date) " $@
 else
   logger -t $0 -p local3.info "$@"
 fi
}

if [[ -f /etc/vz/vz.conf ]]; then
   . /etc/vz/vz.conf
else
   Log this wont work without openvz
   exit 1
fi

thishost=
otherhosts=
for h in $hosts; do
  if [[ "$h" == $(uname -n) ]]; then
     thishost=$h
  else
     if [[ -z "$otherhosts" ]]; then
       otherhosts=$h
     else
       otherhosts="$h $otherhosts"
     fi
  fi
done 

if [[ "$1" == "--check" ]]; then
  [[ -z "$thishost" ]] && exit 1
  exit 0
fi

if [[ -z "$thishost" ]]; then
  echo "this script should only be run on the following host nodes: $hosts"
  exit 1
fi

if [[ "$1" == "--sync" ]]; then
  Log sync started from $otherhosts to $thishost
  if /usr/sbin/vzlist -H -o veid | grep -q $VEID; then
    Log $VEID is already here, not syncing
    exit
  else
    if [[ $(echo $otherhosts | wc -w ) == 1 ]]; then
       Log only one host remaining, this must be the source: $otherhosts
       source=$otherhosts
    else
      # be smart and try to figure out the source host
      # by comparing ARP entries
      siparp = $(arp -n $sip | tail -1 | awk '{ print $3 }')
      for host in $otherhosts; do
        harp=$(arp -n $host | tail -1 | awk '{ print $3 }')
        if [[ "$harp" == "$siparp" ]]; then
          source=$host
          Log detected source host: $source
        fi
      done
    fi

    Log syncing VE $VEID from $source
    rsync $RSYNC_OPTIONS $source::vzprivate/$VEID/ $VE_PRIVATE/ > /dev/null
    rsync $RSYNC_OPTIONS $source::vzconf/$VEID.conf /etc/vz/conf/$VEID.conf.failover
    Log sync done exit code $?
    exit
  fi
fi

# failover

echo -n "checking if ip is available..."
if ping -c 2 $sip > /dev/null; then
  echo "$sip is up, you should do a vzmigrate instead"
  exit 1
else 
  echo Not available, so will try to start up
  mv /etc/vz/conf/$VEID.conf.failover /etc/vz/conf/$VEID.conf
  vzctl start $VEID
fi

Log All done




More information about the Users mailing list