<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<article>
  <title>Backups and Restoring</title>

  <articleinfo>
    <pubdate>2004-12-15</pubdate>

    <author>
      <firstname>Joshua</firstname>

      <surname>Ferraro</surname>

      <email>jmf at kados dot org</email>
    </author>

    <revhistory>
      <revision>
        <revnumber>2.2.0</revnumber>

        <date>2004-12-13</date>

        <authorinitials>jmf</authorinitials>

        <revdescription>
          <para>Changes made by Joshua Ferraro. Original Document</para>
        </revdescription>
      </revision>
    </revhistory>
  </articleinfo>

  <para>TO DO:</para>

  <itemizedlist>
    <listitem>
      <para>Add more script examples for automation</para>
    </listitem>

    <listitem>
      <para>Discuss replication in more detail</para>
    </listitem>
  </itemizedlist>

  <section>
    <title>Using this document</title>

    <section>
      <title id="copyright">Copyright and License</title>

      <para>Copyright 2004 Joshua Ferraro <email>jmf at kados dot
      org</email></para>

      <para>This document is related to Koha and is licensed to you under the
      GNU General Public License version 2 or later (<ulink
      url="http://www.gnu.org/licenses/gpl.html">http://www.gnu.org/licenses/gpl.html</ulink>).
      Koha-related documents may be reproduced and distributed in whole or in
      part, in any medium physical or electronic, as long as this copyright
      notice is retained on all copies.</para>

      <para>You may create a derivative work and distribute it provided that
      you: <orderedlist>
          <listitem>
            <para>License the derivative work with this same license, or the
            Linux Documentation Project License (<ulink
            url="http://www.tldp.org/COPYRIGHT.html">http://www.tldp.org/COPYRIGHT.html</ulink>).
            Include a copyright notice and at least a pointer to the license
            used.</para>
          </listitem>

          <listitem>
            <para>Give due credit to previous authors and major
            contributors.</para>
          </listitem>
        </orderedlist></para>

      <para>Commercial redistribution is allowed and encouraged; however, the
      author would like to be notified of any such distributions.</para>
    </section>

    <section>
      <title id="disclaimer">Disclaimer</title>

      <para>No liability for the contents of this document can be accepted.
      Use the concepts, examples and information at your own risk. There may
      be errors and inaccuracies, that could be damaging to your system.
      Proceed with caution, and although this is highly unlikely, the
      author(s) do not take any responsibility.</para>

      <para>All copyrights are held by their by their respective owners,
      unless specifically noted otherwise. Use of a term in this document
      should not be regarded as affecting the validity of any trademark or
      service mark. Naming of particular products or brands should not be seen
      as endorsements.</para>
    </section>
  </section>

  <section>
    <title>Introduction</title>

    <para>Understanding how to properly backup and restore your
    <application>Koha</application> installation is a critical step in
    administering <application>Koha</application> successfully. Not only are
    these skills needed in an emergency, such as a hard drive failure on your
    primary production machine, but they are also useful for when you update,
    upgrade, or anytime you plan to custom modify either the filesystem or the
    database.</para>

    <para>There are three main components of <application>Koha</application>
    that must be considered when backing up and restoring: the database, the
    <application>Koha</application> filesystem, and the operating system
    customizations for the system. This document describes every step
    necessary for backing up and restoring both the database and the
    filesystem and points out some important considerations when backing up
    the operating system customizations -- these customizations often vary
    widely from system to system.</para>
  </section>

  <section>
    <title>Backing up and Restoring the Koha Database</title>

    <section>
      <title>Backups</title>

      <para>There are many ways to backup <application>MySQL</application>
      databases, or parts of databases. After reading this section I highly
      recommend reading the <application>MySQL</application> manual sections
      that discuss backing up and restoring data. You can find them online at
      <ulink
      url="http://dev.mysql.com/doc/mysql/en/index.html">http://dev.mysql.com/doc/mysql/en/index.html</ulink>.</para>

      <para>Perhaps the best way to backup your <database>Koha</database>
      database manually is the <command>mysqldump</command> application. To
      backup the whole database you can invoke it thusly: <literallayout>[root@frodo]# mysqldump --add-drop-table -uroot -p<emphasis>your password</emphasis> Koha &gt; 2004_11_30_koha.sql</literallayout></para>

      <para>Notice that this naming convention uses the date, which makes it
      easy to organize a number of backups in a directory without being
      confused about which backup corresponds to which date. It's also clear
      that it's an SQL backup. Choose your naming conventions wisely and
      you'll thank yourself when you run into trouble.</para>

      <para>Also notice the <userinput>--add-drop-table</userinput> option.
      This specifies that when you restore the data you will not append the
      data to existing data; additionally, if tables in your backup do not
      exist in the database they will be created.</para>

      <para>If you plan to work on a specific table and you'd like to back up
      just that table you can use <command>mysqldump</command> to do that as
      well. Suppose you're working on the <filename>issues</filename> table.
      You can back it up thusly:<literallayout>[root@frodo]# mysqldump --add-drop-table -uroot -p<emphasis>your password</emphasis> Koha.issues &gt; 2004_11_30_koha.issues.sql</literallayout></para>

      <para>For more information on backups using the
      <command>mysqldump</command> utility please see the
      <application>MySQL</application> manual section <ulink
      url="http://dev.mysql.com/doc/mysql/en/mysqldump.html">8.8: The
      mysqldump Database Backup Program</ulink>. For more ways to backup your
      database, the <application>MySQL</application> manual is your best
      reference.</para>

      <para>Now you can use the <command>gzip</command> program to compress
      the sql file and reduce it's overall size for easier transport:
      <literallayout>[root@frodo]# gzip 2004_11_30_koha.sql</literallayout></para>
    </section>

    <section>
      <title>Restoration</title>

      <para>To unzip your file (in case you need to restore your database) you
      simply type:<literallayout>[root@frodo]# gzip -d filename.tgz</literallayout></para>

      <para>An sql file can be used to restore data using the
      <application>MySQL</application> command-line tool. Here's an example of
      how to restore the <database>Koha</database> database using an sql file
      called <filename>koha.sql</filename>:<literallayout>[root@frodo]# mysql -uroot -p<emphasis>your password</emphasis> Koha &lt; koha.sql</literallayout></para>

      <para>To restore a single table such as <filename>issues</filename> from
      a file called <filename>koha.issues.sql</filename> you would
      type:<literallayout>[root@frodo]# mysql -uroot -p<emphasis>your password</emphasis> Koha.issues &lt; koha.issues.sql</literallayout></para>
    </section>

    <section>
      <title>Role of Replication as Backup</title>

      <para>In addition to being useful in load balancing, replication can
      serve as a realtime backup for your <application>Koha</application>
      system. You will need to manually promote a slave to serve as master,
      and you will likely need to adjust your local configurations depending
      on how you have your systems setup, but having replication in place will
      probably ensure the fastest route to recovery from a complete system
      crash. See the replication section of the
      <application>MySQL</application> manual for more details.</para>
    </section>
  </section>

  <section>
    <title>Backing up the Koha Filesystem</title>

    <para>Even if you don't plan to use the latest CVS updates for your
    installation, it might be worth your time to centralize your
    <application>Koha</application> installation filesystem to ease the backup
    and restore process. For more info on how to achieve that see the
    <citetitle>Updating Koha</citetitle> section of the
    <application>Koha</application> documentation.</para>

    <para>To backup the <application>Koha</application> installation directory
    tree you need to ensure that all your <application>Koha</application>
    files are being considered. Most of them are located by default in
    <filename>/usr/local/koha</filename>. The
    <filename>koha-httpd.conf</filename> and the
    <filename>koha.conf</filename> files are located in the
    <filename>/etc/</filename> directory.</para>

    <section>
      <title>Automating the Filesystem Backup</title>

      <para>Here's a short Perl script that I run with <command>cron</command>
      every evening. It backs up the filesystem using <command>tar</command>
      and <command>gzip</command>. You'll need to change some of the
      Configurable Variables for the script to work in your environment. Other
      than that, it's pretty much ready to go -- setup <command>cron</command>
      to run it nightly.<programlisting>#!/usr/bin/perl -w
#This script assumes that you have set up a CVS repository
#that is symlinked to your installation directories.  For
#info on setting that up see the Koha manual "Updating Koha"
#Configurable Variables
my $koha_base = "/build/websites/openils.com/koha"; #location of Koha cvs repo
my $bak_loc = "/build/backups"; #where to put backups
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
                                           = localtime(time);
$year += "1900";
my $date = "$year-$mon-$mday";
#Backup the filesystem
print "Backing up the filesystem\n";
print "Please wait ...";
`rm -rf $koha_base.bak`;        #remove the previous backup
`cp $koha_base $koha_base.bak`; #make a new backup
`tar -cvzf $bak_loc/$date-kohafilesystem.tgz $koha_base.bak`; #targzip it</programlisting></para>
    </section>
  </section>

  <section>
    <title>Backing up the Local Operating System Configuration</title>

    <para>It's best to write a script to perform all the local operating
    system configurations as you would manually perform them to ensure that in
    the event of an emergency, you can simply run the script to set everything
    the way you had it before.</para>

    <para>The list of possible custom configuration files for your system is
    unlimited, so I can just list a few common examples that you might want to
    consider including as part of your regular backup scheme:</para>

    <itemizedlist>
      <listitem>
        <para><filename>httpd.conf</filename></para>
      </listitem>

      <listitem>
        <para><filename>my.cnf</filename></para>
      </listitem>

      <listitem>
        <para>any custom <command>cron</command> jobs related to
        <application>Koha</application></para>
      </listitem>
    </itemizedlist>
  </section>

  <section>
    <title>Storing your Backup Offsite Using scp</title>

    <para>You may want to keep a copy of your database, filesystem and
    operating system customizations on your production server to ease the
    process of restoring should the need arise. However, what if your hard
    drive on that machine crashes? It's a very good idea to store your backups
    on a different machine (or two or three) and it's probably a good ideal to
    store them in a different building too.</para>

    <para>Perhaps the best way to avoid the problems inherent in physical
    backup medium such as a tape (that you may never really be sure is going
    to work anyway) is to use <command>ssh</command> and in particular
    <command>scp</command> as a part of your backup process. Why mess with
    tapes when you can securely copy a file from one system to another? You
    can invoke <command>scp</command> thusly:<literallayout>[root@frodo]# scp koha.bak jmf@sam.openils.com:/build/backups/</literallayout></para>

    <para>The <userinput>:</userinput> signifies that this is a network
    address and also allows a specific directory to be specified on the
    receiving machine -- in this case,
    <systemitem>sam.openils.com</systemitem> has a
    <filename>/build/backups</filename> directory where I store
    backups.</para>
  </section>
</article>