advanced (Blog)http://www.turnkeylinux.org/blog/term/907/0
enHow TKLBAM hooks workhttp://www.turnkeylinux.org/blog/tklbam-hooks
<p>
Most TKLBAM users probably don&#39;t realize this, but TKLBAM has a nifty, general purpose hooks mechanism you can use to trigger useful actions on backup and restore.</p>
<p>
Examples of hooks:</p>
<ul class="simple">
<li>
Cleaning up temporary files</li>
<li>
Stopping/starting services to increase data consistency</li>
<li>
Encoding/decoding data from non-supported databases</li>
<li>
Using LVM to create/restore a snapshot of a fast changing volume</li>
</ul>
<!--break-->
<p>
Originally I developed the hooks mechanism so we could fix a few issues indirectly related to the usability of TKLBAM. In particular, our very first beta users reported that sometimes tklbam-restore would fail to find any backup volumes. When we investigated this turned out to be a clock discrepancy. The obvious solution was to sync the clock before starting the restore, but the more I thought about it the more the idea of hardwiring that ntpdate stuff rubbed me the wrong way. For a few reasons:</p>
<ul class="simple">
<li>
It&#39;s an auxiliary problem, not a core issue with TKLBAM&#39;s logic</li>
<li>
I&#39;m offline much of the time during development so I needed some way to turn this off, but I don&#39;t want to add more testing-specific code unless it&#39;s absolutely necessary.</li>
<li>
It&#39;s OK if a specific server (e.g., pool.ntp.org) is the default, but there should be some way to configure it if a user, for example, wants to use an internal NTP server.</li>
</ul>
<p>
I tried to think of a clean way to achieve these simple goals in a clean way (e.g., cli options, environment variables, configuration files), but everything I came up with was just so darn ugly.</p>
<p>
Then I realized that a hooks mechanism would solve this problem in a simple, generic way.</p>
<div class="section" id="implementation">
<h2>
Implementation</h2>
<p>
/etc/tklbam/hooks.d may contains executables (e.g., scripts) that will be run by tklbam before and after two operations (currently):</p>
<ol class="arabic simple">
<li>
backup</li>
<li>
restore</li>
</ol>
<p>
Two arguments are passed to the hooks:</p>
<ol class="arabic simple">
<li>
operation: restore/backup</li>
<li>
state: pre/post</li>
</ol>
<p>
Non zero exitcodes raise a HookError is raised.</p>
</div>
<div class="section" id="advantages">
<h2>
Advantages</h2>
<p>
In one stroke, solve the clock problem and also lets advanced users define their own hooks to take care of things TKLBAM doesn&#39;t (e.g., stopping IO intensive processes before backup, encoding/decoding unsupported databases, etc.)</p>
</div>
<div class="section" id="example-fixclock-hook">
<h2>
Example fixclock hook</h2>
<pre class="literal-block">
#!/usr/bin/python
# hook that runs ntpdate before duplicity to sync clock to UTC
import os
import sys
import executil
from string import Template
NTPSERVER = os.environ.get(&quot;NTPSERVER&quot;, &quot;pool.ntp.org&quot;)
ERROR_TPL = &quot;&quot;&quot;\
##########################
## FIXCLOCK HOOK FAILED ##
##########################
Amazon S3 and Duplicity need a UTC synchronized clock so we invoked the
following command::
$COMMAND
Unfortunately, something went wrong...
$ERROR
&quot;&quot;&quot;
def fixclock():
command = &quot;ntpdate -u &quot; + NTPSERVER
try:
executil.getoutput(command)
except executil.ExecError, e:
msg = Template(ERROR_TPL).substitute(COMMAND=command,
ERROR=e.output)
print &gt;&gt; sys.stderr, msg,
sys.exit(1)
def main():
op, state = sys.argv[1:]
if op in (&#39;restore&#39;, &#39;backup&#39;) and state == &#39;pre&#39;:
fixclock()
if __name__ == &quot;__main__&quot;:
main()
</pre>
</div>http://www.turnkeylinux.org/blog/tklbam-hooks#commentsadvancedtklbamWed, 23 Nov 2011 10:47:11 +0000Liraz Siri2780 at http://www.turnkeylinux.org