prysm-pulse/beacon-chain/db/kv/backup.go
terence tsao 78a865eb0b
Replace boltdb imports with bbolt import (#5193)
* Replaced. Debugging missing strict dependencies...
* Merge branch 'master' into bbolt-import
* Update import path
* Merge branch 'bbolt-import' of github.com:prysmaticlabs/prysm into bbolt-import
* use forked prombbolt
* Merge branch 'bbolt-import' of github.com:prysmaticlabs/prysm into bbolt-import
* fix
* remove old boltdb reference
* Use correct bolt for pk manager
* Merge branch 'bbolt-import' of github.com:prysmaticlabs/prysm into bbolt-import
* fix for docker build
* gaz, oops
2020-03-24 20:00:54 +00:00

59 lines
1.5 KiB
Go

package kv
import (
"context"
"fmt"
"os"
"path"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
bolt "go.etcd.io/bbolt"
"go.opencensus.io/trace"
)
const backupsDirectoryName = "backups"
// Backup the database to the datadir backup directory.
// Example for backup at slot 345: $DATADIR/backups/prysm_beacondb_at_slot_0000345.backup
func (k *Store) Backup(ctx context.Context) error {
ctx, span := trace.StartSpan(ctx, "BeaconDB.Backup")
defer span.End()
backupsDir := path.Join(k.databasePath, backupsDirectoryName)
head, err := k.HeadBlock(ctx)
if err != nil {
return err
}
if head == nil {
return errors.New("no head block")
}
// Ensure the backups directory exists.
if err := os.MkdirAll(backupsDir, os.ModePerm); err != nil {
return err
}
backupPath := path.Join(backupsDir, fmt.Sprintf("prysm_beacondb_at_slot_%07d.backup", head.Block.Slot))
logrus.WithField("prefix", "db").WithField("backup", backupPath).Info("Writing backup database.")
copyDB, err := bolt.Open(backupPath, 0666, nil)
if err != nil {
panic(err)
}
defer copyDB.Close()
return k.db.View(func(tx *bolt.Tx) error {
return tx.ForEach(func(name []byte, b *bolt.Bucket) error {
logrus.Debugf("Copying bucket %s\n", name)
return copyDB.Update(func(tx2 *bolt.Tx) error {
b2, err := tx2.CreateBucketIfNotExists(name)
if err != nil {
return err
}
return b.ForEach(func(k []byte, v []byte) error {
return b2.Put(k, v)
})
})
})
})
}