44 lines
662 B
Go
44 lines
662 B
Go
package bolt
|
|
|
|
import (
|
|
bolt "go.etcd.io/bbolt"
|
|
"net/url"
|
|
"time"
|
|
"os"
|
|
)
|
|
|
|
type BoltStorage struct {
|
|
DB *bolt.DB
|
|
}
|
|
|
|
func (stor *BoltStorage) Shutdown() {
|
|
stor.DB.Close()
|
|
}
|
|
|
|
func New(u *url.URL) (*BoltStorage, error) {
|
|
path := u.Opaque
|
|
if u.Path != "" {
|
|
path = u.Path
|
|
}
|
|
|
|
db, err := bolt.Open(path, 0600, &bolt.Options{Timeout: 3 * time.Second})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &BoltStorage{DB: db}, nil
|
|
}
|
|
|
|
func (stor *BoltStorage) Backup(bpath string) error {
|
|
tx, err := stor.DB.Begin(false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer tx.Rollback()
|
|
|
|
err = tx.CopyFile(bpath, os.FileMode(0600))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|