<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="recovery-from-backups">
      </a>
      9.3.2 Using Backups for Recovery
     </h3>
    </div>
   </div>
  </div>
  <p>
   Now, suppose that we have a catastrophic unexpected exit on
        Wednesday at 8 a.m. that requires recovery from backups. To
        recover, first we restore the last full backup we have (the one
        from Sunday 1 p.m.). The full backup file is just a set of SQL
        statements, so restoring it is very easy:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa49948296"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-terminal"><span class="token prompt">$&gt; </span><span class="token command">mysql</span> &lt; backup_sunday_1_PM<span class="token punctuation">.</span>sql</code></pre>
  </div>
  <p>
   At this point, the data is restored to its state as of Sunday 1
        p.m.. To restore the changes made since then, we must use the
        incremental backups; that is, the
   <code class="filename">
    gbichot2-bin.000007
   </code>
   and
   <code class="filename">
    gbichot2-bin.000008
   </code>
   binary log files. Fetch
        the files if necessary from where they were backed up, and then
        process their contents like this:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa2398624"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-terminal"><span class="token prompt">$&gt; </span><span class="token command">mysqlbinlog</span> gbichot2-bin<span class="token punctuation">.</span>000007 gbichot2-bin<span class="token punctuation">.</span>000008 | mysql</code></pre>
  </div>
  <p>
   We now have recovered the data to its state as of Tuesday 1
        p.m., but still are missing the changes from that date to the
        date of the crash. To not lose them, we would have needed to
        have the MySQL server store its MySQL binary logs into a safe
        location (RAID disks, SAN, ...) different from the place where
        it stores its data files, so that these logs were not on the
        destroyed disk. (That is, we can start the server with a
   <a class="link" href="replication-options-binary-log.html#option_mysqld_log-bin">
    <code class="option">
     --log-bin
    </code>
   </a>
   option that specifies a
        location on a different physical device from the one on which
        the data directory resides. That way, the logs are safe even if
        the device containing the directory is lost.) If we had done
        this, we would have the
   <code class="filename">
    gbichot2-bin.000009
   </code>
   file (and any subsequent files) at hand, and we could apply them
        using
   <a class="link" href="mysqlbinlog.html" title="6.6.9 mysqlbinlog — Utility for Processing Binary Log Files">
    <span class="command">
     <strong>
      mysqlbinlog
     </strong>
    </span>
   </a>
   and
   <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
    <span class="command">
     <strong>
      mysql
     </strong>
    </span>
   </a>
   to restore the most recent data changes
        with no loss up to the moment of the crash:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa7954365"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-terminal"><span class="token prompt">$&gt; </span><span class="token command">mysqlbinlog</span> gbichot2-bin<span class="token punctuation">.</span>000009 <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> | mysql</code></pre>
  </div>
  <p>
   For more information about using
   <a class="link" href="mysqlbinlog.html" title="6.6.9 mysqlbinlog — Utility for Processing Binary Log Files">
    <span class="command">
     <strong>
      mysqlbinlog
     </strong>
    </span>
   </a>
   to process binary log files, see
   <a class="xref" href="point-in-time-recovery.html" title="9.5 Point-in-Time (Incremental) Recovery">
    Section 9.5, “Point-in-Time (Incremental) Recovery”
   </a>
   .
  </p>
 </div>
 <br/>
</div>
