Alternative bash script to clone VPS on OpenVZ

Bagi kawan-kawan yang masih menggunakan platform OpenVZ dan membutuhkan simple bash script untuk memudahkan cloning VPS untuk keperluan backup atau migrasi. Silakan digunakan sebaik-baiknya.

Disclaimer :

Berikut sistem yang digunakan untuk testing script yang saya buat

[root@xxxxxxxx indra]# rpm -qa | grep vzctl
vzctl-core-4.9.4-1.x86_64
vzctl-4.9.4-1.x86_64
[root@xxxxxxxx indra]# rpm -qi vzctl
Name        : vzctl                        Relocations: (not relocatable)
Version     : 4.9.4                             Vendor: (none)
Release     : 1                             Build Date: Thu 27 Aug 2015 07:41:54 AM SGT
Install Date: Thu 22 Oct 2015 09:23:47 PM SGT      Build Host: kir-rhel5-64-tbuild
Group       : System Environment/Kernel     Source RPM: vzctl-4.9.4-1.src.rpm
Size        : 268678                           License: GPLv2+
Signature   : DSA/SHA1, Thu 27 Aug 2015 07:43:46 AM SGT, Key ID 92a60da6a7a1d4b6
URL         : http://openvz.org/
Summary     : OpenVZ containers control utility
Description :
This utility allows system administrators to control Linux containers,
i.e. create, start, shutdown, set various options and limits etc.
[root@xxxxxxxx indra]# uname -a 
Linux xxxxxxxx.netdns.net 2.6.32-042stab112.15 #1 SMP Tue Oct 20 17:22:56 MSK 2015 x86_64 x86_64 x86_64 GNU/Linux 

The codes :

Silahkan di modifikasi sesuai kebutuhan pembaca :

#!/bin/bash
## script name : vzclone.sh
#configuration
trap "" 1 2 3 15
ERROR=0
ARGV="$@"

if [ "$#" -lt 2 ] || [ "$#" -gt 2 ] ; then
   echo "command : ./vzclone.sh  "
   exit
else
   re='^[0-9]+$'
   if ! [[ $1 =~ $re ]] && ! [[ $2 =~ $re ]] ; then
      echo "error: VEID must be a number" >&2;
      echo "command : ./vzclone.sh  ";
      exit
   fi
fi

OLDVE=$1
NEWVE=$2
vzctl stop $OLDVE
mkdir /vz/root/$NEWVE
echo "copying......"
cp /etc/vz/conf/$OLDVE.conf /etc/vz/conf/$NEWVE.conf
cp -a /vz/private/$OLDVE /vz/private/$NEWVE
/bin/sed -i "s/\/$OLDVE/\/$NEWVE/g" /etc/vz/conf/$NEWVE.conf
echo "Starting old VPS ..."
vzctl start $OLDVE;

echo -n "Start new VPS ? [Y/n] : "
read opsi

if [ $opsi ]; then
if [ $opsi == "Y"  ]; then
vzctl start $NEWVE;

##== change hostname
CurHostname=`vzlist -a | grep $OLDVE | tr -s ' ' | cut -d" " -f6`
tmpHostname=$CurHostname"_clone"

echo -n "New VPS Hostname ? [ "$CurHostname"_clone ] : "
read NewHostname
if [ -nz $NewHostname ]; then
vzctl set $NEWVE --hostname $NewHostname --save
else
vzctl set $NEWVE --hostname $tmpHostname --save
fi
##== change hostname

fi
fi

NewIP=""
echo -n "New VPS IP Address? [Empty] : "
read NewIP

CurIP=`vzlist -a | grep $OLDVE | tr -s ' ' | cut -d" " -f5`
if [ $CurIP != '-' ]; then
vzctl set $NEWVE --ipdel $CurIP --save
fi

if [[ -nz $NewIP ]]; then
vzctl set $NEWVE --ipadd $NewIP --save
fi

echo "done."

Buat website murah

Cara menjalankan script:

# ./vzclone  [Old VPS ID]  [New VPS ID]

Contoh :

# ./vzclone  999 1005

Semoga bermanfaat.

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

This site uses Akismet to reduce spam. Learn how your comment data is processed.