Veeam Backup & Replication – Re-IP Rule on Linux VM

Veeam Backup & Replication is one of the best backup and replication software that you can use them in your virtual environment. Veeam BR be able to IP customization during disaster-recovery and you can have your virtual machines in a DR site with different IP plan. But Veeam BR just support Windows VMs for IP customization or “Re-IP Rule”. So what can we do about Linux virtual machines and others?

Veeam BR - Re-IP Rule

There is one unique answer (As I know):

You have to do it manually or by a script during replica machine boot process.

I wrote two scripts on this regard and you can customize them and put one of them on your machine “/etc/rc.d/rc.local” and the script will run as a startup script.

don’t forget make them executable: “chmod +x \path\to\script”

The first script will update your “ifcfg” file and restart network service:

#!/bin/bash
# The script will be worked on RHEL/Fedora/CentOS.
# Set logging options

# Variables.

primary_gw="192.168.100.254"
replica_gw="192.168.200.254"
primary_ipaddr="192.168.100.10"
replica_ipaddr="192.168.200.10"
primary_netmask="24"
replica_netmask="24"
device_name=""

# Logging function

readonly SCRIPT_NAME=$(basename $0)

log() {
  echo "$(date) $@"
# logger -p user.error -t $SCRIPT_NAME "$@"
  logger -s "$(date) $@" 2>> /var/log/dr.log
}

err() {
  echo "$(date) $@" >&2
# logger -p user.error -t $SCRIPT_NAME "$@"
  logger -s "$(date) $@" 2>> /var/log/dr.log
}

# Checking replica machine IP address during boot and reconfigure IP address, if there is need to change it.


# First Step: Checking primary default gateway by ping the IP.
# Retrieving primary gateway via default route.

log "Start to check disaster recovery."
log "Detecting default gateway."
current_gw=$(route -n | grep '^0\.0\.\0\.0[ \t]\+[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]\+[ \t]\+0\.0\.0\.0[ \t]\+[^ \t]*G[^ \t]*[ \t]' | awk '{print $2}')
log "Default gateway is $current_gw."

# Ping the primary default gateway.

log "Start to send ICMP requests to $primary_gw."
ping -c 1 -t 1 $primary_gw > /dev/null
if [ $? -eq 0 ]; then

# Ping successful, no change is needed.

log "Primary gateway is reachable and there was no disaster. So the current configurations will be not changed."
exit 0

# Ping unsuccessful and IP address should change to DR site IP range.

else

# Changing address to DR site IP range and change default gateway as well.

log "Because primary default gateway is not reachable, script is going to changing IP address and default gateway to DR site range."

# Finding device name of network adapter that the IP has been assigned to that.

log "Finding network adapter name."
device_name=$(ip addr | grep "inet $primary_ipaddr/$primary_netmask" | awk '{print $7}')
if [ ! "$device_name" ]; then
device_name=$(ip addr | grep "inet $replica_ipaddr/$replica_netmask" | awk '{print $7}')
fi
if [ ! "$device_name" ]; then
log "There is no active connection, script could not detect network adapter."
exit 0
fi
log "Network adapter name is $device_name."
log "Changing IP address to $replica_ipaddr."
sed -i "/IPADDR=/c\IPADDR=$replica_ipaddr" /etc/sysconfig/network-scripts/ifcfg-$device_name
log "Set default gateway for the network connection."
sed -i "/GATEWAY=/c\GATEWAY=$replica_gw" /etc/sysconfig/network-scripts/ifcfg-$device_name
sed -i "/PREFIX=/c\PREFIX=$replica_netmask" /etc/sysconfig/network-scripts/ifcfg-$device_name
log "Set connection to UP status."
service network restart
nmcli connection up $device_name

fi

# Ping the replica gateway (DR Site) to make sure about network connectivity.

log "Testing new IP address in DR site by sending ICMP requests to DR default gateway."
ping -c 1 -t 1 $replica_gw > /dev/null
if [ $? -eq 0 ]; then

# Ping successful and disaster recovery has been performed successfully.

log "Replica gateway is reachable and there is no problem. This is replica virtual machine."

exit 0

# Ping unsuccessful, so there is wrong DR detection and IP configuration should revert to primary site range.

else

log "Primary and replica gateway addresses are not reachable, so there is something wrong!!!"
log "Script is going to configure primary configuration on machine."

log "Changing IP address to $primary_ipaddr."
sed -i "/IPADDR=/c\IPADDR=$primary_ipaddr" /etc/sysconfig/network-scripts/ifcfg-$device_name
log "Set default gateway for the network connection."
sed -i "/GATEWAY=/c\GATEWAY=$primary_gw" /etc/sysconfig/network-scripts/ifcfg-$device_name
sed -i "/PREFIX=/c\PREFIX=$primary_netmask" /etc/sysconfig/network-scripts/ifcfg-$device_name
log "Set connection to UP status."
service network restart
nmcli connection up $device_name

fi

The second script doesn’t touch your “ifcfg” files and just add IP and route to your network connection:

#!/bin/bash
# The script will be worked on RHEL/Fedora/CentOS.
# Set logging options
# Variables.
primary_gw="192.168.100.254"
replica_gw="192.168.200.254"
primary_ipaddr="192.168.100.0"
replica_ipaddr="192.168.200.10"
primary_netmask="24"
replica_netmask="24"
device_name=""
# Logging function
readonly SCRIPT_NAME=$(basename $0)
log() {
  echo "$(date) $@"
# logger -p user.error -t $SCRIPT_NAME "$@"
  logger -s "$(date) $@" 2>> /var/log/dr.log
}
err() {
  echo "$(date) $@" >&2
# logger -p user.error -t $SCRIPT_NAME "$@"
  logger -s "$(date) $@" 2>> /var/log/dr.log
}
# Checking replica machine IP address during boot and reconfigure IP address, if there is need to change it.
# First Step: Checking primary default gateway by ping the IP.
# Retrieving primary gateway via default route.
log "Start to check disaster recovery."
log "Detecting default gateway."
current_gw=$(route -n | grep '^0\.0\.\0\.0[ \t]\+[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]*\.[1-9][0-9]\+[ \t]\+0\.0\.0\.0[ \t]\+[^ \t]*G[^ \t]*[ \t]' | awk '{print $2}')
log "Default gateway is $current_gw."
# Ping the primary default gateway.
log "Start to send ICMP requests to $primary_gw."
ping -c 1 -t 1 $primary_gw > /dev/null
if [ $? -eq 0 ]; then
# Ping successful, no change is needed.
log "Primary gateway is reachable and there was no disaster. So the current configurations will be not changed."
exit 0
# Ping unsuccessful and IP address should change to DR site IP range.
else
# Changing address to DR site IP range and change default gateway as well.
log "Because primary default gateway is not reachable, script is going to changing IP address and default gateway to DR site range."
# Finding device name of network adapter that the IP has been assigned to that.
log "Finding network adapter name."
device_name=$(ip addr | grep "inet $primary_ipaddr/$primary_netmask" | awk '{print $7}')
if [ ! "$device_name" ]; then
device_name=$(ip addr | grep "inet $replica_ipaddr/$replica_netmask" | awk '{print $7}')
fi
if [ ! "$device_name" ]; then
log "There is no active connection, script couldn not detect network adapter."
exit 0
fi
log "Network adapter name is $device_name."
log "Changing IP address to $replica_ipaddr."
ip addr flush dev $device_name
ip addr add $replica_ipaddr$replica_netmask brd + dev $device_name
log "Set route for the network connection."
ip route add default via $replica_gw dev $device_name
log "Set link to UP status."
ip link set down dev $device_name
ip link set up dev $device_name
fi
# Ping the replica gateway (DR Site) to make sure about network connectivity.
log "Testing new IP address in DR site by sending ICMP requests to DR default gateway."
ping -c 1 -t 1 $replica_gw > /dev/null
if [ $? -eq 0 ]; then
# Ping successful and disaster recovery has been performed successfully.
log "Replica gateway is reachable and there is no problem. This is replica virtual machine."
exit 0
# Ping unsuccessful, so there is wrong DR detection and IP configuration should revert to primary site range.
else
log "Primary and replica gateway addresses are not reachable, so there is something wrong!!!"
log "Script is going to configure primary configuration on machine."
log "Changing IP address to $primary_ipaddr."
ip addr flush dev $device_name
ip addr add $primary_ipaddr$primary_netmask brd + dev $device_name
log "Set route for the network connection."
ip route add default via $primary_gw dev $device_name
log "Set link to UP status."
ip link set down dev $device_name
ip link set up dev $device_name
fi

Don’t forget add your IP addresses, default gateways and mask in the variables section and replace them with your IP plan.

Update: Don’t forget make rc.local executable by this command-line: sudo chmod +x /etc/rc.d/rc.local

Update (09/07/2018)

New sample of script will replace disaster IP range in some files such as “/etc/hosts”, “/etc/fstab” and other files and do some action according to OS version, you can customize the script as well:

#!/bin/bash
# The script will be worked on RHEL/Fedora/CentOS.
# Set logging options
# Variables.
primary_ip_check="192.168.1.254"
replica_ip_check="192.168.2.254"
primary_net="19.168.1"
replica_net="192.168..2"
keep_alive_file="/etc/keepalived/keepalived.conf"
hosts_file="/etc/hosts"
fstab_file="/etc/fstab"
exports_file="/etc/exports"
# Logging function
readonly SCRIPT_NAME=$(basename $0)
log() {
  echo "$(date) $@"
# logger -p user.error -t $SCRIPT_NAME "$@"
  logger -s "$(date) $@" 2>> /var/log/dr.log
}
err() {
  echo "$(date) $@" >&2
# logger -p user.error -t $SCRIPT_NAME "$@"
  logger -s "$(date) $@" 2>> /var/log/dr.log
}
# First Step: Checking defined IP from primary site by sending ICMP requests to the IP.
# Ping the primary default gateway.
log "Start to send ICMP requests to $primary_ip_check."
ping -c 1 -t 1 $primary_ip_check > /dev/null
if [ $? -eq 0 ]; then
               # Ping successful, no change is needed.
               log "$primary_ip_check is reachable in primary site and no disaster has been occurred. So no change will be applied on configurations."
               exit 0
# Ping unsuccessful and IP address should change to DR site IP range.
else
               # Changing address to DR site IP range and change default gateway as well.
               log "$primary_ip_check is not reachable, script is going to changing IP address and anyother configuration by using replica site IP addresses."
               #Collecting all network script files
               log "Collecting network script files."
               ifcfg_files=($(ls /etc/sysconfig/network-scripts/ifcfg-*))
               for ((i=0; i<${#ifcfg_files[@]}; i++)); do
                              #change IP range according to each array elements
                              log "Changing IP range to $replica_net on ${ifcfg_files[i]} ."
                              sed -i "s@$primary_net@$replica_net@" ${ifcfg_files[i]}
               done
               log "Apply the new configuration on ethernet devices."
               OS_ver=$(lsb_release -a | grep Release | awk '{print $2}'| cut -d '.' -f1)
               if [ $OS_ver -eq 6 ]; then
                              service network restart
               fi
               if [ $OS_ver -eq 7 ]; then
                              systemctl status network.service
               fi
fi
# Ping the replica gateway (DR Site) to make sure about network connectivity.
log "Testing new IP configurations in DR site by sending ICMP requests to $replica_ip_check"
ping -c 1 -t 1 $replica_ip_check > /dev/null
if [ $? -eq 0 ]; then
               # Ping successful and disaster recovery has been performed successfully.
               log "$replica_ip_check is reachable and there is no problem. This is replica virtual machine."
               #Change IP range to $replica_net in /etc/fstab, /etc/keepalived/keepalived.conf, /etc/exports, /etc/hosts
               if [ -f "$keep_alive_file" ]
               then
                              log "IP range has been change to $replica_net in $keep_alive_file ."
                              sed -i "s@$primary_net@$replica_net@" $keep_alive_file
               else
                              log "$keep_alive_file was not found."
                              echo "$keep_alive_file was not found."
               fi
               if [ -f "$exports_file" ]
               then
                              log "IP range has been changed to $replica_net in $exports_file ."
                              sed -i "s@$primary_net@$replica_net@" $exports_file
               else
                              log "$exports_file was not found, seems NFS service has not configured."
                              echo "$exports_file was not found."
               fi
               sed -i "s@$primary_net@$replica_net@" $fstab_file
               log "IP range has been changed to $replica_net in $fstab_file (If there was any IP with the specified IP range) ."
               sed -i "s/1.237/2.201/g" $hosts_file
               sed -i "s/1.238/2.202/g" $hosts_file
               sed -i "s/1.239/2.203/g" $hosts_file
               sed -i "s@$primary_net@$replica_net@" $hosts_file
               log "IP range has been changed to $replica_net in $hosts_file ."
               exit 0
else
               # Ping unsuccessful, so there is wrong DR detection and IP configuration should revert to primary site IP range.
               log "Primary and replica addresses are not reachable, so there is something wrong!!!"
               log "Script is going to configure primary site configuration on machine."
               #Collecting all network script files
               log "Collecting network script files."
               ifcfg_files=($(ls /etc/sysconfig/network-scripts/ifcfg-*))
               for ((i=0; i<${#ifcfg_files[@]}; i++)); do
                              #change IP range according to each array elements
                              log "Changing IP range to $primary_net on ${ifcfg_files[i]} ."
                              sed -i "s@$replica_net@$primary_net@" ${ifcfg_files[i]}
               done
               #Change IP range to $primary_net in /etc/fstab, /etc/keepalived/keepalived.conf, /etc/exports, /etc/hosts
               if [ -f "$keep_alive_file" ]
               then
                              log "IP range has been change to $primary_net in $keep_alive_file ."
                              sed -i "s@$replica_net@$primary_net@" $keep_alive_file
               else
                              log "$keep_alive_file was not found."
                              echo "$keep_alive_file was not found."
               fi
               if [ -f "$exports_file" ]
               then
                              log "IP range has been changed to $primary_net in $exports_file ."
                              sed -i "s@$replica_net@$primary_net@" $exports_file
               else
                              log "$exports_file was not found, seems NFS service has not configured."
                              echo "$exports_file was not found."
               fi
               sed -i "s@$replica_net@$primary_net@" $fstab_file
               log "IP range has been changed to $primary_net in $fstab_file (If there was any IP with the specified IP range) ."
               sed -i "s/2.201/1.237/g" $hosts_file
               sed -i "s/2.202/1.238/g" $hosts_file
               sed -i "s/2.203/1.239/g" $hosts_file
               sed -i "st@$replica_net@$primary_ne@" $hosts_file
               log "IP range has been changed to $replica_net in $hosts_file ."
               log "Apply the new configuration on ethernet devices."
               OS_ver=$(lsb_release -a | grep Release | awk '{print $2}'| cut -d '.' -f1)
               if [ $OS_ver -eq 6 ]; then
                              service network restart
               fi
               if [ $OS_ver -eq 7 ]; then
                              systemctl status network.service
               fi
fi

More on Teimouri.Net

[Review]: What’s Remote Direct Memory Access(RDMA)?

[Script]: Enable/Disable vMotion on VMKernel Ports via PowerCLI

[Review]: Veeam Backup & Replication 9.5 Update 3a

VMware Tools Client – Interact with a VM without Network Connectivity

Run ESXi Commands Via PowerShell And SSH

ESXi PCI Passthrough – Large VM Memory (MainHeap) BUG!

[Review]: Dell EMC RecoverPoint for Virtual Machines

Add existing virtual desktops to “Automated Pool” in VMware View manually

[Script]: Check Time Synchronization with Host on Virtual Machines – PowerCLI

Linux KickStart – Automated Installation

[How To]: Change Linux IP Address via PowerCLI

Veeam Backup & Replication Best Practices

[Linux]: /etc/sysconfig/network – No Such File or Directory

PCoIP Configurations – Client Side

Davoud Teimouri

Davoud Teimouri is as a professional blogger, vExpert 2015/2016/2017/2018/2019, VCA, MCITP. This blog is started with simple posts and now, it has large following readers.

1 Response

  1. 27/07/2019

    […] Veeam Backup & Replication – Re-IP Rule on Linux VM […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newsletter and join other subscribers

Holler Box