#!/bin/bash
+# Conditionally run obnam.
+#
+# If $HOST_NAME is defined, check that we can ping it.
+# If $HOST_MAC is defined, check that the arp/neighbour table has a matching
+# MAC address.
+# If those conditions are meet, then backup $SOURCE, but first grab some
+# debconf data so we can reconstruct the packages installed if we need to.
+
. /etc/default/etc-obnam
CLIENT=$(hostname)
SOURCE=${SOURCE:-/home /etc}
WORKDIR=/var/lib/etc-obnam
+# Check that the host is local, support both IPv4 and IPv6.
function check_host() {
local type=$1
if [ "x$type" != "xA" -a "x$type" != "xAAAA" ]
return 0
fi
+ # Make sure that we can get DNS details for the host first.
local ip=$(host -t $type $HOST_NAME | grep address | awk '{ print $NF }' | head -1)
if [ "x$ip" = "x" ]
then
return 1
fi
+ # ping or ping6?
local cmd=ping
if [ $type = "AAAA" ]
then
cmd=ping6
fi
+ # ping at will.
if ! $cmd -n -c3 $HOST_NAME &> /dev/null
then
return 1
fi
+ # Check the neighbour table. If $HOST_NAME is *us* then we won't have an
+ # entry, that is why HOST_MAC is optional. Could check ip link to see if
+ $ $HOST_MAC is on this box.
local mac=$(ip neigh | egrep "^$ip " | grep REACHABLE | awk '{ print $5 }' )
if [ $(echo $mac | wc -l ) -ne 1 ]
then
return 0
}
+# Grab enough details to hopefully be able to reconstuct the installed
+# packages.
function capture_debconf_info() {
# Keep a list of the installed files
dpkg --get-selections > $WORKDIR/installed.packages.new
fi
}
-
+# Check to see if the right host is present, if it is, do a backup.
if ( check_host 'AAAA' -o check_host 'A' )
then
capture_debconf_info