Marks Blog
  • PowerDNS Admin
  • About
  • Simple Ticket System

Remote backup of Linux vserver

October 21, 2009 by mark

On my server i have several Linux Vservers running. This works really well, but i am missing some sort of fail-over. I have read articles about drdb8 and active-passive setups, but I don’t want something to complicated. To solve my problem, I have written a small script to rsync a vserver remotely. The script will be executed from the remote server and uses SSH, rsync and vserver-build to do the work. This will not provide any fail-over, but will give you a copy of the vserver in case the real server crashes. You will have to start the vserver manually and the data will be old (from when the script was executed). I have developed and tested the script on Debian GNU/Linux 5.0 (Lenny). It should work on other Linux distributions, but you may have to change some paths.

#!/bin/sh
# Small script to copy a linux vserver from remote host to local host by using
# ssh, rsync and vserver-build. To avoid typing password, you can generate ssh keys
# without passphrase and append public key to the remote .ssh/authorized_keys file.
# If you specify the third parameter, true, the vserver will be started on this host
# after copying is done. Otherwise it will be started on the remote host again.

# Check parameters
if [ $# -lt 2 ]; then
        echo "Usage: $0 [source] [vhost] "
        echo "Example: $0 root@primary-server vhost1"
        exit 1
fi

# Set values
SOURCE=${1:-"root@primary-server"}
VHOST=${2:-"vhost1"}
MIGRATE=${3:-"false"}
LOGFILE="/tmp/rsync-vhost_$$.log"

# Warning
echo "WARNING: this script will delete the LOCAL vserver ${VHOST} before copy/move"
echo "Press CTRL-C to abort !!!"
sleep 5

# Get context and network
CONTEXT=$(ssh -q -n $SOURCE cat /etc/vservers/${VHOST}/context)
NET_DEV=$(ssh -q -n $SOURCE cat /etc/vservers/${VHOST}/interfaces/0/dev)
NET_IP=$(ssh -q -n $SOURCE cat /etc/vservers/${VHOST}/interfaces/0/ip)
NET_PREFIX=$(ssh -q -n $SOURCE cat /etc/vservers/${VHOST}/interfaces/0/prefix)
NETWORK="${NET_DEV}:${NET_IP}/${NET_PREFIX}"

# Write to logfile
echo "Source: $SOURCE, Vserver: $VHOST, Migrate: $MIGRATE, Context: $CONTEXT, Network: $NETWORK" | tee $LOGFILE
# Make sure vserver is NOT running on local host
vserver $VHOST running >/dev/null 2>&1
if [ $? -eq 0 ]; then
        echo "Vserver $VHOST is already running on LOCAL host, aborting."
        exit 1
fi

# Make sure vserver is running on remote host
ssh $SOURCE "vserver $VHOST running" >/dev/null 2>&1
if [ $? -ne 0 ]; then
        echo "Vserver $VHOST is NOT running on REMOTE host, aborting."
        exit 1
fi

# Delete vserver on local host
echo "Deleting LOCAL copy of $VHOST"
echo 'y' | vserver $VHOST delete >/dev/null 2>&1

# Stop vserver on remote host
echo "Stopping REMOTE vserver ($VHOST on $SOURCE)"
ssh -q -n $SOURCE "vserver $VHOST stop" >>$LOGFILE 2>&1

# Build copy of vserver
echo "Building LOCAL copy of REMOTE vserver ($VHOST on $SOURCE)"
RSYNC_RSH=ssh
vserver ${VHOST} build -m rsync --context ${CONTEXT} \
  --hostname ${VHOST}.nellemann.biz \
  --interface ${NETWORK} \
  -- --source ${SOURCE}:/vservers/${VHOST}

# Start vserver on remote host - or local host if migrating
if [ x"$MIGRATE" == x"true" ]; then
        echo "Migrating $VHOST to LOCAL server"
        echo "vserver $VHOST start"
else
        echo "Starting REMOTE vserver ($VHOST on $SOURCE)"
        ssh -q -n $SOURCE "vserver $VHOST start" >>$LOGFILE 2>&1
fi

echo "done."
exit 0
Posted in: Linux & Unix Tagged: backup, debian, linux, vserver

Weather Station

Pages

  • About
  • PowerDNS Admin
  • Simple Ticket System

Affiliates

  • Danish Sensor Engineering
  • Mintr ApS
  • Nellemann Data

Social

  • BitBucket
  • GitHub
  • LinkedIn
  • Mastodon

Categories

  • Android
  • Automobile
  • Blog & Rantings
  • Development & Scripting
  • Grails
  • Linux & Unix
  • Mac OS X
  • Misc
  • No category
  • PowerDNS Admin
  • Puppet
  • Security Related
  • VirtualBox
  • Virtualization
  • VMware
  • Windows

Archives

  • January 2019
  • September 2017
  • January 2016
  • May 2015
  • March 2015
  • February 2015
  • December 2014
  • October 2014
  • September 2014
  • August 2014
  • June 2014
  • January 2014
  • July 2013
  • June 2013
  • February 2013
  • January 2013
  • December 2012
  • August 2012
  • May 2012
  • October 2011
  • April 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • March 2010
  • October 2009
  • September 2009
  • April 2009
  • January 2009
  • March 2008
  • January 2008
  • November 2007
  • January 2007
  • December 2006
  • November 2006
  • August 2006
  • June 2006
  • March 2006
  • October 2005
  • August 2005

Recent Posts

  • Random Password Generator
  • Recommended Ubuntu PPAs
  • Grails remote logging with Graylog
  • Simple Ticket System
  • Writing an ISO image to USB on a Mac

Tags

backup cache cisco collectd debian dns fans filesystem gpt grails groovy handbrake htaccess https ibook imap iso konsol linux mac mdadm monitor nagios osx performance php plugin ppc proliant raid rrd software space squeeze ssd sync temperature ubuntu usb virtualbox vmware vpn windows xfs zfs

Social

  • BitBucket
  • GitHub
  • LinkedIn
  • Mastodon

Affiliates

  • Danish Sensor Engineering
  • Mintr ApS
  • Nellemann Data

Copyright © 2025 Marks Blog.

Mobile WordPress Theme by themehall.com