2019-10-03 09:29:49 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/consensus-types/blocks"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/io/file"
|
2020-03-24 20:00:54 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
2019-10-03 09:29:49 +00:00
|
|
|
"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
|
2021-06-18 01:37:58 +00:00
|
|
|
func (s *Store) Backup(ctx context.Context, outputDir string, permissionOverride bool) error {
|
2019-10-03 09:29:49 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "BeaconDB.Backup")
|
|
|
|
defer span.End()
|
|
|
|
|
2020-10-26 18:20:54 +00:00
|
|
|
var backupsDir string
|
|
|
|
var err error
|
|
|
|
if outputDir != "" {
|
2021-09-17 21:55:24 +00:00
|
|
|
backupsDir, err = file.ExpandPath(outputDir)
|
2020-10-26 18:20:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
backupsDir = path.Join(s.databasePath, backupsDirectoryName)
|
|
|
|
}
|
2020-10-12 08:11:05 +00:00
|
|
|
head, err := s.HeadBlock(ctx)
|
2019-10-03 09:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-02 15:30:46 +00:00
|
|
|
if err := blocks.BeaconBlockIsNil(head); err != nil {
|
2021-11-01 14:13:05 +00:00
|
|
|
return err
|
2019-10-03 09:29:49 +00:00
|
|
|
}
|
|
|
|
// Ensure the backups directory exists.
|
2021-09-17 21:55:24 +00:00
|
|
|
if err := file.HandleBackupDir(backupsDir, permissionOverride); err != nil {
|
2019-10-03 09:29:49 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-05-26 16:19:54 +00:00
|
|
|
backupPath := path.Join(backupsDir, fmt.Sprintf("prysm_beacondb_at_slot_%07d.backup", head.Block().Slot()))
|
2021-01-11 20:03:28 +00:00
|
|
|
log.WithField("backup", backupPath).Info("Writing backup database.")
|
2020-02-12 22:01:07 +00:00
|
|
|
|
2020-10-26 18:20:54 +00:00
|
|
|
copyDB, err := bolt.Open(
|
|
|
|
backupPath,
|
|
|
|
params.BeaconIoConfig().ReadWritePermissions,
|
2021-06-21 13:14:55 +00:00
|
|
|
&bolt.Options{NoSync: true, Timeout: params.BeaconIoConfig().BoltTimeout, FreelistType: bolt.FreelistMapType},
|
2020-10-26 18:20:54 +00:00
|
|
|
)
|
2020-02-12 22:01:07 +00:00
|
|
|
if err != nil {
|
2020-12-03 22:28:57 +00:00
|
|
|
return err
|
2020-02-12 22:01:07 +00:00
|
|
|
}
|
2021-03-03 15:37:13 +00:00
|
|
|
copyDB.AllocSize = boltAllocSize
|
|
|
|
|
2020-04-13 04:11:09 +00:00
|
|
|
defer func() {
|
|
|
|
if err := copyDB.Close(); err != nil {
|
2021-01-11 20:03:28 +00:00
|
|
|
log.WithError(err).Error("Failed to close backup database")
|
2020-04-13 04:11:09 +00:00
|
|
|
}
|
|
|
|
}()
|
2021-03-03 15:37:13 +00:00
|
|
|
// Prefetch all keys of buckets, and inner keys in a
|
|
|
|
// bucket to use less memory usage when backing up.
|
2021-05-13 06:29:14 +00:00
|
|
|
var bucketKeys [][]byte
|
2021-03-03 15:37:13 +00:00
|
|
|
bucketMap := make(map[string][][]byte)
|
|
|
|
err = s.db.View(func(tx *bolt.Tx) error {
|
2020-02-12 22:01:07 +00:00
|
|
|
return tx.ForEach(func(name []byte, b *bolt.Bucket) error {
|
2021-03-03 15:37:13 +00:00
|
|
|
newName := make([]byte, len(name))
|
|
|
|
copy(newName, name)
|
|
|
|
bucketKeys = append(bucketKeys, newName)
|
2021-05-13 06:29:14 +00:00
|
|
|
var innerKeys [][]byte
|
2021-03-03 15:37:13 +00:00
|
|
|
err := b.ForEach(func(k, v []byte) error {
|
|
|
|
if k == nil {
|
|
|
|
return nil
|
2020-02-12 22:01:07 +00:00
|
|
|
}
|
2021-03-03 15:37:13 +00:00
|
|
|
nKey := make([]byte, len(k))
|
|
|
|
copy(nKey, k)
|
|
|
|
innerKeys = append(innerKeys, nKey)
|
|
|
|
return nil
|
2020-02-12 22:01:07 +00:00
|
|
|
})
|
2021-03-03 15:37:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
bucketMap[string(newName)] = innerKeys
|
|
|
|
return nil
|
2020-02-12 22:01:07 +00:00
|
|
|
})
|
2019-10-03 09:29:49 +00:00
|
|
|
})
|
2021-03-03 15:37:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Utilize much smaller writes, compared to
|
|
|
|
// writing for a whole bucket in a single transaction. Also
|
|
|
|
// prevent long-running read transactions, as Bolt doesn't
|
|
|
|
// handle those well.
|
|
|
|
for _, k := range bucketKeys {
|
|
|
|
log.Debugf("Copying bucket %s\n", k)
|
|
|
|
innerKeys := bucketMap[string(k)]
|
|
|
|
for _, ik := range innerKeys {
|
|
|
|
err = s.db.View(func(tx *bolt.Tx) error {
|
|
|
|
bkt := tx.Bucket(k)
|
|
|
|
return copyDB.Update(func(tx2 *bolt.Tx) error {
|
|
|
|
b2, err := tx2.CreateBucketIfNotExists(k)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return b2.Put(ik, bkt.Get(ik))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Re-enable sync to allow bolt to fsync
|
|
|
|
// again.
|
|
|
|
copyDB.NoSync = false
|
|
|
|
return nil
|
2019-10-03 09:29:49 +00:00
|
|
|
}
|