summaryrefslogtreecommitdiff
path: root/db/docs/ref/transapp/archival.html
blob: 12bb0490d13711eee6e6ac40f76afd6340091b56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<!--$Id: archival.so,v 10.54 2004/04/14 16:18:14 bostic Exp $-->
<!--Copyright 1997-2004 by Sleepycat Software, Inc.-->
<!--All rights reserved.-->
<!--See the file LICENSE for redistribution information.-->
<html>
<head>
<title>Berkeley DB Reference Guide: Database and log file archival</title>
<meta name="description" content="Berkeley DB: An embedded database programmatic toolkit.">
<meta name="keywords" content="embedded,database,programmatic,toolkit,btree,hash,hashing,transaction,transactions,locking,logging,access method,access methods,Java,C,C++">
</head>
<body bgcolor=white>
<a name="2"><!--meow--></a><a name="3"><!--meow--></a>
<table width="100%"><tr valign=top>
<td><h3><dl><dt>Berkeley DB Reference Guide:<dd>Berkeley DB Transactional Data Store Applications</dl></h3></td>
<td align=right><a href="../transapp/checkpoint.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../transapp/logfile.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p>
<h3 align=center>Database and log file archival</h3>
<p>The third component of the administrative infrastructure, archival for
catastrophic recovery, concerns the recoverability of the database in
the face of catastrophic failure.  Recovery after catastrophic failure
is intended to minimize data loss when physical hardware has been
destroyed -- for example, loss of a disk that contains databases or log
files.  Although the application may still experience data loss in this
case, it is possible to minimize it.</p>
<p>First, you may want to periodically create snapshots (that is, backups)
of your databases to make it possible to recover from catastrophic
failure.  These snapshots are either a standard backup, which creates a
consistent picture of the databases as of a single instant in time; or
an on-line backup (also known as a <i>hot</i> backup), which creates
a consistent picture of the databases as of an unspecified instant
during the period of time when the snapshot was made.  The advantage of
a hot backup is that applications may continue to read and write the
databases while the snapshot is being taken.  The disadvantage of a hot
backup is that more information must be archived, and recovery based on
a hot backup is to an unspecified time between the start of the backup
and when the backup is completed.</p>
<p>Second, after taking a snapshot, you should periodically archive the
log files being created in the environment.  It is often helpful to
think of database archival in terms of full and incremental filesystem
backups.  A snapshot is a full backup, whereas the periodic archival of
the current log files is an incremental backup.  For example, it might
be reasonable to take a full snapshot of a database environment weekly
or monthly, and archive additional log files daily.  Using both the
snapshot and the log files, a catastrophic crash at any time can be
recovered to the time of the most recent log archival; a time long after
the original snapshot.</p>
<p>To create a standard backup of your database that can be used to recover
from catastrophic failure, take the following steps:</p>
<ol>
<p><li>Commit or abort all ongoing transactions.
<p><li>Stop writing your databases until the backup has completed.  Read-only
operations are permitted, but no write operations and no filesystem
operations may be performed (for example, the <a href="../../api_c/env_remove.html">DB_ENV-&gt;remove</a> and
<a href="../../api_c/db_open.html">DB-&gt;open</a> methods may not be called).
<p><li>Force an environment checkpoint (see <a href="../../utility/db_checkpoint.html">db_checkpoint</a> for more
information).
<p><li>Run <a href="../../utility/db_archive.html">db_archive</a> <b>-s</b> to identify all the database data
files, and copy them to a backup device such as CD-ROM, alternate disk,
or tape.
<p>If the database files are stored in a separate directory from the other
Berkeley DB files, it may be simpler to archive the directory itself instead
of the individual files (see <a href="../../api_c/env_set_data_dir.html">DB_ENV-&gt;set_data_dir</a> for additional
information).  <b>Note: if any of the database files did not have
an open <a href="../../api_c/db_class.html">DB</a> handle during the lifetime of the current log files,
<a href="../../utility/db_archive.html">db_archive</a> will not list them in its output!</b>  This is another
reason it may be simpler to use a separate database file directory and
archive the entire directory instead of archiving only the files listed
by <a href="../../utility/db_archive.html">db_archive</a>.</p>
<p><li>Run <a href="../../utility/db_archive.html">db_archive</a> <b>-l</b> to identify all the log files,
and copy the last one (that is, the one with the highest number) to a
backup device such as CD-ROM, alternate disk, or tape.
</ol>
<a name="4"><!--meow--></a>
<p>To create a <i>hot</i> backup of your database that can be used to
recover from catastrophic failure, take the following steps:</p>
<ol>
<p><li>Archive your databases, as described in the previous step #4.
You do not have to halt ongoing transactions or force a checkpoint.  As
this is a hot backup, and the databases may be modified during the copy,
the utility you use to copy the databases must read database pages
atomically (as described by <a href="../../ref/transapp/reclimit.html">Berkeley DB recoverability</a>).
<p><li>Archive <b>all</b> of the log files.  The order of these two operations
is required, and the database files must be archived <b>before</b> the
log files.  This means that if the database files and log files are in
the same directory, you cannot simply archive the directory; you must
make sure that the correct order of archival is maintained.
<p>To archive your log files, run the <a href="../../utility/db_archive.html">db_archive</a> utility using
the <b>-l</b> option to identify all the database log files, and
copy them to your backup media.  If the database log files are stored
in a separate directory from the other database files, it may be simpler
to archive the directory itself instead of the individual files (see
the <a href="../../api_c/env_set_lg_dir.html">DB_ENV-&gt;set_lg_dir</a> method for more information).</p>
</ol>
<p>To minimize the archival space needed for log files when doing a hot
backup, run db_archive to identify those log files which are not in use.
Log files which are not in use do not need to be included when creating
a hot backup, and you can discard them or move them aside for use with
previous backups (whichever is appropriate), before beginning the hot
backup.</p>
<p>After completing one of these two sets of steps, the database
environment can be recovered from catastrophic failure (see
<a href="recovery.html">Recovery procedures</a> for more information).</p>
<p>To update either a hot or cold backup so that recovery from catastrophic
failure is possible to a new point in time, repeat step #2 under the
hot backup instructions and archive <b>all</b> of the log files in the
database environment.  Each time both the database and log files are
copied to backup media, you may discard all previous database snapshots
and saved log files.  Archiving additional log files does not allow you
to discard either previous database snapshots or log files.  Generally,
updating a backup must be integrated with the application's log file
removal procedures.</p>
<p>The time to restore from catastrophic failure is a function of the
number of log records that have been written since the snapshot was
originally created.  Perhaps more importantly, the more separate pieces
of backup media you use, the more likely it is that you will have a
problem reading from one of them.  For these reasons, it is often best
to make snapshots on a regular basis.</p>
<p><b>Obviously, the reliability of your archive media will affect the safety
of your data.  For archival safety, ensure that you have multiple copies
of your database backups, verify that your archival media is error-free
and readable, and that copies of your backups are stored offsite!</b></p>
<p>The functionality provided by the <a href="../../utility/db_archive.html">db_archive</a> utility is also
available directly from the Berkeley DB library.  The following code fragment
prints out a list of log and database files that need to be archived:</p>
<blockquote><pre>void
log_archlist(DB_ENV *dbenv)
{
	int ret;
	char **begin, **list;
<p>
	/* Get the list of database files. */
	if ((ret = dbenv-&gt;log_archive(dbenv,
	    &list, DB_ARCH_ABS | DB_ARCH_DATA)) != 0) {
		dbenv-&gt;err(dbenv, ret, "DB_ENV-&gt;log_archive: DB_ARCH_DATA");
		exit (1);
	}
	if (list != NULL) {
		for (begin = list; *list != NULL; ++list)
			printf("database file: %s\n", *list);
		free (begin);
	}
<p>
	/* Get the list of log files. */
	if ((ret = dbenv-&gt;log_archive(dbenv,
	    &list, DB_ARCH_ABS | DB_ARCH_LOG)) != 0) {
		dbenv-&gt;err(dbenv, ret, "DB_ENV-&gt;log_archive: DB_ARCH_LOG");
		exit (1);
	}
	if (list != NULL) {
		for (begin = list; *list != NULL; ++list)
			printf("log file: %s\n", *list);
		free (begin);
	}
}</pre></blockquote>
<table width="100%"><tr><td><br></td><td align=right><a href="../transapp/checkpoint.html"><img src="../../images/prev.gif" alt="Prev"></a><a href="../toc.html"><img src="../../images/ref.gif" alt="Ref"></a><a href="../transapp/logfile.html"><img src="../../images/next.gif" alt="Next"></a>
</td></tr></table>
<p><font size=1><a href="../../sleepycat/legal.html">Copyright (c) 1996-2004</a> <a href="http://www.sleepycat.com">Sleepycat Software, Inc.</a> - All rights reserved.</font>
</body>
</html>