Debian Codegraph @

www/backup.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>SQLite Backup API</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118
119
120
121 <style>
122 /* Formatting for the blocks containing the example code */
123 pre a:visited, pre a:link { text-decoration: none ; color: #40534b }
124 pre {
125 background: #F3F3F3;
126 float: right;
127 padding: 1ex 2ex;
128 margin-left: 1em;
129 border: solid black 1px;
130 }
131 h1,h2 { clear: both }
132 </style>
133
134 <div class=fancy>
135 <h1>Using the SQLite Online Backup API</h1>
136
137 <p>
138 Historically, backups (copies) of SQLite databases have been created
139 using the following method:
140
141 <ol>
142 <li> Establish a shared lock on the database file using the SQLite API (i.e.
143 the shell tool).
144 <li> Copy the database file using an external tool (for example the unix 'cp'
145 utility or the DOS 'copy' command).
146 <li> Relinquish the shared lock on the database file obtained in step 1.
147 </ol>
148
149 <p>
150 This procedure works well in many scenarios and is usually very
151 fast. However, this technique has the following shortcomings:
152
153 <ul>
154 <li> Any database clients wishing to write to the database file while a
155 backup is being created must wait until the shared lock is
156 relinquished.
157
158 <li> It cannot be used to copy data to or from in-memory databases.
159
160 <li> If a power failure or operating system failure occurs while copying
161 the database file the backup database may be corrupted following
162 system recovery.
163 </ul>
164
165 <p>
166 The <a href="c3ref/backup_finish.html#sqlite3backupinit">Online Backup API</a> was created to
167 address these concerns. The online backup API allows the contents of
168 one database to be copied into another database file, replacing any
169 original contents of the target database. The copy operation may be
170 done incrementally, in which case the source database does not need
171 to be locked for the duration of the copy, only for the brief periods
172 of time when it is actually being read from. This allows other database
173 users to continue without excessive delays while a backup of an online
174 database is made.
175 <p>
176 The effect of completing the backup call sequence is to make the
177 destination a bit-wise identical copy of the source database as it
178 was when the copying commenced. (The destination becomes a "snapshot.")
179
180 <p>
181 The online backup API is <a href="c3ref/backup_finish.html#sqlite3backupinit">documented here</a>.
182 The remainder of this page contains two C language examples illustrating
183 common uses of the API and discussions thereof. Reading these examples
184 is no substitute for reading the API documentation!
185
186 <p>
187 Update: The <a href="lang_vacuum.html#vacuuminto">VACUUM INTO</a> command introduced in
188 SQLite version 3.27.0 (2019-02-07) can serve as an
189 alternative to the backup API.
190
191 <h2>Example 1: Loading and Saving In-Memory Databases</h2>
192
193 <pre>
194 <span style="color:blue;font-style:italic">/*</span>
195 <span style="color:blue;font-style:italic">** This function is used to load the contents of a database file on disk </span>
196 <span style="color:blue;font-style:italic">** into the "main" database of open database connection pInMemory, or</span>
197 <span style="color:blue;font-style:italic">** to save the current contents of the database opened by pInMemory into</span>
198 <span style="color:blue;font-style:italic">** a database file on disk. pInMemory is probably an in-memory database, </span>
199 <span style="color:blue;font-style:italic">** but this function will also work fine if it is not.</span>
200 <span style="color:blue;font-style:italic">**</span>
201 <span style="color:blue;font-style:italic">** Parameter zFilename points to a nul-terminated string containing the</span>
202 <span style="color:blue;font-style:italic">** name of the database file on disk to load from or save to. If parameter</span>
203 <span style="color:blue;font-style:italic">** isSave is non-zero, then the contents of the file zFilename are </span>
204 <span style="color:blue;font-style:italic">** overwritten with the contents of the database opened by pInMemory. If</span>
205 <span style="color:blue;font-style:italic">** parameter isSave is zero, then the contents of the database opened by</span>
206 <span style="color:blue;font-style:italic">** pInMemory are replaced by data loaded from the file zFilename.</span>
207 <span style="color:blue;font-style:italic">**</span>
208 <span style="color:blue;font-style:italic">** If the operation is successful, SQLITE_OK is returned. Otherwise, if</span>
209 <span style="color:blue;font-style:italic">** an error occurs, an SQLite error code is returned.</span>
210 <span style="color:blue;font-style:italic">*/</span>
211 int loadOrSaveDb(<a href="c3ref/sqlite3.html">sqlite3</a> *pInMemory, const char *zFilename, int isSave){
212 int rc; <span style="color:blue;font-style:italic">/* Function return code */</span>
213 <a href="c3ref/sqlite3.html">sqlite3</a> *pFile; <span style="color:blue;font-style:italic">/* Database connection opened on zFilename */</span>
214 <a href="c3ref/backup.html">sqlite3_backup</a> *pBackup; <span style="color:blue;font-style:italic">/* Backup object used to copy data */</span>
215 <a href="c3ref/sqlite3.html">sqlite3</a> *pTo; <span style="color:blue;font-style:italic">/* Database to copy to (pFile or pInMemory) */</span>
216 <a href="c3ref/sqlite3.html">sqlite3</a> *pFrom; <span style="color:blue;font-style:italic">/* Database to copy from (pFile or pInMemory) */</span>
217
218 <span style="color:blue;font-style:italic">/* Open the database file identified by zFilename. Exit early if this fails</span>
219 <span style="color:blue;font-style:italic">** for any reason. */</span>
220 rc = <a href="c3ref/open.html">sqlite3_open</a>(zFilename, &pFile);
221 if( rc==SQLITE_OK ){
222
223 <span style="color:blue;font-style:italic">/* If this is a 'load' operation (isSave==0), then data is copied</span>
224 <span style="color:blue;font-style:italic">** from the database file just opened to database pInMemory. </span>
225 <span style="color:blue;font-style:italic">** Otherwise, if this is a 'save' operation (isSave==1), then data</span>
226 <span style="color:blue;font-style:italic">** is copied from pInMemory to pFile. Set the variables pFrom and</span>
227 <span style="color:blue;font-style:italic">** pTo accordingly. */</span>
228 pFrom = (isSave ? pInMemory : pFile);
229 pTo = (isSave ? pFile : pInMemory);
230
231 <span style="color:blue;font-style:italic">/* Set up the backup procedure to copy from the "main" database of </span>
232 <span style="color:blue;font-style:italic">** connection pFile to the main database of connection pInMemory.</span>
233 <span style="color:blue;font-style:italic">** If something goes wrong, pBackup will be set to NULL and an error</span>
234 <span style="color:blue;font-style:italic">** code and message left in connection pTo.</span>
235 <span style="color:blue;font-style:italic">**</span>
236 <span style="color:blue;font-style:italic">** If the backup object is successfully created, call backup_step()</span>
237 <span style="color:blue;font-style:italic">** to copy data from pFile to pInMemory. Then call backup_finish()</span>
238 <span style="color:blue;font-style:italic">** to release resources associated with the pBackup object. If an</span>
239 <span style="color:blue;font-style:italic">** error occurred, then an error code and message will be left in</span>
240 <span style="color:blue;font-style:italic">** connection pTo. If no error occurred, then the error code belonging</span>
241 <span style="color:blue;font-style:italic">** to pTo is set to SQLITE_OK.</span>
242 <span style="color:blue;font-style:italic">*/</span>
243 pBackup = <a href="c3ref/backup_finish.html#sqlite3backupinit">sqlite3_backup_init</a>(pTo, "main", pFrom, "main");
244 if( pBackup ){
245 (void)<a href="c3ref/backup_finish.html#sqlite3backupstep">sqlite3_backup_step</a>(pBackup, -1);
246 (void)<a href="c3ref/backup_finish.html#sqlite3backupfinish">sqlite3_backup_finish</a>(pBackup);
247 }
248 rc = <a href="c3ref/errcode.html">sqlite3_errcode</a>(pTo);
249 }
250
251 <span style="color:blue;font-style:italic">/* Close the database connection opened on database file zFilename</span>
252 <span style="color:blue;font-style:italic">** and return the result of this function. */</span>
253 (void)<a href="c3ref/close.html">sqlite3_close</a>(pFile);
254 return rc;
255 }
256 </pre>
257
258
259 <p>
260 The C function to the right demonstrates one of the simplest,
261 and most common, uses of the backup API: loading and saving the contents
262 of an in-memory database to a file on disk. The backup API is used as
263 follows in this example:
264
265 <ol>
266 <li>Function <a href="c3ref/backup_finish.html#sqlite3backupinit">sqlite3_backup_init()</a> is called to create an <a href="c3ref/backup.html">sqlite3_backup</a>
267 object to copy data between the two databases (either from a file and
268 into the in-memory database, or vice-versa).
269 <li>Function <a href="c3ref/backup_finish.html#sqlite3backupstep">sqlite3_backup_step()</a> is called with a parameter of
270 <tt>-1</tt> to copy the entire source database to the destination.
271 <li>Function <a href="c3ref/backup_finish.html#sqlite3backupfinish">sqlite3_backup_finish()</a> is called to clean up resources
272 allocated by <a href="c3ref/backup_finish.html#sqlite3backupinit">sqlite3_backup_init()</a>.
273 </ol>
274
275 <p><b>Error handling</b>
276
277 <p>
278 If an error occurs in any of the three main backup API routines
279 then the <a href="rescode.html">error code</a> and <a href="c3ref/errcode.html">message</a> are attached to
280 the destination <a href="c3ref/sqlite3.html">database connection</a>.
281 Additionally, if
282 <a href="c3ref/backup_finish.html#sqlite3backupstep">sqlite3_backup_step()</a> encounters an error, then the <a href="rescode.html">error code</a> is returned
283 by both the <a href="c3ref/backup_finish.html#sqlite3backupstep">sqlite3_backup_step()</a> call itself, and by the subsequent call
284 to <a href="c3ref/backup_finish.html#sqlite3backupfinish">sqlite3_backup_finish()</a>. So a call to <a href="c3ref/backup_finish.html#sqlite3backupfinish">sqlite3_backup_finish()</a>
285 does not overwrite an <a href="rescode.html">error code</a> stored in the destination
286 <a href="c3ref/sqlite3.html">database connection</a> by <a href="c3ref/backup_finish.html#sqlite3backupstep">sqlite3_backup_step()</a>. This feature
287 is used in the example code to reduce amount of error handling required.
288 The return values of the <a href="c3ref/backup_finish.html#sqlite3backupstep">sqlite3_backup_step()</a> and <a href="c3ref/backup_finish.html#sqlite3backupfinish">sqlite3_backup_finish()</a>
289 calls are ignored and the error code indicating the success or failure of
290 the copy operation collected from the destination <a href="c3ref/sqlite3.html">database connection</a>
291 afterward.
292
293 <p><b>Possible Enhancements</b>
294
295 <p>
296 The implementation of this function could be enhanced in at least two ways:
297
298 <ol>
299 <li> Failing to obtain the lock on database file zFilename (an <a href="rescode.html#busy">SQLITE_BUSY</a>
300 error) could be handled, and
301 <li> Cases where the page-sizes of database pInMemory and zFilename are
302 different could be handled better.
303 </ol>
304
305 <p>
306 Since database zFilename is a file on disk, then it may be accessed
307 externally by another process. This means that when the call to
308 sqlite3_backup_step() attempts to read from or write data to it, it may
309 fail to obtain the required file lock. If this happens, this implementation
310 will fail, returning SQLITE_BUSY immediately. The solution would be to
311 register a busy-handler callback or
312 timeout with <a href="c3ref/sqlite3.html">database connection</a> pFile
313 using <a href="c3ref/busy_handler.html">sqlite3_busy_handler()</a> or <a href="c3ref/busy_timeout.html">sqlite3_busy_timeout()</a>
314 as soon as it is opened. If it fails to obtain a required lock immediately,
315 <a href="c3ref/backup_finish.html#sqlite3backupstep">sqlite3_backup_step()</a> uses any registered busy-handler callback or timeout
316 in the same way as <a href="c3ref/step.html">sqlite3_step()</a> or <a href="c3ref/exec.html">sqlite3_exec()</a> does.
317
318 <p>
319 Usually, it does not matter if the page-sizes of the source database and the
320 destination database are different before the contents of the destination
321 are overwritten. The page-size of the destination database is simply changed
322 as part of the backup operation. The exception is if the destination database
323 happens to be an in-memory database. In this case, if the page sizes
324 are not the same at the start of the backup operation, then the operation
325 fails with an SQLITE_READONLY error. Unfortunately, this could occur when
326 loading a database image from a file into an in-memory database using
327 function loadOrSaveDb().
328
329 <p>
330 However, if in-memory database pInMemory has just been opened (and is
331 therefore completely empty) before being passed to function loadOrSaveDb(),
332 then it is still possible to change its page size using an SQLite "PRAGMA
333 page_size" command. Function loadOrSaveDb() could detect this case, and
334 attempt to set the page-size of the in-memory database to the page-size
335 of database zFilename before invoking the online backup API functions.
336
337 <h2>Example 2: Online Backup of a Running Database</h2>
338
339 <pre>
340 <span style="color:blue;font-style:italic">/*</span>
341 <span style="color:blue;font-style:italic">** Perform an online backup of database pDb to the database file named</span>
342 <span style="color:blue;font-style:italic">** by zFilename. This function copies 5 database pages from pDb to</span>
343 <span style="color:blue;font-style:italic">** zFilename, then unlocks pDb and sleeps for 250 ms, then repeats the</span>
344 <span style="color:blue;font-style:italic">** process until the entire database is backed up.</span>
345 <span style="color:blue;font-style:italic">** </span>
346 <span style="color:blue;font-style:italic">** The third argument passed to this function must be a pointer to a progress</span>
347 <span style="color:blue;font-style:italic">** function. After each set of 5 pages is backed up, the progress function</span>
348 <span style="color:blue;font-style:italic">** is invoked with two integer parameters: the number of pages left to</span>
349 <span style="color:blue;font-style:italic">** copy, and the total number of pages in the source file. This information</span>
350 <span style="color:blue;font-style:italic">** may be used, for example, to update a GUI progress bar.</span>
351 <span style="color:blue;font-style:italic">**</span>
352 <span style="color:blue;font-style:italic">** While this function is running, another thread may use the database pDb, or</span>
353 <span style="color:blue;font-style:italic">** another process may access the underlying database file via a separate </span>
354 <span style="color:blue;font-style:italic">** connection.</span>
355 <span style="color:blue;font-style:italic">**</span>
356 <span style="color:blue;font-style:italic">** If the backup process is successfully completed, SQLITE_OK is returned.</span>
357 <span style="color:blue;font-style:italic">** Otherwise, if an error occurs, an SQLite error code is returned.</span>
358 <span style="color:blue;font-style:italic">*/</span>
359 int backupDb(
360 <a href="c3ref/sqlite3.html">sqlite3</a> *pDb, <span style="color:blue;font-style:italic">/* Database to back up */</span>
361 const char *zFilename, <span style="color:blue;font-style:italic">/* Name of file to back up to */</span>
362 void(*xProgress)(int, int) <span style="color:blue;font-style:italic">/* Progress function to invoke */ </span>
363 ){
364 int rc; <span style="color:blue;font-style:italic">/* Function return code */</span>
365 <a href="c3ref/sqlite3.html">sqlite3</a> *pFile; <span style="color:blue;font-style:italic">/* Database connection opened on zFilename */</span>
366 <a href="c3ref/backup.html">sqlite3_backup</a> *pBackup; <span style="color:blue;font-style:italic">/* Backup handle used to copy data */</span>
367
368 <span style="color:blue;font-style:italic">/* Open the database file identified by zFilename. */</span>
369 rc = <a href="c3ref/open.html">sqlite3_open</a>(zFilename, &pFile);
370 if( rc==SQLITE_OK ){
371
372 <span style="color:blue;font-style:italic">/* Open the <a href="c3ref/backup.html">sqlite3_backup</a> object used to accomplish the transfer */</span>
373 pBackup = <a href="c3ref/backup_finish.html#sqlite3backupinit">sqlite3_backup_init</a>(pFile, "main", pDb, "main");
374 if( pBackup ){
375
376 <span style="color:blue;font-style:italic">/* Each iteration of this loop copies 5 database pages from database</span>
377 <span style="color:blue;font-style:italic">** pDb to the backup database. If the return value of backup_step()</span>
378 <span style="color:blue;font-style:italic">** indicates that there are still further pages to copy, sleep for</span>
379 <span style="color:blue;font-style:italic">** 250 ms before repeating. */</span>
380 do {
381 rc = <a href="c3ref/backup_finish.html#sqlite3backupstep">sqlite3_backup_step</a>(pBackup, 5);
382 xProgress(
383 <a href="c3ref/backup_finish.html#sqlite3backupremaining">sqlite3_backup_remaining</a>(pBackup),
384 <a href="c3ref/backup_finish.html#sqlite3backuppagecount">sqlite3_backup_pagecount</a>(pBackup)
385 );
386 if( rc==SQLITE_OK || rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
387 <a href="c3ref/sleep.html">sqlite3_sleep</a>(250);
388 }
389 } while( rc==SQLITE_OK || rc==SQLITE_BUSY || rc==SQLITE_LOCKED );
390
391 <span style="color:blue;font-style:italic">/* Release resources allocated by backup_init(). */</span>
392 (void)<a href="c3ref/backup_finish.html#sqlite3backupfinish">sqlite3_backup_finish</a>(pBackup);
393 }
394 rc = <a href="c3ref/errcode.html">sqlite3_errcode</a>(pFile);
395 }
396
397 <span style="color:blue;font-style:italic">/* Close the database connection opened on database file zFilename</span>
398 <span style="color:blue;font-style:italic">** and return the result of this function. */</span>
399 (void)<a href="c3ref/close.html">sqlite3_close</a>(pFile);
400 return rc;
401 }
402 </pre>
403
404
405 <p>
406 The function presented in the previous example copies the entire source
407 database in one call to <a href="c3ref/backup_finish.html#sqlite3backupstep">sqlite3_backup_step()</a>. This requires holding a
408 read-lock on the source database file for the duration of the operation,
409 preventing any other database user from writing to the database. It also
410 holds the mutex associated with database pInMemory throughout the copy,
411 preventing any other thread from using it. The C function in this section,
412 designed to be called by a background thread or process for creating a
413 backup of an online database, avoids these problems using the following
414 approach:
415
416 <ol>
417 <li>Function <a href="c3ref/backup_finish.html#sqlite3backupinit">sqlite3_backup_init()</a> is called to create an <a href="c3ref/backup.html">sqlite3_backup</a>
418 object to copy data from database pDb to the backup database file
419 identified by zFilename.
420 <li>Function <a href="c3ref/backup_finish.html#sqlite3backupstep">sqlite3_backup_step()</a> is called with a parameter of 5 to
421 copy 5 pages of database pDb to the backup database (file zFilename).
422 <li>If there are still more pages to copy from database pDb, then the
423 function sleeps for 250 milliseconds (using the <a href="c3ref/sleep.html">sqlite3_sleep()</a>
424 utility) and then returns to step 2.
425 <li>Function <a href="c3ref/backup_finish.html#sqlite3backupfinish">sqlite3_backup_finish()</a> is called to clean up resources
426 allocated by <a href="c3ref/backup_finish.html#sqlite3backupinit">sqlite3_backup_init()</a>.
427 </ol>
428
429 <p><b>File and Database Connection Locking</b>
430
431 <p>
432 During the 250 ms sleep in step 3 above, no read-lock is held on the database
433 file and the mutex associated with pDb is not held. This allows other threads
434 to use <a href="c3ref/sqlite3.html">database connection</a> pDb and other connections to write to the
435 underlying database file.
436
437 <p>
438 If another thread or process writes to the source database while this
439 function is sleeping, then SQLite detects this and usually restarts the
440 backup process when sqlite3_backup_step() is next called. There is one
441 exception to this rule: If the source database is not an in-memory database,
442 and the write is performed from within the same process as the backup
443 operation and uses the same database handle (pDb), then the destination
444 database (the one opened using connection pFile) is automatically updated
445 along with the source. The backup process may then be continued after the
446 sqlite3_sleep() call returns as if nothing had happened.
447
448 <p>
449 Whether or not the backup process is restarted as a result of writes to
450 the source database mid-backup, the user can be sure that when the backup
451 operation is completed the backup database contains a consistent and
452 up-to-date snapshot of the original. However:
453
454 <ul>
455 <li> Writes to an in-memory source database, or writes to a file-based
456 source database by an external process or thread using a
457 database connection other than pDb are significantly more expensive
458 than writes made to a file-based source database using pDb (as the
459 entire backup operation must be restarted in the former two cases).
460
461 <li> If the backup process is restarted frequently enough it may never
462 run to completion and the backupDb() function may never return.
463 </ul>
464
465 <p><b>backup_remaining() and backup_pagecount()</b>
466
467 <p>
468 The backupDb() function uses the sqlite3_backup_remaining() and
469 sqlite3_backup_pagecount() functions to report its progress via the
470 user-supplied xProgress() callback. Function sqlite3_backup_remaining()
471 returns the number of pages left to copy and sqlite3_backup_pagecount()
472 returns the total number of pages in the source database (in this case
473 the database opened by pDb). So the percentage completion of the process
474 may be calculated as:
475
476 <p style="font-family:fixed;margin-left:5em">
477 Completion = 100% * (pagecount() - remaining()) / pagecount()
478
479 <p>
480 The sqlite3_backup_remaining() and sqlite3_backup_pagecount() APIs report
481 values stored by the previous call to sqlite3_backup_step(), they do not
482 actually inspect the source database file. This means that if the source
483 database is written to by another thread or process after the call to
484 sqlite3_backup_step() returns but before the values returned by
485 sqlite3_backup_remaining() and sqlite3_backup_pagecount() are used, the
486 values may be technically incorrect. This is not usually a problem.
487
488
489 <div style="clear:both"></div>
490 <p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/backup.in?m=6237da6d29">2022-04-18 02:55:50</a> UTC </small></i></p>
491