Maxim Khitrov
2015-01-30 15:22:23 UTC
Hi all,
I wrote two simple functions for rc.shutdown and rc.login that
save/restore unbound cache when the system is restarted. Since each
record has a relative TTL field, the cache can only be restored within
a short time window to avoid serving stale data to clients. I set this
window to 10 minutes; enough to survive a reboot, but not for any
extended downtime. Is there any interest in including this
functionality in the base OS (moved to /etc/rc)?
- Max
--- /var/backups/etc_rc.shutdown.current Mon Aug 4 21:03:16 2014
+++ /etc/rc.shutdown Fri Jan 30 10:06:11 2015
@@ -8,3 +8,17 @@
powerdown=NO # set to YES for powerdown
# Add your local shutdown actions here.
+
+save_unbound_cache() {
+ local db=/var/db/unbound.cache
+ /etc/rc.d/unbound check || return
+ echo -n 'saving unbound cache: '
+ if unbound-control dump_cache > $db; then
+ chmod 0600 $db
+ echo 'done.'
+ else
+ rm -f $db
+ fi
+}
+
+save_unbound_cache
--- /var/backups/etc_rc.local.current Mon Aug 4 21:03:16 2014
+++ /etc/rc.local Fri Jan 30 10:07:00 2015
@@ -4,3 +4,17 @@
# can be done AFTER your system goes into securemode. For actions
# which should be done BEFORE your system has gone into securemode
# please see /etc/rc.securelevel.
+
+restore_unbound_cache() {
+ local db=/var/db/unbound.cache
+ test -s $db && /etc/rc.d/unbound check || return
+ echo -n 'restoring unbound cache: '
+ if [ $(($(date '+%s') - $(stat -qf '%m' $db))) -lt 600 ]; then
+ unbound-control load_cache < $db
+ else
+ echo 'failed (cache expired).'
+ fi
+ rm -f $db
+}
+
+restore_unbound_cache
I wrote two simple functions for rc.shutdown and rc.login that
save/restore unbound cache when the system is restarted. Since each
record has a relative TTL field, the cache can only be restored within
a short time window to avoid serving stale data to clients. I set this
window to 10 minutes; enough to survive a reboot, but not for any
extended downtime. Is there any interest in including this
functionality in the base OS (moved to /etc/rc)?
- Max
--- /var/backups/etc_rc.shutdown.current Mon Aug 4 21:03:16 2014
+++ /etc/rc.shutdown Fri Jan 30 10:06:11 2015
@@ -8,3 +8,17 @@
powerdown=NO # set to YES for powerdown
# Add your local shutdown actions here.
+
+save_unbound_cache() {
+ local db=/var/db/unbound.cache
+ /etc/rc.d/unbound check || return
+ echo -n 'saving unbound cache: '
+ if unbound-control dump_cache > $db; then
+ chmod 0600 $db
+ echo 'done.'
+ else
+ rm -f $db
+ fi
+}
+
+save_unbound_cache
--- /var/backups/etc_rc.local.current Mon Aug 4 21:03:16 2014
+++ /etc/rc.local Fri Jan 30 10:07:00 2015
@@ -4,3 +4,17 @@
# can be done AFTER your system goes into securemode. For actions
# which should be done BEFORE your system has gone into securemode
# please see /etc/rc.securelevel.
+
+restore_unbound_cache() {
+ local db=/var/db/unbound.cache
+ test -s $db && /etc/rc.d/unbound check || return
+ echo -n 'restoring unbound cache: '
+ if [ $(($(date '+%s') - $(stat -qf '%m' $db))) -lt 600 ]; then
+ unbound-control load_cache < $db
+ else
+ echo 'failed (cache expired).'
+ fi
+ rm -f $db
+}
+
+restore_unbound_cache