Skip to content

Instantly share code, notes, and snippets.

@miticojo
Last active July 3, 2016 14:58
Show Gist options
  • Save miticojo/3fa86436282ef67d1e3ee4f6dd182261 to your computer and use it in GitHub Desktop.
Save miticojo/3fa86436282ef67d1e3ee4f6dd182261 to your computer and use it in GitHub Desktop.
network rollback script for centos 7 (with ovs)
#!/bin/bash
netscripts="/etc/sysconfig/network-scripts"
netlock="/var/spool/network-restore.lck"
logfile="/var/log/net-rollback.log"
check_ip="8.8.8.8"
log () {
echo $1 >&2
echo "`date -u` - $1" >> $logfile
}
sh -c 'ping -c 5 '$check_ip' -q > /dev/null 2>&1'
if [ "$?" != "0" ]; then
log "public network is unreachable"
if [ -f $netlock ]; then
log "lock file exists - nothing will be touched till network restore"
exit 2
fi
log "moving current configuration to $netscripts/ifcfg-eth0.new"
mv $netscripts/ifcfg-eth0 $netscripts/ifcfg-eth0.new
log "restoring old configuration to $netscripts/ifcfg-eth0"
mv $netscripts/ifcfg-eth0.bck $netscripts/ifcfg-eth0
log "reset ovs conf db"
rm -rf /var/log/openvswitch/*
rm -rf /etc/openvswitch/conf.db
log "restarting ovs"
systemctl restart openvswitch
sleep 5
log "restarting network"
systemctl restart network
sleep 5
sh -c 'ping -c 5 '$check_ip' -q > /dev/null 2>&1'
if [ "$?" != "0" ]; then
log "network recover didn't work and machine is isolated!"
touch $netlock
else
log "network restored and public internet reachable"
fi
else
if [ ! -f $netscripts/ifcfg-eth0.bck ]; then
log "machine reaches public network and a backup conf is taken"
cp $netscripts/ifcfg-eth0 $netscripts/ifcfg-eth0.bck
fi
if [ -f $netlock ]; then
log "lock file is no more needed and will be deleted"
rm -f $netlock
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment