Debian Codegraph @

www/recovery.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>Recovering Data From A Corrupt SQLite Database</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 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 Recovering Data From A Corrupt SQLite Database
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#recovering_some_data_from_a_corrupt_sqlite_database">1. Recovering (Some) Data From A Corrupt SQLite Database</a></div>
129 <div class="fancy-toc2"><a href="#limitations">1.1. Limitations</a></div>
130 <div class="fancy-toc1"><a href="#recovery_using_the_recover_command_in_the_cli">2. Recovery Using The ".recover" Command In The CLI</a></div>
131 <div class="fancy-toc1"><a href="#building_the_recovery_api_into_an_application">3. Building The Recovery API Into An Application</a></div>
132 <div class="fancy-toc2"><a href="#source_code_files">3.1. Source Code Files</a></div>
133 <div class="fancy-toc2"><a href="#how_to_implement_recovery">3.2. How To Implement Recovery</a></div>
134 <div class="fancy-toc2"><a href="#example_implementations">3.3. Example Implementations</a></div>
135 </div>
136 </div>
137 <script>
138 function toggle_toc(){
139 var sub = document.getElementById("toc_sub")
140 var mk = document.getElementById("toc_mk")
141 if( sub.style.display!="block" ){
142 sub.style.display = "block";
143 mk.innerHTML = "&#x25bc;";
144 } else {
145 sub.style.display = "none";
146 mk.innerHTML = "&#x25ba;";
147 }
148 }
149 </script>
150 </div>
151
152
153
154
155 <h1 id="recovering_some_data_from_a_corrupt_sqlite_database"><span>1. </span>Recovering (Some) Data From A Corrupt SQLite Database</h1>
156
157 <p>
158 SQLite databases are remarkably rebust. Application faults and
159 power failures typically leave the content of the database intact.
160 However, it is possible to <a href="howtocorrupt.html">corrupt an SQLite database</a>.
161 For example, hardware malfunctions can damage the database file, or a
162 rogue process can open the database and overwrite parts of it.
163
164 </p><p>
165 Given a corrupt database file, it is sometimes desirable to try to
166 salvage as much data from the file as possible. The recovery API
167 is designed to facilitate this.
168
169 </p><h2 id="limitations"><span>1.1. </span>Limitations</h2>
170
171 <p>
172 It is sometimes possible to perfectly restore a database that has
173 gone corrupt, but that is the exception. Usually
174 the recovered database will be defective in a number of ways:
175
176 </p><ul>
177 <li><p>
178 Some content might be permanently deleted and unrecoverable.
179 This can happen, for example, if a rogue process overwrites part
180 of the database file.
181
182 </p></li><li><p>
183 Previously deleted content might reappear. Normally when SQLite
184 does a DELETE operation, it does not actually overwrite the old content
185 but instead remembers that space is available for reuse during the next
186 INSERT. If such deleted content is still in the file when a recovery
187 is attempted, it might be extracted and "resurrected".
188
189 </p></li><li><p>
190 Recovered content might be altered.
191 For example, the value stored in a particular row
192 might change from 48 to 49. Or it might change from an integer into
193 a string or blob. A value that was NULL might become an integer.
194 A string value might become a BLOB. And so forth.
195
196 </p></li><li><p>
197 Constraints may not be valid after recovery. CHECK constraints,
198 FOREIGN KEY constraints, UNIQUE constraints, type constraints on
199 <a href="stricttables.html">STRICT tables</a> - any of these might be violated in the recovered
200 database.
201
202 </p></li><li><p>
203 Content might be moved from one table into another.
204 </p></li></ul>
205
206 <p>
207 The recovery API does as good of a job as it can at restoring a database,
208 but the results will always be suspect. Sometimes (for example if the
209 corruption is restricted to indexes) the recovery will perfectly restore
210 the database content. However in other cases, the recovery will be imperfect.
211 The impact of this imperfection depends on the application. A database that
212 holds a list of bookmarks is still a list of bookmarks after recovery.
213 A few bookmarks might be missing or added or altered after recovery, but
214 the list is "fuzzy" and imperfect to begin with so adding a bit more
215 uncertainty will not be fatal to the application. But if an accounting
216 database goes corrupt and is subsequently recovered, the books might be
217 out of balance.
218
219 </p><p>
220 It is best to think of the recovery API as a salvage undertaking.
221 Recovery will extract as much usable data as it can from the wreck
222 of the old database, but some parts may be damaged beyond repair and
223 some rework and testing should be performed prior to returning the
224 recovered database to service.
225
226 </p><h1 id="recovery_using_the_recover_command_in_the_cli"><span>2. </span>Recovery Using The ".recover" Command In The CLI</h1>
227
228 <p>
229 The easiest way to manually recover a corrupt database is using
230 the <a href="cli.html">Command Line Interface</a> or "CLI" for SQLite. The CLI is a program
231 named "sqlite3". Use it to recover a corrupt database file using
232 a command similar to the following:
233
234 </p><div class="codeblock"><pre>sqlite3 corrupt.db .recover &gt;data.sql
235 </pre></div>
236
237 <p>
238 This will generate SQL text in the file named "data.sql" that can be used
239 to reconstruct the original database:
240
241 </p><div class="codeblock"><pre>sqlite3 recovered.db &lt;data.sql
242 </pre></div>
243
244 <p>
245 The ".recover" option is actually a command that is issued to the
246 CLI. That command can accept arguments. For example, by running:
247
248 </p><div class="codeblock"><pre>sqlite3 corruptdb ".recover --ignore-freelist" &gt;data.sql
249 </pre></div>
250
251 <p>
252 Notice that the ".recover" command and its arguments must be contained
253 in quotes. The following options are supported:
254
255 </p><p>
256 </p><blockquote>
257 <dt>--ignore-freelist</dt>
258 <dd><p>
259 Ignore pages of the database that appear to be part of the
260 freelist. Normally the freelist is scanned, and if it contains
261 pages that look like they have content, that content is output.
262 But if the page really is on the freelist, that can mean that
263 previously deleted information is reintroduced into the database.
264 </p></dd>
265
266 <dt>--lost-and-found <i>TABLE</i></dt>
267 <dd><p>
268 If content is found during recovery that cannot be associated
269 with a particular table, it is put into the "lost_and_found"
270 table. Use this option to change the name of the
271 "lost_and_found" table to "TABLE".
272 </p></dd>
273
274 <dt>--no-rowids</dt>
275 <dd><p>
276 If this option is provided, then rowid values that are not also
277 INTEGER PRIMARY KEY values are not extracted from the
278 corrupt database.
279 </p></dd>
280
281 </blockquote>
282
283 <h1 id="building_the_recovery_api_into_an_application"><span>3. </span>Building The Recovery API Into An Application</h1>
284
285 <h2 id="source_code_files"><span>3.1. </span>Source Code Files</h2>
286
287 <p>If you want to build the recovery API into your application, you will
288 need to add some source files to your build, above and beyond the usual
289 "sqlite3.c" and "sqlite3.h" source files. You will need:
290
291 </p><p>
292 </p><blockquote>
293 <table border="0" cellpadding="20">
294 <tr>
295 <td>
296 <a href="https://sqlite.org/src/file/ext/recover/sqlite3recover.c">sqlite3recover.c</a>
297 </td><td>
298 This is the main source file that implements the recovery API.
299 </td>
300 </tr>
301
302 <tr>
303 <td>
304 <a href="https://sqlite.org/src/file/ext/recover/sqlite3recover.h">sqlite3recover.h</a>
305 </td><td>
306 This is the header file that goes with sqlite3recover.h.
307 </td>
308 </tr>
309
310 <tr>
311 <td>
312 <a href="https://sqlite.org/src/file/ext/recover/dbdata.c">dbdata.c</a>
313 </td><td>
314 This file implements two virtual tables name "sqlite_dbdata" and
315 "sqlite_dbptr" that required by sqlite3recover.c.
316 </td>
317 </tr>
318 </table>
319 </blockquote>
320
321 <p>
322 The two C source file above need to be linked into your application in the
323 same way as "sqlite3.c" is linked in. And the header file needs to be
324 accessible to the compiler when the C files are being compiled.
325
326 </p><p>
327 Additionally, the application, or more specifically the sqlite3.c linked
328 into the application, must be compiled with the following option:
329 </p><pre>
330 -DSQLITE_ENABLE_DBPAGE_VTAB
331 </pre>
332
333 <h2 id="how_to_implement_recovery"><span>3.2. </span>How To Implement Recovery</h2>
334
335 <p>These are the basic steps needed to recover content from a corrupt
336 Database:
337
338 </p><ol>
339 <li><p>
340 Creates an sqlite3_recover handle by calling either
341 sqlite3_recover_init() or sqlite3_recover_init_sql().
342 Use sqlite3_recover_init() to store the recovered content
343 in a separate database and use sqlite3_recover_init_sql()
344 to generate SQL text that will reconstruct the database.
345
346 </p></li><li><p>
347 Make zero or more calls to sqlite3_recover_config() to set
348 options on the new sqlite3_recovery handle.
349
350 </p></li><li><p>
351 Invoke sqlite3_recover_step() repeatedly
352 until it returns something other than SQLITE_OK. If it
353 returns SQLITE_DONE, then the recovery operation completed without
354 error. If it returns some other non-SQLITE_OK value, then an error
355 has occurred. The sqlite3_recover_run() interface is also
356 available as a convenience wrapper that simply invokes
357 sqlite3_recover_step() repeatedly until it returns something other
358 than SQLITE_DONE.
359
360 </p></li><li><p>
361 Retrieves any error code and English language error message using the
362 sqlite3_recover_errcode() and sqlite3_recover_errmsg() interfaces,
363 respectively.
364
365 </p></li><li><p>
366 Invoke sqlite3_recover_finish() to destroy the sqlite3_recover object.
367 </p></li></ol>
368
369 <p>
370 Details of the interface are described in comments in the
371 <a href="https://sqlite.org/src/file/ext/recover/sqlite3recover.h">sqlite3_recover.h header file</a>.
372
373 </p><h2 id="example_implementations"><span>3.3. </span>Example Implementations</h2>
374
375
376 <p>
377 Examples of how the recovery extension is used by SQLite itself
378 can be seen at the following links:
379
380 </p><ul>
381 <li><p><a href="https://sqlite.org/src/info/30475c820dc5ab8a8?ln=999,1026">https://sqlite.org/src/info/30475c820dc5ab8a8?ln=999,1026</a>
382 </p><p>
383 An example of the recovery extension found in the
384 "fuzzcheck" testing utility in the SQLite tree.
385
386 </p></li><li><p><a href="https://sqlite.org/src/info/84bb08d8762920285f08f1c0?ln=7299,7361">https://sqlite.org/src/info/84bb08d8762920285f08f1c0?ln=7299,7361</a>
387 </p><p>
388 The code that implements the ".recover" command in the <a href="cli.html">CLI</a>.
389 </p></li></ul>
390 <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/recovery.in?m=846399393d">2023-02-28 11:22:37</a> UTC </small></i></p>
391