1 .Dd April 29, 2026 2 .Dt SQLITE3_BACKUP_INIT 3 3 .Os 4 .Sh NAME 5 .Nm sqlite3_backup_init , 6 .Nm sqlite3_backup_step , 7 .Nm sqlite3_backup_finish , 8 .Nm sqlite3_backup_remaining , 9 .Nm sqlite3_backup_pagecount 10 .Nd online backup API 11 .Sh SYNOPSIS 12 .In sqlite3.h 13 .Ft sqlite3_backup * 14 .Fo sqlite3_backup_init 15 .Fa "sqlite3 *pDest" 16 .Fa "const char *zDestName" 17 .Fa "sqlite3 *pSource" 18 .Fa "const char *zSourceName" 19 .Fc 20 .Ft int 21 .Fo sqlite3_backup_step 22 .Fa "sqlite3_backup *p" 23 .Fa "int nPage" 24 .Fc 25 .Ft int 26 .Fo sqlite3_backup_finish 27 .Fa "sqlite3_backup *p" 28 .Fc 29 .Ft int 30 .Fo sqlite3_backup_remaining 31 .Fa "sqlite3_backup *p" 32 .Fc 33 .Ft int 34 .Fo sqlite3_backup_pagecount 35 .Fa "sqlite3_backup *p" 36 .Fc 37 .Sh DESCRIPTION 38 The backup API copies the content of one database into another. 39 It is useful either for creating backups of databases or for copying 40 in-memory databases to or from persistent files. 41 .Pp 42 SQLite holds a write transaction open on the destination database file 43 for the duration of the backup operation. 44 The source database is read-locked only while it is being read; it 45 is not locked continuously for the entire backup operation. 46 Thus, the backup may be performed on a live source database without 47 preventing other database connections from reading or writing to the 48 source database while the backup is underway. 49 .Pp 50 To perform a backup operation: 51 .Bl -enum 52 .It 53 \fBsqlite3_backup_init()\fP is called once to initialize the backup, 54 .It 55 \fBsqlite3_backup_step()\fP is called one or more times to transfer the data 56 between the two databases, and finally 57 .It 58 \fBsqlite3_backup_finish()\fP is called to release all resources associated 59 with the backup operation. 60 .El 61 .Pp 62 There should be exactly one call to sqlite3_backup_finish() for each 63 successful call to sqlite3_backup_init(). 64 .Pp 65 \fBsqlite3_backup_init()\fP 66 .Pp 67 The D and N arguments to sqlite3_backup_init(D,N,S,M) are the database connection 68 associated with the destination database and the database name, respectively. 69 The database name is "main" for the main database, "temp" for the temporary 70 database, or the name specified after the AS keyword in an ATTACH 71 statement for an attached database. 72 The S and M arguments passed to sqlite3_backup_init(D,N,S,M) identify 73 the database connection and database name of the 74 source database, respectively. 75 The source and destination database connections 76 (parameters S and D) must be different or else sqlite3_backup_init(D,N,S,M) 77 will fail with an error. 78 .Pp 79 A call to sqlite3_backup_init() will fail, returning NULL, if there 80 is already a read or read-write transaction open on the destination 81 database. 82 .Pp 83 If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is 84 returned and an error code and error message are stored in the destination 85 database connection D. 86 The error code and message for the failed call to sqlite3_backup_init() 87 can be retrieved using the 88 .Fn sqlite3_errcode , 89 .Fn sqlite3_errmsg , 90 and/or 91 .Fn sqlite3_errmsg16 92 functions. 93 A successful call to sqlite3_backup_init() returns a pointer to an 94 sqlite3_backup object. 95 The sqlite3_backup object may be used with the sqlite3_backup_step() 96 and sqlite3_backup_finish() functions to perform the specified backup 97 operation. 98 .Pp 99 \fBsqlite3_backup_step()\fP 100 .Pp 101 Function sqlite3_backup_step(B,N) will copy up to N pages between the 102 source and destination databases specified by sqlite3_backup 103 object B. 104 If N is negative, all remaining source pages are copied. 105 If sqlite3_backup_step(B,N) successfully copies N pages and there are 106 still more pages to be copied, then the function returns SQLITE_OK. 107 If sqlite3_backup_step(B,N) successfully finishes copying all pages 108 from source to destination, then it returns SQLITE_DONE. 109 If an error occurs while running sqlite3_backup_step(B,N), then an 110 error code is returned. 111 As well as SQLITE_OK and SQLITE_DONE, a call to 112 sqlite3_backup_step() may return SQLITE_READONLY, SQLITE_NOMEM, 113 SQLITE_BUSY, SQLITE_LOCKED, or an SQLITE_IOERR_XXX 114 extended error code. 115 .Pp 116 The sqlite3_backup_step() might return SQLITE_READONLY 117 if 118 .Bl -enum 119 .It 120 the destination database was opened read-only, or 121 .It 122 the destination database is using write-ahead-log journaling and the 123 destination and source page sizes differ, or 124 .It 125 the destination database is an in-memory database and the destination 126 and source page sizes differ. 127 .El 128 .Pp 129 If sqlite3_backup_step() cannot obtain a required file-system lock, 130 then the busy-handler function is invoked (if 131 one is specified). 132 If the busy-handler returns non-zero before the lock is available, 133 then SQLITE_BUSY is returned to the caller. 134 In this case the call to sqlite3_backup_step() can be retried later. 135 If the source database connection is being used 136 to write to the source database when sqlite3_backup_step() is called, 137 then SQLITE_LOCKED is returned immediately. 138 Again, in this case the call to sqlite3_backup_step() can be retried 139 later on. 140 If SQLITE_IOERR_XXX, SQLITE_NOMEM, or SQLITE_READONLY 141 is returned, then there is no point in retrying the call to sqlite3_backup_step(). 142 These errors are considered fatal. 143 The application must accept that the backup operation has failed and 144 pass the backup operation handle to the sqlite3_backup_finish() to 145 release associated resources. 146 .Pp 147 The first call to sqlite3_backup_step() obtains an exclusive lock on 148 the destination file. 149 The exclusive lock is not released until either sqlite3_backup_finish() 150 is called or the backup operation is complete and sqlite3_backup_step() 151 returns SQLITE_DONE. 152 Every call to sqlite3_backup_step() obtains a shared lock 153 on the source database that lasts for the duration of the sqlite3_backup_step() 154 call. 155 Because the source database is not locked between calls to sqlite3_backup_step(), 156 the source database may be modified mid-way through the backup process. 157 If the source database is modified by an external process or via a 158 database connection other than the one being used by the backup operation, 159 then the backup will be automatically restarted by the next call to 160 sqlite3_backup_step(). 161 If the source database is modified by using the same database connection 162 as is used by the backup operation, then the backup database is automatically 163 updated at the same time. 164 .Pp 165 \fBsqlite3_backup_finish()\fP 166 .Pp 167 When sqlite3_backup_step() has returned SQLITE_DONE, or 168 when the application wishes to abandon the backup operation, the application 169 should destroy the sqlite3_backup by passing it to sqlite3_backup_finish(). 170 The sqlite3_backup_finish() interfaces releases all resources associated 171 with the sqlite3_backup object. 172 If sqlite3_backup_step() has not yet returned SQLITE_DONE, 173 then any active write-transaction on the destination database is rolled 174 back. 175 The sqlite3_backup object is invalid and may not be used 176 following a call to sqlite3_backup_finish(). 177 .Pp 178 The value returned by sqlite3_backup_finish is SQLITE_OK if 179 no sqlite3_backup_step() errors occurred, regardless of whether or 180 not sqlite3_backup_step() completed. 181 If an out-of-memory condition or IO error occurred during any prior 182 sqlite3_backup_step() call on the same sqlite3_backup 183 object, then sqlite3_backup_finish() returns the corresponding error code. 184 .Pp 185 A return of SQLITE_BUSY or SQLITE_LOCKED from 186 sqlite3_backup_step() is not a permanent error and does not affect 187 the return value of sqlite3_backup_finish(). 188 .Pp 189 \fBsqlite3_backup_remaining() and sqlite3_backup_pagecount()\fP 190 .Pp 191 The sqlite3_backup_remaining() routine returns the number of pages 192 still to be backed up at the conclusion of the most recent sqlite3_backup_step(). 193 The sqlite3_backup_pagecount() routine returns the total number of 194 pages in the source database at the conclusion of the most recent sqlite3_backup_step(). 195 The values returned by these functions are only updated by sqlite3_backup_step(). 196 If the source database is modified in a way that changes the size of 197 the source database or the number of pages remaining, those changes 198 are not reflected in the output of sqlite3_backup_pagecount() and sqlite3_backup_remaining() 199 until after the next sqlite3_backup_step(). 200 .Pp 201 \fBConcurrent Usage of Database Handles\fP 202 .Pp 203 The source database connection may be used by the 204 application for other purposes while a backup operation is underway 205 or being initialized. 206 If SQLite is compiled and configured to support threadsafe database 207 connections, then the source database connection may be used concurrently 208 from within other threads. 209 .Pp 210 However, the application must guarantee that the destination database connection 211 is not passed to any other API (by any thread) after sqlite3_backup_init() 212 is called and before the corresponding call to sqlite3_backup_finish(). 213 SQLite does not currently check to see if the application incorrectly 214 accesses the destination database connection and 215 so no error code is reported, but the operations may malfunction nevertheless. 216 Use of the destination database connection while a backup is in progress 217 might also cause a mutex deadlock. 218 .Pp 219 If running in shared cache mode, the application must 220 guarantee that the shared cache used by the destination database is 221 not accessed while the backup is running. 222 In practice this means that the application must guarantee that the 223 disk file being backed up to is not accessed by any connection within 224 the process, not just the specific connection that was passed to sqlite3_backup_init(). 225 .Pp 226 The sqlite3_backup object itself is partially threadsafe. 227 Multiple threads may safely make multiple concurrent calls to sqlite3_backup_step(). 228 However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount() 229 APIs are not strictly speaking threadsafe. 230 If they are invoked at the same time as another thread is invoking 231 sqlite3_backup_step() it is possible that they return invalid values. 232 .Pp 233 \fBAlternatives To Using The Backup API\fP 234 .Pp 235 Other techniques for safely creating a consistent backup of an SQLite 236 database include: 237 .Bl -bullet 238 .It 239 The VACUUM INTO command. 240 .It 241 The sqlite3_rsync utility program. 242 .El 243 .Pp 244 .Sh IMPLEMENTATION NOTES 245 These declarations were extracted from the 246 interface documentation at line 9552. 247 .Bd -literal 248 SQLITE_API sqlite3_backup *sqlite3_backup_init( 249 sqlite3 *pDest, /* Destination database handle */ 250 const char *zDestName, /* Destination database name */ 251 sqlite3 *pSource, /* Source database handle */ 252 const char *zSourceName /* Source database name */ 253 ); 254 SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage); 255 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p); 256 SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p); 257 SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p); 258 .Ed 259 .Sh SEE ALSO 260 .Xr sqlite3 3 , 261 .Xr sqlite3_backup 3 , 262 .Xr sqlite3_busy_handler 3 , 263 .Xr sqlite3_errcode 3 , 264 .Xr SQLITE_ERROR_MISSING_COLLSEQ 3 , 265 .Xr SQLITE_OK 3 266