summaryrefslogtreecommitdiffstats
path: root/www/c3ref/backup_finish.html
blob: bdd1acd62bab8ea049e2f55700d4eb50d519ff8b (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<!DOCTYPE html>
<html><head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link href="../sqlite.css" rel="stylesheet">
<title>Online Backup API.</title>
<!-- path=../ -->
</head>
<body>
<div class=nosearch>
<a href="../index.html">
<img class="logo" src="../images/sqlite370_banner.gif" alt="SQLite" border="0">
</a>
<div><!-- IE hack to prevent disappearing logo --></div>
<div class="tagline desktoponly">
Small. Fast. Reliable.<br>Choose any three.
</div>
<div class="menu mainmenu">
<ul>
<li><a href="../index.html">Home</a>
<li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
<li class='wideonly'><a href='../about.html'>About</a>
<li class='desktoponly'><a href="../docs.html">Documentation</a>
<li class='desktoponly'><a href="../download.html">Download</a>
<li class='wideonly'><a href='../copyright.html'>License</a>
<li class='desktoponly'><a href="../support.html">Support</a>
<li class='desktoponly'><a href="../prosupport.html">Purchase</a>
<li class='search' id='search_menubutton'>
<a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
</ul>
</div>
<div class="menu submenu" id="submenu">
<ul>
<li><a href='../about.html'>About</a>
<li><a href='../docs.html'>Documentation</a>
<li><a href='../download.html'>Download</a>
<li><a href='../support.html'>Support</a>
<li><a href='../prosupport.html'>Purchase</a>
</ul>
</div>
<div class="searchmenu" id="searchmenu">
<form method="GET" action="../search">
<select name="s" id="searchtype">
<option value="d">Search Documentation</option>
<option value="c">Search Changelog</option>
</select>
<input type="text" name="q" id="searchbox" value="">
<input type="submit" value="Go">
</form>
</div>
</div>
<script>
function toggle_div(nm) {
var w = document.getElementById(nm);
if( w.style.display=="block" ){
w.style.display = "none";
}else{
w.style.display = "block";
}
}
function toggle_search() {
var w = document.getElementById("searchmenu");
if( w.style.display=="block" ){
w.style.display = "none";
} else {
w.style.display = "block";
setTimeout(function(){
document.getElementById("searchbox").focus()
}, 30);
}
}
function div_off(nm){document.getElementById(nm).style.display="none";}
window.onbeforeunload = function(e){div_off("submenu");}
/* Disable the Search feature if we are not operating from CGI, since */
/* Search is accomplished using CGI and will not work without it. */
if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
document.getElementById("search_menubutton").style.display = "none";
}
/* Used by the Hide/Show button beside syntax diagrams, to toggle the */
function hideorshow(btn,obj){
var x = document.getElementById(obj);
var b = document.getElementById(btn);
if( x.style.display!='none' ){
x.style.display = 'none';
b.innerHTML='show';
}else{
x.style.display = '';
b.innerHTML='hide';
}
return false;
}
var antiRobot = 0;
function antiRobotGo(){
if( antiRobot!=3 ) return;
antiRobot = 7;
var j = document.getElementById("mtimelink");
if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
}
function antiRobotDefense(){
document.body.onmousedown=function(){
antiRobot |= 2;
antiRobotGo();
document.body.onmousedown=null;
}
document.body.onmousemove=function(){
antiRobot |= 2;
antiRobotGo();
document.body.onmousemove=null;
}
setTimeout(function(){
antiRobot |= 1;
antiRobotGo();
}, 100)
antiRobotGo();
}
antiRobotDefense();
</script>
<!-- keywords: sqlite3_backup_finish sqlite3_backup_init sqlite3_backup_pagecount sqlite3_backup_remaining sqlite3_backup_step -->
<div class=nosearch>
<a href="../c3ref/intro.html"><h2>SQLite C Interface</h2></a>
<h2>Online Backup API.</h2>
</div>
<blockquote><pre>
sqlite3_backup *sqlite3_backup_init(
  sqlite3 *pDest,                        /* Destination database handle */
  const char *zDestName,                 /* Destination database name */
  sqlite3 *pSource,                      /* Source database handle */
  const char *zSourceName                /* Source database name */
);
int sqlite3_backup_step(sqlite3_backup *p, int nPage);
int sqlite3_backup_finish(sqlite3_backup *p);
int sqlite3_backup_remaining(sqlite3_backup *p);
int sqlite3_backup_pagecount(sqlite3_backup *p);
</pre></blockquote>
<p>
The backup API copies the content of one database into another.
It is useful either for creating backups of databases or
for copying in-memory databases to or from persistent files.</p>

<p>See Also: <a href="../backup.html">Using the SQLite Online Backup API</a></p>

<p>SQLite holds a write transaction open on the destination database file
for the duration of the backup operation.
The source database is read-locked only while it is being read;
it is not locked continuously for the entire backup operation.
Thus, the backup may be performed on a live source database without
preventing other database connections from
reading or writing to the source database while the backup is underway.</p>

<p>To perform a backup operation:
<ol>
<li><b>sqlite3_backup_init()</b> is called once to initialize the
backup,
<li><b>sqlite3_backup_step()</b> is called one or more times to transfer
the data between the two databases, and finally
<li><b>sqlite3_backup_finish()</b> is called to release all resources
associated with the backup operation.
</ol>
There should be exactly one call to sqlite3_backup_finish() for each
successful call to sqlite3_backup_init().</p>

<p><a name="sqlite3backupinit"></a>
 <b>sqlite3_backup_init()</b></p>

<p>The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
<a href="../c3ref/sqlite3.html">database connection</a> associated with the destination database
and the database name, respectively.
The database name is "main" for the main database, "temp" for the
temporary database, or the name specified after the AS keyword in
an <a href="../lang_attach.html">ATTACH</a> statement for an attached database.
The S and M arguments passed to
sqlite3_backup_init(D,N,S,M) identify the <a href="../c3ref/sqlite3.html">database connection</a>
and database name of the source database, respectively.
The source and destination <a href="../c3ref/sqlite3.html">database connections</a> (parameters S and D)
must be different or else sqlite3_backup_init(D,N,S,M) will fail with
an error.</p>

<p>A call to sqlite3_backup_init() will fail, returning NULL, if
there is already a read or read-write transaction open on the
destination database.</p>

<p>If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
returned and an error code and error message are stored in the
destination <a href="../c3ref/sqlite3.html">database connection</a> D.
The error code and message for the failed call to sqlite3_backup_init()
can be retrieved using the <a href="../c3ref/errcode.html">sqlite3_errcode()</a>, <a href="../c3ref/errcode.html">sqlite3_errmsg()</a>, and/or
<a href="../c3ref/errcode.html">sqlite3_errmsg16()</a> functions.
A successful call to sqlite3_backup_init() returns a pointer to an
<a href="../c3ref/backup.html">sqlite3_backup</a> object.
The <a href="../c3ref/backup.html">sqlite3_backup</a> object may be used with the sqlite3_backup_step() and
sqlite3_backup_finish() functions to perform the specified backup
operation.</p>

<p><a name="sqlite3backupstep"></a>
 <b>sqlite3_backup_step()</b></p>

<p>Function sqlite3_backup_step(B,N) will copy up to N pages between
the source and destination databases specified by <a href="../c3ref/backup.html">sqlite3_backup</a> object B.
If N is negative, all remaining source pages are copied.
If sqlite3_backup_step(B,N) successfully copies N pages and there
are still more pages to be copied, then the function returns <a href="../rescode.html#ok">SQLITE_OK</a>.
If sqlite3_backup_step(B,N) successfully finishes copying all pages
from source to destination, then it returns <a href="../rescode.html#done">SQLITE_DONE</a>.
If an error occurs while running sqlite3_backup_step(B,N),
then an <a href="../rescode.html">error code</a> is returned. As well as <a href="../rescode.html#ok">SQLITE_OK</a> and
<a href="../rescode.html#done">SQLITE_DONE</a>, a call to sqlite3_backup_step() may return <a href="../rescode.html#readonly">SQLITE_READONLY</a>,
<a href="../rescode.html#nomem">SQLITE_NOMEM</a>, <a href="../rescode.html#busy">SQLITE_BUSY</a>, <a href="../rescode.html#locked">SQLITE_LOCKED</a>, or an
<a href="../rescode.html#ioerr_access">SQLITE_IOERR_XXX</a> extended error code.</p>

<p>The sqlite3_backup_step() might return <a href="../rescode.html#readonly">SQLITE_READONLY</a> if
<ol>
<li> the destination database was opened read-only, or
<li> the destination database is using write-ahead-log journaling
and the destination and source page sizes differ, or
<li> the destination database is an in-memory database and the
destination and source page sizes differ.
</ol></p>

<p>If sqlite3_backup_step() cannot obtain a required file-system lock, then
the <a href="../c3ref/busy_handler.html">busy-handler function</a>
is invoked (if one is specified). If the
busy-handler returns non-zero before the lock is available, then
<a href="../rescode.html#busy">SQLITE_BUSY</a> is returned to the caller. In this case the call to
sqlite3_backup_step() can be retried later. If the source
<a href="../c3ref/sqlite3.html">database connection</a>
is being used to write to the source database when sqlite3_backup_step()
is called, then <a href="../rescode.html#locked">SQLITE_LOCKED</a> is returned immediately. Again, in this
case the call to sqlite3_backup_step() can be retried later on. If
<a href="../rescode.html#ioerr_access">SQLITE_IOERR_XXX</a>, <a href="../rescode.html#nomem">SQLITE_NOMEM</a>, or
<a href="../rescode.html#readonly">SQLITE_READONLY</a> is returned, then
there is no point in retrying the call to sqlite3_backup_step(). These
errors are considered fatal.  The application must accept
that the backup operation has failed and pass the backup operation handle
to the sqlite3_backup_finish() to release associated resources.</p>

<p>The first call to sqlite3_backup_step() obtains an exclusive lock
on the destination file. The exclusive lock is not released until either
sqlite3_backup_finish() is called or the backup operation is complete
and sqlite3_backup_step() returns <a href="../rescode.html#done">SQLITE_DONE</a>.  Every call to
sqlite3_backup_step() obtains a <a href="../lockingv3.html#shared_lock">shared lock</a> on the source database that
lasts for the duration of the sqlite3_backup_step() call.
Because the source database is not locked between calls to
sqlite3_backup_step(), the source database may be modified mid-way
through the backup process.  If the source database is modified by an
external process or via a database connection other than the one being
used by the backup operation, then the backup will be automatically
restarted by the next call to sqlite3_backup_step(). If the source
database is modified by the using the same database connection as is used
by the backup operation, then the backup database is automatically
updated at the same time.</p>

<p><a name="sqlite3backupfinish"></a>
 <b>sqlite3_backup_finish()</b></p>

<p>When sqlite3_backup_step() has returned <a href="../rescode.html#done">SQLITE_DONE</a>, or when the
application wishes to abandon the backup operation, the application
should destroy the <a href="../c3ref/backup.html">sqlite3_backup</a> by passing it to sqlite3_backup_finish().
The sqlite3_backup_finish() interfaces releases all
resources associated with the <a href="../c3ref/backup.html">sqlite3_backup</a> object.
If sqlite3_backup_step() has not yet returned <a href="../rescode.html#done">SQLITE_DONE</a>, then any
active write-transaction on the destination database is rolled back.
The <a href="../c3ref/backup.html">sqlite3_backup</a> object is invalid
and may not be used following a call to sqlite3_backup_finish().</p>

<p>The value returned by sqlite3_backup_finish is <a href="../rescode.html#ok">SQLITE_OK</a> if no
sqlite3_backup_step() errors occurred, regardless or whether or not
sqlite3_backup_step() completed.
If an out-of-memory condition or IO error occurred during any prior
sqlite3_backup_step() call on the same <a href="../c3ref/backup.html">sqlite3_backup</a> object, then
sqlite3_backup_finish() returns the corresponding <a href="../rescode.html">error code</a>.</p>

<p>A return of <a href="../rescode.html#busy">SQLITE_BUSY</a> or <a href="../rescode.html#locked">SQLITE_LOCKED</a> from sqlite3_backup_step()
is not a permanent error and does not affect the return value of
sqlite3_backup_finish().</p>

<p><a name="sqlite3backupremaining"></a>
 <a name="sqlite3backuppagecount"></a>

<b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b></p>

<p>The sqlite3_backup_remaining() routine returns the number of pages still
to be backed up at the conclusion of the most recent sqlite3_backup_step().
The sqlite3_backup_pagecount() routine returns the total number of pages
in the source database at the conclusion of the most recent
sqlite3_backup_step().
The values returned by these functions are only updated by
sqlite3_backup_step(). If the source database is modified in a way that
changes the size of the source database or the number of pages remaining,
those changes are not reflected in the output of sqlite3_backup_pagecount()
and sqlite3_backup_remaining() until after the next
sqlite3_backup_step().</p>

<p><b>Concurrent Usage of Database Handles</b></p>

<p>The source <a href="../c3ref/sqlite3.html">database connection</a> may be used by the application for other
purposes while a backup operation is underway or being initialized.
If SQLite is compiled and configured to support threadsafe database
connections, then the source database connection may be used concurrently
from within other threads.</p>

<p>However, the application must guarantee that the destination
<a href="../c3ref/sqlite3.html">database connection</a> is not passed to any other API (by any thread) after
sqlite3_backup_init() is called and before the corresponding call to
sqlite3_backup_finish().  SQLite does not currently check to see
if the application incorrectly accesses the destination <a href="../c3ref/sqlite3.html">database connection</a>
and so no error code is reported, but the operations may malfunction
nevertheless.  Use of the destination database connection while a
backup is in progress might also cause a mutex deadlock.</p>

<p>If running in <a href="../sharedcache.html">shared cache mode</a>, the application must
guarantee that the shared cache used by the destination database
is not accessed while the backup is running. In practice this means
that the application must guarantee that the disk file being
backed up to is not accessed by any connection within the process,
not just the specific connection that was passed to sqlite3_backup_init().</p>

<p>The <a href="../c3ref/backup.html">sqlite3_backup</a> object itself is partially threadsafe. Multiple
threads may safely make multiple concurrent calls to sqlite3_backup_step().
However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
APIs are not strictly speaking threadsafe. If they are invoked at the
same time as another thread is invoking sqlite3_backup_step() it is
possible that they return invalid values.
</p><p>See also lists of
  <a href="../c3ref/objlist.html">Objects</a>,
  <a href="../c3ref/constlist.html">Constants</a>, and
  <a href="../c3ref/funclist.html">Functions</a>.</p>