-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackup.go
More file actions
62 lines (53 loc) · 1.58 KB
/
backup.go
File metadata and controls
62 lines (53 loc) · 1.58 KB
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
package sqlite
// #include <amalgamation/sqlite3.h>
// #include <stdint.h>
//
// extern char * go_strcpy(_GoString_ st);
// extern void go_free(void*);
// extern int sqlite_BindGoPointer(sqlite3_stmt* stmt, int pos, uintptr_t ptr, const char* name);
import "C"
import "unsafe"
// DBName is the name of the database used in the application (main by default).
//
// It can be changed before a back-up is started to reflect the application specificities.
var DBName = "main"
// BackupDB performs an [online backup] to dest
//
// [online backup] https://www.sqlite.org/backup.html
func BackupDB(pool *Connections, dest string) error {
ctn := pool.take()
defer pool.put(ctn)
return Backup(ctn, dest)
}
// Backup performs an [online backup] to dest
//
// [online backup] https://www.sqlite.org/backup.html
func Backup(ctn *Conn, dest string) error {
var db *C.sqlite3
cname := C.go_strcpy(dest)
defer C.go_free(unsafe.Pointer(cname))
rv := C.sqlite3_open_v2(cname, &db,
C.SQLITE_OPEN_FULLMUTEX|
C.SQLITE_OPEN_READWRITE|
C.SQLITE_OPEN_CREATE|
C.SQLITE_OPEN_URI,
nil)
if rv != C.SQLITE_OK {
return errText[rv]
}
if db == nil {
panic("sqlite succeeded without returning a database")
}
cmain := C.go_strcpy(DBName)
defer C.go_free(unsafe.Pointer(cmain))
back := C.sqlite3_backup_init(db, cmain, ctn.db, cmain)
if back == nil {
return errorString{s: C.GoString(C.sqlite3_errmsg(ctn.db))}
}
C.sqlite3_backup_step(back, -1)
if rv := C.sqlite3_backup_finish(back); rv != C.SQLITE_OK {
return errorString{s: C.GoString(C.sqlite3_errmsg(ctn.db))}
}
C.sqlite3_close(db)
return nil
}