| 1 | <?xml version="1.0" encoding="UTF-8" standalone="no"?> |
| 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 3 | <html xmlns="http://www.w3.org/1999/xhtml"> |
| 4 | <head> |
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| 6 | <title>Copying or moving databases</title> |
| 7 | <link rel="stylesheet" href="gettingStarted.css" type="text/css" /> |
| 8 | <meta name="generator" content="DocBook XSL Stylesheets V1.73.2" /> |
| 9 | <link rel="start" href="index.html" title="Berkeley DB Programmer's Reference Guide" /> |
| 10 | <link rel="up" href="program.html" title="Chapter 15. Programmer Notes" /> |
| 11 | <link rel="prev" href="program_cache.html" title="Disk drive caches" /> |
| 12 | <link rel="next" href="program_compatible.html" title="Compatibility with historic UNIX interfaces" /> |
| 13 | </head> |
| 14 | <body> |
| 15 | <div xmlns="" class="navheader"> |
| 16 | <div class="libver"> |
| 17 | <p>Library Version 11.2.5.3</p> |
| 18 | </div> |
| 19 | <table width="100%" summary="Navigation header"> |
| 20 | <tr> |
| 21 | <th colspan="3" align="center">Copying or moving databases</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="program_cache.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 15. |
| 26 | Programmer Notes |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="program_compatible.html">Next</a></td> |
| 29 | </tr> |
| 30 | </table> |
| 31 | <hr /> |
| 32 | </div> |
| 33 | <div class="sect1" lang="en" xml:lang="en"> |
| 34 | <div class="titlepage"> |
| 35 | <div> |
| 36 | <div> |
| 37 | <h2 class="title" style="clear: both"><a id="program_copy"></a>Copying or moving databases</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p>There are two issues with copying or moving databases: database page log |
| 42 | sequence numbers (LSNs), and database file identification strings.</p> |
| 43 | <p>Because database pages contain references to the database environment |
| 44 | log records (LSNs), databases cannot be copied or moved from one |
| 45 | transactional database environment to another without first clearing the |
| 46 | LSNs. Note that this is not a concern for non-transactional database |
| 47 | environments and applications, and can be ignored if the database is not |
| 48 | being used transactionally. Specifically, databases created and written |
| 49 | non-transactionally (for example, as part of a bulk load procedure), can |
| 50 | be copied or moved into a transactional database environment without |
| 51 | resetting the LSNs. The database's LSNs may be reset in one of three |
| 52 | ways: the application can call the <a href="../api_reference/C/envlsn_reset.html" class="olink">DB_ENV->lsn_reset()</a> method to reset the |
| 53 | LSNs in place, or a system administrator can reset the LSNs in place |
| 54 | using the <span class="bold"><strong>-r</strong></span> option to the <a href="../api_reference/C/db_load.html" class="olink">db_load</a> utility, or by |
| 55 | dumping and reloading the database (using the <a href="../api_reference/C/db_dump.html" class="olink">db_dump</a> utility and the <a href="../api_reference/C/db_load.html" class="olink">db_load</a> utility).</p> |
| 56 | <p>Because system file identification information (for example, filenames, |
| 57 | device and inode numbers, volume and file IDs, and so on) are not |
| 58 | necessarily unique or maintained across system reboots, each Berkeley DB |
| 59 | database file contains a unique 20-byte file identification bytestring. |
| 60 | When multiple processes or threads open the same database file in Berkeley DB, |
| 61 | it is this bytestring that is used to ensure the same underlying pages |
| 62 | are updated in the database environment cache, no matter which Berkeley DB |
| 63 | handle is used for the operation.</p> |
| 64 | <p>The database file identification string is not a concern when moving |
| 65 | databases, and databases may be moved or renamed without resetting the |
| 66 | identification string. However, when copying a database, you must |
| 67 | ensure there are never two databases with the same file identification |
| 68 | bytestring in the same cache at the same time. Copying databases is |
| 69 | further complicated because Berkeley DB caches do not discard cached database |
| 70 | pages when database handles are closed. Cached pages are only discarded |
| 71 | when the database is removed by calling the <a href="../api_reference/C/envremove.html" class="olink">DB_ENV->remove()</a> or |
| 72 | <a href="../api_reference/C/dbremove.html" class="olink">DB->remove()</a> methods.</p> |
| 73 | <p>Before physically copying a database file, first ensure that all |
| 74 | modified pages have been written from the cache to the backing database |
| 75 | file. This is done using the <a href="../api_reference/C/dbsync.html" class="olink">DB->sync()</a> or <a href="../api_reference/C/dbclose.html" class="olink">DB->close()</a> methods.</p> |
| 76 | <p>Before using a copy of a database file in a database environment, you |
| 77 | must ensure that all pages from any other database with the same |
| 78 | bytestring have been removed from the memory pool cache. If the |
| 79 | environment in which you will open the copy of the database has pages |
| 80 | from files with identical bytestrings to the copied database, there are |
| 81 | a few possible solutions:</p> |
| 82 | <div class="orderedlist"> |
| 83 | <ol type="1"> |
| 84 | <li>Remove the environment, either using system utilities or by calling the |
| 85 | <a href="../api_reference/C/envremove.html" class="olink">DB_ENV->remove()</a> method. Obviously, this will not allow you to access |
| 86 | both the original database and the copy of the database at the same |
| 87 | time.</li> |
| 88 | <li> |
| 89 | Create a new file that will have a new bytestring. The simplest way to |
| 90 | create a new file that will have a new bytestring is to call the |
| 91 | <a href="../api_reference/C/db_dump.html" class="olink">db_dump</a> utility to dump out the contents of the database and then use the |
| 92 | <a href="../api_reference/C/db_load.html" class="olink">db_load</a> utility to load the dumped output into a new file. This allows you |
| 93 | to access both the original and copy of the database at the same time. |
| 94 | </li> |
| 95 | <li> |
| 96 | If your database is too large to be dumped and reloaded, you can copy |
| 97 | the database by other means, and then reset the bytestring in the |
| 98 | copied database to a new bytestring. There are two ways to reset the |
| 99 | bytestring in the copy: the application can call the <a href="../api_reference/C/envfileid_reset.html" class="olink">DB_ENV->fileid_reset()</a> |
| 100 | method, or a system administrator can use the <span class="bold"><strong>-r</strong></span> option to the <a href="../api_reference/C/db_load.html" class="olink">db_load</a> utility. This allows you |
| 101 | to access both the original and copy of the database at the same time. |
| 102 | </li> |
| 103 | </ol> |
| 104 | </div> |
| 105 | </div> |
| 106 | <div class="navfooter"> |
| 107 | <hr /> |
| 108 | <table width="100%" summary="Navigation footer"> |
| 109 | <tr> |
| 110 | <td width="40%" align="left"><a accesskey="p" href="program_cache.html">Prev</a> </td> |
| 111 | <td width="20%" align="center"> |
| 112 | <a accesskey="u" href="program.html">Up</a> |
| 113 | </td> |
| 114 | <td width="40%" align="right"> <a accesskey="n" href="program_compatible.html">Next</a></td> |
| 115 | </tr> |
| 116 | <tr> |
| 117 | <td width="40%" align="left" valign="top">Disk drive caches </td> |
| 118 | <td width="20%" align="center"> |
| 119 | <a accesskey="h" href="index.html">Home</a> |
| 120 | </td> |
| 121 | <td width="40%" align="right" valign="top"> Compatibility with historic UNIX interfaces</td> |
| 122 | </tr> |
| 123 | </table> |
| 124 | </div> |
| 125 | </body> |
| 126 | </html> |