recovery.html   [plain text]


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Recovery Procedures</title>
    <link rel="stylesheet" href="gettingStarted.css" type="text/css" />
    <meta name="generator" content="DocBook XSL Stylesheets V1.62.4" />
    <link rel="home" href="index.html" title="Getting Started with Berkeley DB Transaction Processing" />
    <link rel="up" href="filemanagement.html" title="Chapter 5. Managing DB Files" />
    <link rel="previous" href="backuprestore.html" title="Backup Procedures" />
    <link rel="next" href="architectrecovery.html" title="Designing Your Application for Recovery" />
  </head>
  <body>
    <div class="navheader">
      <table width="100%" summary="Navigation header">
        <tr>
          <th colspan="3" align="center">Recovery Procedures</th>
        </tr>
        <tr>
          <td width="20%" align="left"><a accesskey="p" href="backuprestore.html">Prev</a> </td>
          <th width="60%" align="center">Chapter 5. Managing DB Files</th>
          <td width="20%" align="right"> <a accesskey="n" href="architectrecovery.html">Next</a></td>
        </tr>
      </table>
      <hr />
    </div>
    <div class="sect1" lang="en" xml:lang="en">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both"><a id="recovery"></a>Recovery Procedures</h2>
          </div>
        </div>
        <div></div>
      </div>
      <p>
           DB supports two types of recovery: 
        </p>
      <div class="itemizedlist">
        <ul type="disc">
          <li>
            <p>
                    Normal recovery, which is run when your environment is
                    opened upon application startup, examines only those
                    log records needed to bring the databases to a consistent
                    state since the last checkpoint.  Normal recovery
                    starts with any logs used by any transactions active at
                    the time of the last checkpoint, and examines all logs
                    from then to the current logs.
                </p>
          </li>
          <li>
            <p>
                    Catastrophic recovery, which is performed in the same
                    way that normal recovery is except that it examines
                    all available log files. You use catastrophic recovery
                    to restore your databases from a previously created backup.
                </p>
          </li>
        </ul>
      </div>
      <p>
            Of these two, normal recovery should be considered a routine
            matter; in fact you should run normal
            recovery whenever you start up your application.
        </p>
      <p>
            Catastrophic recovery is run whenever you have lost or
            corrupted your database files and you want to restore from a
            backup.  You also run catastrophic recovery when
            you create a hot backup
            (see <a href="hotfailover.html">Using Hot Failovers</a> for more information).
        </p>
      <div class="sect2" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title"><a id="normalrecovery"></a>Normal Recovery</h3>
            </div>
          </div>
          <div></div>
        </div>
        <p>
                Normal recovery examines the contents of your environment's
                log files, and uses this information to ensure that your
                database files are consistent relative to the
                information contained in the log files. 
            </p>
        <p>
               Normal recovery also recreates your environment's region files. 
               This has the desired effect of clearing any unreleased locks
               that your application may have held at the time of an
               unclean application shutdown.
            </p>
        <p>
                Normal recovery is run only against those log files created
                since the time of your last checkpoint. For this reason,
                your recovery time is dependent on how much data has been
                written since the last checkpoint, and therefore on how
                much log file information there is to examine. If you run
                checkpoints infrequently, then normal recovery can
                take a relatively long time.
            </p>
        <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
          <h3 class="title">Note</h3>
          <p>
                You should run normal recovery every
                time you perform application startup.
            </p>
        </div>
        <p>
                To run normal recovery:
            </p>
        <div class="itemizedlist">
          <ul type="disc">
            <li>
              <p>
                        Make sure all your environment handles are closed.
                    </p>
            </li>
            <li>
              <p>
                        Normal recovery <span class="emphasis"><em>must
                        be</em></span> single-threaded.
                    </p>
            </li>
            <li>
              <p>
                        Provide the <tt class="literal">DB_RECOVER</tt> flag when
                        you open your environment.
                    </p>
            </li>
          </ul>
        </div>
        <p>
                You can also run recovery by pausing or shutting down your
                application and using the <span><b class="command">db_recover</b></span>
                command line utility.
            </p>
        <p>
                For example:
            </p>
        <pre class="programlisting">#include "db_cxx.h"

...
                                                                                                                                  
void *checkpoint_thread(void *);

int main(void)
{
    u_int32_t env_flags = DB_CREATE     |  // If the environment does not
                                           // exist, create it.
                          DB_INIT_LOCK  |  // Initialize locking
                          DB_INIT_LOG   |  // Initialize logging
                          DB_INIT_MPOOL |  // Initialize the cache
                          DB_INIT_TXN   |  // Initialize transactions
                          DB_THREAD     |  // Free-thread the env handle
                          DB_RECOVER;      // Run normal recovery
                                                                                                                                  
    std::string envHome("/export1/testEnv");
    DbEnv myEnv(0);

    try {

        myEnv.open(envHome.c_str(), env_flags, 0);

        ...

        // All other operations are identical from here. Notice, however,
        // that we have not created any other threads of control before
        // recovery is complete. You want to run recovery for
        // the first thread in your application that opens an environment,
        // but not for any subsequent threads.  </pre>
      </div>
      <div class="sect2" lang="en" xml:lang="en">
        <div class="titlepage">
          <div>
            <div>
              <h3 class="title"><a id="catastrophicrecovery"></a>Catastrophic Recovery</h3>
            </div>
          </div>
          <div></div>
        </div>
        <p>
                Use catastrophic recovery when you are
                recovering your databases from a previously created backup.
                Note that to restore your databases from a previous backup, you
                should copy the backup to a new environment directory, and
                then run catastrophic recovery.  Failure to do so can lead to 
                the internal database structures being out of sync with your log files.
            </p>
        <p>
                Catastrophic recovery must  be run single-threaded.
            </p>
        <p>
                To run catastrophic recovery:
            </p>
        <div class="itemizedlist">
          <ul type="disc">
            <li>
              <p>
                        Shutdown all database operations.
                    </p>
            </li>
            <li>
              <p>
                        Restore the backup to an empty directory.
                    </p>
            </li>
            <li>
              <p>
                        Provide the <tt class="literal">DB_RECOVER_FATAL</tt> flag when
                        you open your environment. This environment open
                        must be single-threaded.
                    </p>
            </li>
          </ul>
        </div>
        <p>
                You can also run recovery by pausing or shutting down your
                application and using the <span><b class="command">db_recover</b></span>
                command line utility with the the <tt class="literal">-c</tt> option.
            </p>
        <p>
                Note that catastrophic recovery examines every available
                log file — not just those log files created since the
                last checkpoint as is the case for normal recovery. For this reason, 
                catastrophic recovery is likely to take longer than does
                normal recovery.
            </p>
        <p>
                For example:
            </p>
        <pre class="programlisting">#include "db_cxx.h"

...
                                                                                                                                  
void *checkpoint_thread(void *);

int main(void)
{
    u_int32_t env_flags = DB_CREATE     |  // If the environment does not
                                           // exist, create it.
                          DB_INIT_LOCK  |  // Initialize locking
                          DB_INIT_LOG   |  // Initialize logging
                          DB_INIT_MPOOL |  // Initialize the cache
                          DB_INIT_TXN   |  // Initialize transactions
                          DB_THREAD     |  // Free-thread the env handle
                          <b class="userinput"><tt>DB_RECOVER_FATAL;   // Run catastrophic recovery</tt></b>
                                                                                                                                  
    std::string envHome("/export1/testEnv");
    DbEnv myEnv(0);

    try {

        myEnv.open(envHome.c_str(), env_flags, 0);

        ...

        // All other operations are identical from here. Notice, however,
        // that we have not created any other threads of control before
        // recovery is complete. You want to run recovery for
        // the first thread in your application that opens an environment,
        // but not for any subsequent threads.  </pre>
      </div>
    </div>
    <div class="navfooter">
      <hr />
      <table width="100%" summary="Navigation footer">
        <tr>
          <td width="40%" align="left"><a accesskey="p" href="backuprestore.html">Prev</a> </td>
          <td width="20%" align="center">
            <a accesskey="u" href="filemanagement.html">Up</a>
          </td>
          <td width="40%" align="right"> <a accesskey="n" href="architectrecovery.html">Next</a></td>
        </tr>
        <tr>
          <td width="40%" align="left" valign="top">Backup Procedures </td>
          <td width="20%" align="center">
            <a accesskey="h" href="index.html">Home</a>
          </td>
          <td width="40%" align="right" valign="top"> Designing Your Application for Recovery</td>
        </tr>
      </table>
    </div>
  </body>
</html>