unbound_cache.sh 3.38 KB
Newer Older
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
1
#!/sbin/sh
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
2

Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
3
4
5
# --------------------------------------------------------------
# -- DNS cache save/load script
# --
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
6
# -- Version 1.2
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
7
8
9
# -- By Yuri Voinov (c) 2006, 2014
# --------------------------------------------------------------
#
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
10
# ident   "@(#)unbound_cache.sh     1.2     14/10/30 YV"
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#

#############
# 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`
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
30
EXPR=`which expr`
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
31
32
GETOPT=`which getopt`
ID=`which id`
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
33
LS=`which ls`
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
34
35
36
37
38
39
40
41

###############
# Subroutines #
###############

usage_note ()
{
# Script usage note
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
42
43
 $ECHO "Usage: `$BASENAME $0` [-s] or [-l] or [-r] or [-h] [filename]"
 $ECHO .
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
44
45
46
47
 $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."
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
48
 $ECHO "filename - file to save/load dumped cache. If not specified, $CONF/$FNAME will be used instead."
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
 $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 ()
{
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
73
74
75
76
77
78
 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
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
79
80
81
82
83
84
85
86
87
  $ECHO .
  $ECHO "ERROR: File $CONF/$FNAME does not exists. Save it first."
  exit 1
 fi
}

save_cache ()
{
 # Save unbound cache
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
88
89
90
91
92
93
94
95
96
97
 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
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
98
99
100
101
102
 $ECHO "ok"
}

load_cache ()
{
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
103
104
105
106
107
108
109
110
111
112
113
114
115
 # 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
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
116
117
118
119
120
}

reload_cache ()
{
 # Reloading and refresh existing cache and saved dump
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
121
122
123
 filename=$1
 save_cache $filename
 load_cache $filename
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
124
125
126
127
128
129
130
131
132
133
134
135
136
}

##############
# Main block #
##############

# Root check
root_check

# Check unbound-control
check_uc

# Check command-line arguments
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
137
138
if [ "x$*" = "x" ]; then
 # If arguments list empty,load cache by default
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
139
140
 load_cache
else
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
141
 arg_list=$*
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
142
143
144
145
146
 # Parse command line
 set -- `$GETOPT sSlLrRhH: $arg_list` || {
  usage_note 1>&2
 }

Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
147
 # Read arguments
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
148
149
150
 for i in $arg_list
  do
   case $i in
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
151
152
153
    -s | -S) save="1";;
    -l | -L) save="0";;
    -r | -R) save="2";;
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
154
    -h | -H | \?) usage_note;;
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
155
156
157
    *) shift
       file=$1
       break;;
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
158
   esac
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
159
   shift
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
160
  done
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
161
162
163
164
165
166
167
168
169
170
171

 # 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
Dag-Erling Smørgrav's avatar
Dag-Erling Smørgrav committed
172
173
174
fi

exit 0