| 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>Database and log file archival</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="transapp.html" title="Chapter 11. Berkeley DB Transactional Data Store Applications" /> |
| 11 | <link rel="prev" href="transapp_checkpoint.html" title="Checkpoints" /> |
| 12 | <link rel="next" href="transapp_logfile.html" title="Log file removal" /> |
| 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">Database and log file archival</th> |
| 22 | </tr> |
| 23 | <tr> |
| 24 | <td width="20%" align="left"><a accesskey="p" href="transapp_checkpoint.html">Prev</a> </td> |
| 25 | <th width="60%" align="center">Chapter 11. |
| 26 | Berkeley DB Transactional Data Store Applications |
| 27 | </th> |
| 28 | <td width="20%" align="right"> <a accesskey="n" href="transapp_logfile.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="transapp_archival"></a>Database and log file archival</h2> |
| 38 | </div> |
| 39 | </div> |
| 40 | </div> |
| 41 | <p> |
| 42 | The third component of the administrative infrastructure, archival |
| 43 | for catastrophic recovery, concerns the recoverability of the |
| 44 | database in the face of catastrophic failure. Recovery after |
| 45 | catastrophic failure is intended to minimize data loss when |
| 46 | physical hardware has been destroyed — for example, loss of a |
| 47 | disk that contains databases or log files. Although the |
| 48 | application may still experience data loss in this case, it is |
| 49 | possible to minimize it. |
| 50 | </p> |
| 51 | <p> |
| 52 | First, you may want to periodically create snapshots (that is, |
| 53 | backups) of your databases to make it possible to recover from |
| 54 | catastrophic failure. These snapshots are either a standard |
| 55 | backup, which creates a consistent picture of the databases as of a |
| 56 | single instant in time; or an on-line backup (also known as a |
| 57 | <span class="emphasis"><em>hot</em></span> backup), which creates a consistent |
| 58 | picture of the databases as of an unspecified instant during the |
| 59 | period of time when the snapshot was made. The advantage of a hot |
| 60 | backup is that applications may continue to read and write the |
| 61 | databases while the snapshot is being taken. The disadvantage of a |
| 62 | hot backup is that more information must be archived, and recovery |
| 63 | based on a hot backup is to an unspecified time between the start |
| 64 | of the backup and when the backup is completed. |
| 65 | </p> |
| 66 | <p> |
| 67 | Second, after taking a snapshot, you should periodically archive |
| 68 | the log files being created in the environment. It is often |
| 69 | helpful to think of database archival in terms of full and |
| 70 | incremental filesystem backups. A snapshot is a full backup, |
| 71 | whereas the periodic archival of the current log files is an |
| 72 | incremental backup. For example, it might be reasonable to take a |
| 73 | full snapshot of a database environment weekly or monthly, and |
| 74 | archive additional log files daily. Using both the snapshot and |
| 75 | the log files, a catastrophic crash at any time can be recovered to |
| 76 | the time of the most recent log archival; a time long after the |
| 77 | original snapshot. |
| 78 | </p> |
| 79 | <p> |
| 80 | When incremental backups are implemented using this procedure, it |
| 81 | is important to know that a database copy taken prior to a bulk |
| 82 | loading event (that is, a transaction started with the |
| 83 | <a href="../api_reference/C/txnbegin.html#txnbegin_DB_TXN_BULK" class="olink">DB_TXN_BULK</a> flag) can no longer be used as the target of an |
| 84 | incremental backup. This is true because bulk loading omits |
| 85 | logging of some record insertions, so these insertions cannot be |
| 86 | rolled forward by recovery. It is recommended that a full backup |
| 87 | be scheduled following a bulk loading event. |
| 88 | </p> |
| 89 | <p> |
| 90 | To create a standard backup of your database that can be used to |
| 91 | recover from catastrophic failure, take the following steps: |
| 92 | </p> |
| 93 | <div class="orderedlist"> |
| 94 | <ol type="1"> |
| 95 | <li> |
| 96 | Commit or abort all ongoing transactions. |
| 97 | </li> |
| 98 | <li> |
| 99 | Stop writing your databases until the backup has completed. |
| 100 | Read-only operations are permitted, but no write operations and |
| 101 | no filesystem operations may be performed (for example, the |
| 102 | <a href="../api_reference/C/envremove.html" class="olink">DB_ENV->remove()</a> and <a href="../api_reference/C/dbopen.html" class="olink">DB->open()</a> methods may not be called). |
| 103 | </li> |
| 104 | <li> |
| 105 | Force an environment checkpoint (see the <a href="../api_reference/C/db_checkpoint.html" class="olink">db_checkpoint</a> utility for |
| 106 | more information). |
| 107 | </li> |
| 108 | <li> |
| 109 | <p> |
| 110 | Run the <a href="../api_reference/C/db_archive.html" class="olink">db_archive</a> utility with option <span class="bold"><strong>-s</strong></span> to identify all the database data |
| 111 | files, and copy them to a backup device such as CD-ROM, alternate |
| 112 | disk, or tape. |
| 113 | </p> |
| 114 | <p> |
| 115 | If the database files are stored in a separate directory from the |
| 116 | other Berkeley DB files, it may be simpler to archive the directory |
| 117 | itself instead of the individual files (see <a href="../api_reference/C/envset_data_dir.html" class="olink">DB_ENV->set_data_dir()</a> for |
| 118 | additional information). |
| 119 | </p> |
| 120 | <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"> |
| 121 | <h3 class="title">Note</h3> |
| 122 | <p> |
| 123 | If any of the database files did not have an open <a href="../api_reference/C/db.html" class="olink">DB</a> |
| 124 | handle during the lifetime of the current log files, |
| 125 | the <a href="../api_reference/C/db_archive.html" class="olink">db_archive</a> utility will not list them in its output. This |
| 126 | is another reason it may be simpler to use a separate |
| 127 | database file directory and archive the entire |
| 128 | directory instead of archiving only the files listed by |
| 129 | the <a href="../api_reference/C/db_archive.html" class="olink">db_archive</a> utility. |
| 130 | </p> |
| 131 | </div> |
| 132 | </li> |
| 133 | <li> |
| 134 | Run the <a href="../api_reference/C/db_archive.html" class="olink">db_archive</a> utility with option <span class="bold"><strong>-l</strong></span> to identify all the log files, |
| 135 | and copy the last one (that is, the one with the highest |
| 136 | number) to a backup device such as CD-ROM, alternate disk, or |
| 137 | tape. |
| 138 | </li> |
| 139 | </ol> |
| 140 | </div> |
| 141 | <p> |
| 142 | To create a <span class="emphasis"><em>hot</em></span> backup of your database that |
| 143 | can be used to recover from catastrophic failure, take the |
| 144 | following steps: |
| 145 | </p> |
| 146 | <div class="orderedlist"> |
| 147 | <ol type="1"> |
| 148 | <li> |
| 149 | Set the <a href="../api_reference/C/envset_flags.html#set_flags_DB_HOTBACKUP_IN_PROGRESS" class="olink">DB_HOTBACKUP_IN_PROGRESS</a> flag in the environment. |
| 150 | This affects the behavior of transactions started with the |
| 151 | <a href="../api_reference/C/txnbegin.html#txnbegin_DB_TXN_BULK" class="olink">DB_TXN_BULK</a> flag. |
| 152 | </li> |
| 153 | <li> |
| 154 | <p> |
| 155 | Archive your databases, as described in the previous step |
| 156 | #4. You do not have to halt ongoing transactions or force |
| 157 | a checkpoint. As this is a hot backup, and the databases |
| 158 | may be modified during the copy, it is critical that |
| 159 | database pages be read atomically as described by |
| 160 | <a class="xref" href="transapp_reclimit.html" title="Berkeley DB recoverability">Berkeley DB recoverability</a>. |
| 161 | </p> |
| 162 | <p> |
| 163 | Note that only UNIX based systems are known to support the |
| 164 | atomicity of reads. These systems include: Solaris, Mac OSX, |
| 165 | HPUX and various BSD based systems. Linux and Windows |
| 166 | based systems do not support atomic filesystem reads |
| 167 | directly. The XFS file system supports atomic reads |
| 168 | despite the lack of it in Linux. On systems that do not |
| 169 | support atomic file system reads, the <a href="../api_reference/C/db_hotbackup.html" class="olink">db_hotbackup</a> utility |
| 170 | should be used or a tool can be constructed |
| 171 | using the <a href="../api_reference/C/envbackup.html" class="olink">DB_ENV->backup()</a> method. Alternatively, you can |
| 172 | construct a tool using the the <a href="../api_reference/C/db_copy.html" class="olink">db_copy()</a> method. You can |
| 173 | also perform a hot backup of just a single database in your |
| 174 | environment using the <a href="../api_reference/C/envdbbackup.html" class="olink">DB_ENV->dbbackup()</a> method. |
| 175 | </p> |
| 176 | </li> |
| 177 | <li> |
| 178 | <p> |
| 179 | Archive <span class="bold"><strong>all</strong></span> of the log files. |
| 180 | The order of these two operations is required, and the database |
| 181 | files must be archived <span class="bold"><strong>before</strong></span> |
| 182 | the log files. This means that if the database files and log |
| 183 | files are in the same directory, you cannot simply archive the |
| 184 | directory; you must make sure that the correct order of |
| 185 | archival is maintained. |
| 186 | </p> |
| 187 | <p> |
| 188 | To archive your log files, run the <a href="../api_reference/C/db_archive.html" class="olink">db_archive</a> utility using the |
| 189 | <span class="bold"><strong>-l</strong></span> option to identify all the |
| 190 | database log files, and copy them to your backup media. If the |
| 191 | database log files are stored in a separate directory from the |
| 192 | other database files, it may be simpler to archive the |
| 193 | directory itself instead of the individual files (see the |
| 194 | <a href="../api_reference/C/envset_lg_dir.html" class="olink">DB_ENV->set_lg_dir()</a> method for more information). |
| 195 | </p> |
| 196 | </li> |
| 197 | <li> |
| 198 | Reset the <a href="../api_reference/C/envset_flags.html#set_flags_DB_HOTBACKUP_IN_PROGRESS" class="olink">DB_HOTBACKUP_IN_PROGRESS</a> flag. |
| 199 | </li> |
| 200 | </ol> |
| 201 | </div> |
| 202 | <p> |
| 203 | To minimize the archival space needed for log files when doing a |
| 204 | hot backup, run db_archive to identify those log files which are |
| 205 | not in use. Log files which are not in use do not need to be |
| 206 | included when creating a hot backup, and you can discard them or |
| 207 | move them aside for use with previous backups (whichever is |
| 208 | appropriate), before beginning the hot backup. |
| 209 | </p> |
| 210 | <p> |
| 211 | After completing one of these two sets of steps, the database |
| 212 | environment can be recovered from catastrophic failure (see |
| 213 | <a class="xref" href="transapp_recovery.html" title="Recovery procedures">Recovery procedures</a> |
| 214 | for more information). |
| 215 | </p> |
| 216 | <p> |
| 217 | To update either a hot or cold backup so that recovery from |
| 218 | catastrophic failure is possible to a new point in time, repeat |
| 219 | step #2 under the hot backup instructions and archive |
| 220 | <span class="bold"><strong>all</strong></span> of the log files in the |
| 221 | database environment. Each time both the database and log files |
| 222 | are copied to backup media, you may discard all previous database |
| 223 | snapshots and saved log files. Archiving additional log files does |
| 224 | not allow you to discard either previous database snapshots or log |
| 225 | files. Generally, updating a backup must be integrated with the |
| 226 | application's log file removal procedures. |
| 227 | </p> |
| 228 | <p> |
| 229 | The time to restore from catastrophic failure is a function of the |
| 230 | number of log records that have been written since the snapshot was |
| 231 | originally created. Perhaps more importantly, the more separate |
| 232 | pieces of backup media you use, the more likely it is that you will |
| 233 | have a problem reading from one of them. For these reasons, it is |
| 234 | often best to make snapshots on a regular basis. |
| 235 | </p> |
| 236 | <p> |
| 237 | <span class="bold"><strong>Obviously, the reliability of your archive |
| 238 | media will affect the safety of your data. For archival |
| 239 | safety, ensure that you have multiple copies of your database |
| 240 | backups, verify that your archival media is error-free and |
| 241 | readable, and that copies of your backups are stored |
| 242 | offsite!</strong></span> |
| 243 | </p> |
| 244 | <p> |
| 245 | The functionality provided by the <a href="../api_reference/C/db_archive.html" class="olink">db_archive</a> utility is also available |
| 246 | directly from the Berkeley DB library. The following code fragment |
| 247 | prints out a list of log and database files that need to be |
| 248 | archived: |
| 249 | </p> |
| 250 | <pre class="programlisting">void |
| 251 | log_archlist(DB_ENV *dbenv) |
| 252 | { |
| 253 | int ret; |
| 254 | char **begin, **list; |
| 255 | |
| 256 | /* Get the list of database files. */ |
| 257 | if ((ret = dbenv->log_archive(dbenv, |
| 258 | &list, DB_ARCH_ABS | DB_ARCH_DATA)) != 0) { |
| 259 | dbenv->err(dbenv, ret, "DB_ENV->log_archive: DB_ARCH_DATA"); |
| 260 | exit (1); |
| 261 | } |
| 262 | if (list != NULL) { |
| 263 | for (begin = list; *list != NULL; ++list) |
| 264 | printf("database file: %s\n", *list); |
| 265 | free (begin); |
| 266 | } |
| 267 | |
| 268 | /* Get the list of log files. */ |
| 269 | if ((ret = dbenv->log_archive(dbenv, |
| 270 | &list, DB_ARCH_ABS | DB_ARCH_LOG)) != 0) { |
| 271 | dbenv->err(dbenv, ret, "DB_ENV->log_archive: DB_ARCH_LOG"); |
| 272 | exit (1); |
| 273 | } |
| 274 | if (list != NULL) { |
| 275 | for (begin = list; *list != NULL; ++list) |
| 276 | printf("log file: %s\n", *list); |
| 277 | free (begin); |
| 278 | } |
| 279 | }</pre> |
| 280 | </div> |
| 281 | <div class="navfooter"> |
| 282 | <hr /> |
| 283 | <table width="100%" summary="Navigation footer"> |
| 284 | <tr> |
| 285 | <td width="40%" align="left"><a accesskey="p" href="transapp_checkpoint.html">Prev</a> </td> |
| 286 | <td width="20%" align="center"> |
| 287 | <a accesskey="u" href="transapp.html">Up</a> |
| 288 | </td> |
| 289 | <td width="40%" align="right"> <a accesskey="n" href="transapp_logfile.html">Next</a></td> |
| 290 | </tr> |
| 291 | <tr> |
| 292 | <td width="40%" align="left" valign="top">Checkpoints </td> |
| 293 | <td width="20%" align="center"> |
| 294 | <a accesskey="h" href="index.html">Home</a> |
| 295 | </td> |
| 296 | <td width="40%" align="right" valign="top"> Log file removal</td> |
| 297 | </tr> |
| 298 | </table> |
| 299 | </div> |
| 300 | </body> |
| 301 | </html> |