-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsembed.go
53 lines (40 loc) · 1 KB
/
lsembed.go
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
package lsembed
import (
"context"
"log"
"os"
"github.com/benbjohnson/litestream"
)
var ctx = context.Background()
func Replicate(r *litestream.Replica) (*litestream.DB, error) {
lsdb := r.DB()
lsdb.Replicas = append(lsdb.Replicas, r)
if err := Restore(r); err != nil {
return nil, err
}
if err := lsdb.Open(); err != nil {
return nil, err
}
return lsdb, nil
}
func Restore(r *litestream.Replica) (err error) {
// skip restore if local database already exists
if _, err := os.Stat(r.DB().Path()); err == nil {
log.Println("local database already exists, skipping restore")
return nil
}
opt := litestream.NewRestoreOptions()
opt.OutputPath = r.DB().Path()
if opt.Generation, _, err = r.CalcRestoreTarget(ctx, opt); err != nil {
return err
}
if opt.Generation == "" {
log.Println("no generation to restore")
}
log.Printf("restoring %s to %s", opt.Generation, opt.OutputPath)
if err := r.Restore(ctx, opt); err != nil {
return err
}
log.Println("restore complete")
return nil
}