-
Notifications
You must be signed in to change notification settings - Fork 45
/
backup.go
97 lines (82 loc) · 3.14 KB
/
backup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2019 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package superviser
import (
"encoding/hex"
"errors"
"github.com/dfuse-io/node-manager/metrics"
"github.com/dfuse-io/node-manager/superviser"
"github.com/eoscanada/pitreos"
"go.uber.org/zap"
)
type NodeosBackupInfo struct {
ChainIDStr string `yaml:"chainIdStr"`
ServerVersion string `yaml:"serverVersion"`
LastBlockSeen uint32 `yaml:"lastBlockSeen"`
ServerVersionString string `yaml:"serverVersionString"`
}
func (s *NodeosSuperviser) TakeBackup(backupTag string, backupStoreURL string) error {
if s.options.NoBlocksLog {
return errors.New("unable to take backup: refusing to take backup on an instance with option 'NoBlocksLog'")
}
if s.IsRunning() {
return errors.New("unable to take backup: refusing to take backup while process is running")
}
p, err := superviser.GetPitreos(s.Logger, backupStoreURL, "blocks/blocks.log")
if err != nil {
return err
}
details := make(map[string]interface{})
details["nodeosInfo"] = NodeosBackupInfo{
ChainIDStr: hex.EncodeToString(s.chainID),
ServerVersion: string(s.serverVersion),
ServerVersionString: s.serverVersionString,
LastBlockSeen: s.lastBlockSeen,
}
s.Logger.Info("creating backup", zap.String("store_url", backupStoreURL), zap.String("tag", backupTag))
err = p.GenerateBackup(s.options.DataDir, backupTag, details, pitreos.MustNewIncludeThanExcludeFilter(".*", ""))
if err == nil {
metrics.SuccessfulBackups.Inc()
}
return err
}
func (s *NodeosSuperviser) RestoreBackup(backupName, backupTag string, backupStoreURL string) error {
if s.IsRunning() {
return errors.New("unable to take backup: refusing to restore backup while process is running")
}
var appendonlyFiles []string
var exclusionFilter string
if s.options.NoBlocksLog {
exclusionFilter = "blocks/blocks.(log|index)"
} else {
appendonlyFiles = append(appendonlyFiles, "blocks/blocks.log")
}
p, err := superviser.GetPitreos(s.Logger, backupStoreURL, appendonlyFiles...)
if err != nil {
return err
}
if backupName == "latest" {
// FIXME: This logic should be moved up to the operator, so it's not repeated between each superviser!
backupName, err = p.GetLatestBackup(backupTag)
if err != nil {
return err
}
}
s.logger.Info("restoring from pitreos", zap.String("backup_name", backupName), zap.Any("appendonly_files", appendonlyFiles), zap.String("exclusion_filter", exclusionFilter))
err = p.RestoreFromBackup(s.options.DataDir, backupName, pitreos.MustNewIncludeThanExcludeFilter(".*", exclusionFilter))
if s.HandlePostRestore != nil {
s.HandlePostRestore()
}
return err
}