mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-05 17:22:18 +00:00
d077483577
* v3 import renamings * tidy * fmt * rev * Update beacon-chain/core/epoch/precompute/reward_penalty_test.go * Update beacon-chain/core/helpers/validators_test.go * Update beacon-chain/db/alias.go * Update beacon-chain/db/alias.go * Update beacon-chain/db/alias.go * Update beacon-chain/db/iface/BUILD.bazel * Update beacon-chain/db/kv/kv.go * Update beacon-chain/db/kv/state.go * Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go * Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go * Update beacon-chain/sync/initial-sync/service.go * fix deps * fix bad replacements * fix bad replacements * change back * gohashtree version * fix deps Co-authored-by: Nishant Das <nishdas93@gmail.com> Co-authored-by: Potuz <potuz@prysmaticlabs.com>
83 lines
2.2 KiB
Go
83 lines
2.2 KiB
Go
package kv
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path"
|
|
"time"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/config/params"
|
|
"github.com/prysmaticlabs/prysm/v3/io/file"
|
|
bolt "go.etcd.io/bbolt"
|
|
"go.opencensus.io/trace"
|
|
)
|
|
|
|
const backupsDirectoryName = "backups"
|
|
|
|
// Backup the database to the datadir backup directory.
|
|
// Example for backup: $DATADIR/backups/prysm_validatordb_1029019.backup
|
|
func (s *Store) Backup(ctx context.Context, outputDir string, permissionOverride bool) error {
|
|
ctx, span := trace.StartSpan(ctx, "ValidatorDB.Backup")
|
|
defer span.End()
|
|
|
|
var backupsDir string
|
|
var err error
|
|
if outputDir != "" {
|
|
backupsDir, err = file.ExpandPath(outputDir)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
backupsDir = path.Join(s.databasePath, backupsDirectoryName)
|
|
}
|
|
// Ensure the backups directory exists.
|
|
if err := file.HandleBackupDir(backupsDir, permissionOverride); err != nil {
|
|
return err
|
|
}
|
|
backupPath := path.Join(backupsDir, fmt.Sprintf("prysm_validatordb_%d.backup", time.Now().Unix()))
|
|
log.WithField("backup", backupPath).Info("Writing backup database")
|
|
|
|
copyDB, err := bolt.Open(
|
|
backupPath,
|
|
params.BeaconIoConfig().ReadWritePermissions,
|
|
&bolt.Options{Timeout: params.BeaconIoConfig().BoltTimeout},
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
if err := copyDB.Close(); err != nil {
|
|
log.WithError(err).Error("Failed to close backup database")
|
|
}
|
|
}()
|
|
|
|
return s.db.View(func(tx *bolt.Tx) error {
|
|
return tx.ForEach(func(name []byte, b *bolt.Bucket) error {
|
|
log.Debugf("Copying bucket %s\n with %d keys", name, b.Stats().KeyN)
|
|
return copyDB.Update(func(tx2 *bolt.Tx) error {
|
|
b2, err := tx2.CreateBucketIfNotExists(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return b.ForEach(createNestedBuckets(b, b2, b2.Put))
|
|
})
|
|
})
|
|
})
|
|
}
|
|
|
|
// Walks through each buckets and looks out for nested buckets so that
|
|
// the backup db also includes them.
|
|
func createNestedBuckets(srcBucket, dstBucket *bolt.Bucket, fn func(k, v []byte) error) func(k, v []byte) error {
|
|
return func(k, v []byte) error {
|
|
bkt := srcBucket.Bucket(k)
|
|
if bkt != nil {
|
|
b2, err := dstBucket.CreateBucketIfNotExists(k)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return bkt.ForEach(createNestedBuckets(bkt, b2, b2.Put))
|
|
}
|
|
return fn(k, v)
|
|
}
|
|
}
|