prysm-pulse/beacon-chain/db/kv/backup.go
Shay Zluf 9103ec98cb
QSP-14 consistent file permissions (#6378)
* QSP-14 consistent file permissions
* Merge refs/heads/master into consistent_permissions
* Merge refs/heads/master into consistent_permissions
* default io file permissions
* Merge branch 'consistent_permissions' of github.com:prysmaticlabs/prysm into consistent_permissions
* fix comments
* Merge refs/heads/master into consistent_permissions
* Merge refs/heads/master into consistent_permissions
* gaz
* Merge branch 'consistent_permissions' of github.com:prysmaticlabs/prysm into consistent_permissions
* Merge refs/heads/master into consistent_permissions
* Rename IoConfig.go to io_config.go
* Update shared/params/BUILD.bazel
* Merge refs/heads/master into consistent_permissions
* raul feedback
* Merge branch 'consistent_permissions' of github.com:prysmaticlabs/prysm into consistent_permissions
* Merge refs/heads/master into consistent_permissions
* Merge refs/heads/master into consistent_permissions
* Merge refs/heads/master into consistent_permissions
* Merge refs/heads/master into consistent_permissions
* Merge refs/heads/master into consistent_permissions
* Merge refs/heads/master into consistent_permissions
* gaz
* Merge branch 'consistent_permissions' of github.com:prysmaticlabs/prysm into consistent_permissions
* Merge refs/heads/master into consistent_permissions
* Merge refs/heads/master into consistent_permissions
* Merge refs/heads/master into consistent_permissions
* Merge refs/heads/master into consistent_permissions
2020-06-25 16:12:59 +00:00

64 lines
1.7 KiB
Go

package kv
import (
"context"
"fmt"
"os"
"path"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/shared/params"
"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 (kv *Store) Backup(ctx context.Context) error {
ctx, span := trace.StartSpan(ctx, "BeaconDB.Backup")
defer span.End()
backupsDir := path.Join(kv.databasePath, backupsDirectoryName)
head, err := kv.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, params.BeaconIoConfig().ReadWritePermissions, nil)
if err != nil {
panic(err)
}
defer func() {
if err := copyDB.Close(); err != nil {
logrus.WithError(err).Error("Failed to close destination database")
}
}()
return kv.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)
})
})
})
})
}