-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbackup.go
More file actions
41 lines (38 loc) · 1.21 KB
/
Copy pathbackup.go
File metadata and controls
41 lines (38 loc) · 1.21 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
package minisql
import (
"context"
"database/sql"
"fmt"
)
// Backup creates a consistent, point-in-time copy of db at destPath.
//
// The database remains fully readable and writable during the backup.
// Write transactions are blocked only for the brief moment needed to snapshot
// the WAL index (typically microseconds); the page-copy phase runs concurrently
// with normal database activity.
//
// The destination is a standalone file that can be opened directly:
//
// if err := minisql.Backup(ctx, db, "/path/to/backup.db"); err != nil {
// log.Fatal(err)
// }
// backup, err := sql.Open("minisql", "/path/to/backup.db")
//
// If the source database uses transparent encryption the backup is encrypted
// with the same key. The backup carries no WAL file.
//
// Backup must not be called from inside an explicit user transaction.
func Backup(ctx context.Context, db *sql.DB, destPath string) error {
conn, err := db.Conn(ctx)
if err != nil {
return fmt.Errorf("minisql: Backup: acquire connection: %w", err)
}
defer conn.Close()
return conn.Raw(func(c any) error {
mc, ok := c.(*Conn)
if !ok {
return fmt.Errorf("minisql: Backup: unexpected connection type %T", c)
}
return mc.db.Backup(ctx, destPath)
})
}