#!/bin/sh # # $Id: checkdns.backup,v 1.7 2006/09/30 04:13:12 kadmos Exp $ # # Point the DNS for a domain to this server (the backup server) # if this server can't access the primary server. # # This script should be run periodically on the backup host. # # Which DNS records are updated is determined by the ddclient # configuration file (e.g. $HOME/.ddclient..conf). # # You must set the following variables in ~/.checkdns # domain name of A record to redirect # primary public host name of server that should serve domain by default # backup public host name of server that should serve domain if primary is down ### # FUNCTIONS # Import the common shell functions. if test -f "$HOME/bin/functions" then . "$HOME/bin/functions" else echo "${0##/*/}: Cannot find function library, exiting" exit 1 fi ### # CONFIGURATION # DEFAULTS # Device name of the internet interface. interface=ppp0 # Whether to print debug messages to stderr. debug= quiet= sms=true syslog=true while getopts "c:dq" flag do case $flag in d) debug=true quiet= ;; q) quiet=true debug= ;; *) error "Invalid option -$flag" exit 2 esac done # Read local configuration. if test -f "$HOME/.checkdns" then . "$HOME/.checkdns" else error "Cannot find configuration file $HOME/.checkdns, exiting" exit 1 fi # Configuration file for ddclient. conf=$HOME/.ddclient.$domain.conf # Cache file for ddclient. cache=$HOME/.ddclient.$domain.cache # must always call this function at the start of the script get_addresses() { primaryaddress=$(get_address_of ${primary}) if test $? -eq 0 then backupaddress=$(get_address_of ${backup}) if test $? -eq 0 then domainaddress=$(get_address_of ${domain}) if test $? -eq 0 then return 0 else return 1 fi else return 1 fi else return 1 fi } primary_down() { ! host_up ${primary} } redirected() { if test "${domainaddress}" = "${backupaddress}" then return 0 else return 1 fi } redirect_to_backup() { notice "Primary server for ${domain} is down, redirecting to ${backup} (${backupaddress})" run /usr/sbin/ddclient -file "${conf}" -cache /dev/null -use=ip -ip "${backupaddress}" if test $? -eq 0 then info "Redirected ${domain} to ${backup}" return 0 else error "Error redirecting ${domain} to ${backup}" return 1 fi } get_addresses if internet_reachable then if primary_down then info "Cannot reach ${primary}" if ! redirected then if redirect_to_backup then info "Redirection complete" exit 0 else error "Cannot redirect ${domain}" exit 1 fi else debug "Backup server for ${domain} is already in charge, not redirecting" exit 0 fi else debug "Primary server for ${domain} is up, not redirecting" exit 0 fi else error "Cannot reach the internet" exit 1 fi