monero/external/unbound/contrib/unbound_cache.sh

175 lines
3.4 KiB
Bash
Raw Normal View History

2014-10-05 15:44:31 -06:00
#!/sbin/sh
2015-04-02 03:16:18 -06:00
2014-10-05 15:44:31 -06:00
# --------------------------------------------------------------
# -- DNS cache save/load script
# --
2015-04-02 03:16:18 -06:00
# -- Version 1.2
2014-10-05 15:44:31 -06:00
# -- By Yuri Voinov (c) 2006, 2014
# --------------------------------------------------------------
#
2015-04-02 03:16:18 -06:00
# ident "@(#)unbound_cache.sh 1.2 14/10/30 YV"
2014-10-05 15:44:31 -06:00
#
#############
# Variables #
#############
# Installation base dir
CONF="/etc/opt/csw/unbound"
BASE="/opt/csw"
# Unbound binaries
UC="$BASE/sbin/unbound-control"
FNAME="unbound_cache.dmp"
# OS utilities
BASENAME=`which basename`
CAT=`which cat`
CUT=`which cut`
ECHO=`which echo`
2015-04-02 03:16:18 -06:00
EXPR=`which expr`
2014-10-05 15:44:31 -06:00
GETOPT=`which getopt`
ID=`which id`
2015-04-02 03:16:18 -06:00
LS=`which ls`
2014-10-05 15:44:31 -06:00
###############
# Subroutines #
###############
usage_note ()
{
# Script usage note
2015-04-02 03:16:18 -06:00
$ECHO "Usage: `$BASENAME $0` [-s] or [-l] or [-r] or [-h] [filename]"
$ECHO .
2014-10-05 15:44:31 -06:00
$ECHO "l - Load - default mode. Warming up Unbound DNS cache from saved file. cache-ttl must be high value."
$ECHO "s - Save - save Unbound DNS cache contents to plain file with domain names."
$ECHO "r - Reload - reloadind new cache entries and refresh existing cache"
$ECHO "h - this screen."
2015-04-02 03:16:18 -06:00
$ECHO "filename - file to save/load dumped cache. If not specified, $CONF/$FNAME will be used instead."
2014-10-05 15:44:31 -06:00
$ECHO "Note: Run without any arguments will be in default mode."
$ECHO " Also, unbound-control must be configured."
exit 0
}
root_check ()
{
if [ ! `$ID | $CUT -f1 -d" "` = "uid=0(root)" ]; then
$ECHO "ERROR: You must be super-user to run this script."
exit 1
fi
}
check_uc ()
{
if [ ! -f "$UC" ]; then
$ECHO .
$ECHO "ERROR: $UC not found. Exiting..."
exit 1
fi
}
check_saved_file ()
{
2015-04-02 03:16:18 -06:00
filename=$1
if [ ! -z "$filename" -a ! -f "$filename" ]; then
$ECHO .
$ECHO "ERROR: File $filename does not exists. Save it first."
exit 1
elif [ ! -f "$CONF/$FNAME" ]; then
2014-10-05 15:44:31 -06:00
$ECHO .
$ECHO "ERROR: File $CONF/$FNAME does not exists. Save it first."
exit 1
fi
}
save_cache ()
{
# Save unbound cache
2015-04-02 03:16:18 -06:00
filename=$1
if [ -z "$filename" ]; then
$ECHO "Saving cache in $CONF/$FNAME..."
$UC dump_cache>$CONF/$FNAME
$LS -lh $CONF/$FNAME
else
$ECHO "Saving cache in $filename..."
$UC dump_cache>$filename
$LS -lh $filename
fi
2014-10-05 15:44:31 -06:00
$ECHO "ok"
}
load_cache ()
{
2015-04-02 03:16:18 -06:00
# Load saved cache contents and warmup cache
filename=$1
if [ -z "$filename" ]; then
$ECHO "Loading cache from saved $CONF/$FNAME..."
$LS -lh $CONF/$FNAME
check_saved_file $filename
$CAT $CONF/$FNAME|$UC load_cache
else
$ECHO "Loading cache from saved $filename..."
$LS -lh $filename
check_saved_file $filename
$CAT $filename|$UC load_cache
fi
2014-10-05 15:44:31 -06:00
}
reload_cache ()
{
# Reloading and refresh existing cache and saved dump
2015-04-02 03:16:18 -06:00
filename=$1
save_cache $filename
load_cache $filename
2014-10-05 15:44:31 -06:00
}
##############
# Main block #
##############
# Root check
root_check
# Check unbound-control
check_uc
# Check command-line arguments
2015-04-02 03:16:18 -06:00
if [ "x$*" = "x" ]; then
# If arguments list empty,load cache by default
2014-10-05 15:44:31 -06:00
load_cache
else
2015-04-02 03:16:18 -06:00
arg_list=$*
2014-10-05 15:44:31 -06:00
# Parse command line
set -- `$GETOPT sSlLrRhH: $arg_list` || {
usage_note 1>&2
}
2015-04-02 03:16:18 -06:00
# Read arguments
2014-10-05 15:44:31 -06:00
for i in $arg_list
do
case $i in
2015-04-02 03:16:18 -06:00
-s | -S) save="1";;
-l | -L) save="0";;
-r | -R) save="2";;
2014-10-05 15:44:31 -06:00
-h | -H | \?) usage_note;;
2015-04-02 03:16:18 -06:00
*) shift
file=$1
break;;
2014-10-05 15:44:31 -06:00
esac
2015-04-02 03:16:18 -06:00
shift
2014-10-05 15:44:31 -06:00
done
2015-04-02 03:16:18 -06:00
# Remove trailing --
shift `$EXPR $OPTIND - 1`
fi
if [ "$save" = "1" ]; then
save_cache $file
elif [ "$save" = "0" ]; then
load_cache $file
elif [ "$save" = "2" ]; then
reload_cache $file
2014-10-05 15:44:31 -06:00
fi
2015-12-30 23:39:56 -07:00
exit 0