Skip to content

Commit

Permalink
feat: add backup path (#10649)
Browse files Browse the repository at this point in the history
## Description
Closes:  #10533

Cosmovisor can cause problems related to disk space when they try backing up, not enough space might fail the node.
A new feature is added to allow user to set a backup path, which can be separated from the node-home directory.
A new env var `DAEMON_BACKUP_DIR` is added. If `DAEMON_BACKUP_DIR` is set, backups will go there instead of `DAEMON_HOME`. 

- [x] add custom data backup dir 
- [x] add tests



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
yaruwangway authored Jan 4, 2022
1 parent f8b552b commit 344be86
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 62 deletions.
2 changes: 1 addition & 1 deletion cosmovisor/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Features

+ [\#10285](https://github.com/cosmos/cosmos-sdk/pull/10316) Added `run` action.

+ [\#10533](https://github.com/cosmos/cosmos-sdk/pull/10649) Added environmental variable `DAEMON_BACKUP_DIR` to allow node to set a custom backup directory.
### Deprecated

+ [\#10285](https://github.com/cosmos/cosmos-sdk/pull/10316) Running `cosmovisor` without the `run` argument.
Expand Down
40 changes: 34 additions & 6 deletions cosmovisor/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@ const (
EnvDownloadBin = "DAEMON_ALLOW_DOWNLOAD_BINARIES"
EnvRestartUpgrade = "DAEMON_RESTART_AFTER_UPGRADE"
EnvSkipBackup = "UNSAFE_SKIP_BACKUP"
EnvDataBackupPath = "DAEMON_DATA_BACKUP_DIR"
EnvInterval = "DAEMON_POLL_INTERVAL"
EnvPreupgradeMaxRetries = "DAEMON_PREUPGRADE_MAX_RETRIES"
)

const (
rootName = "cosmovisor"
genesisDir = "genesis"
upgradesDir = "upgrades"
currentLink = "current"
rootName = "cosmovisor"
genesisDir = "genesis"
upgradesDir = "upgrades"
currentLink = "current"
)

// must be the same as x/upgrade/types.UpgradeInfoFilename
Expand All @@ -47,6 +48,7 @@ type Config struct {
RestartAfterUpgrade bool
PollInterval time.Duration
UnsafeSkipBackup bool
DataBackupPath string
PreupgradeMaxRetries int

// currently running upgrade
Expand Down Expand Up @@ -129,8 +131,13 @@ func (cfg *Config) CurrentBin() (string, error) {
func GetConfigFromEnv() (*Config, error) {
var errs []error
cfg := &Config{
Home: os.Getenv(EnvHome),
Name: os.Getenv(EnvName),
Home: os.Getenv(EnvHome),
Name: os.Getenv(EnvName),
DataBackupPath: os.Getenv(EnvDataBackupPath),
}

if cfg.DataBackupPath == "" {
cfg.DataBackupPath = cfg.Home
}

var err error
Expand Down Expand Up @@ -213,6 +220,25 @@ func (cfg *Config) validate() []error {
}
}

// check the DataBackupPath
if cfg.UnsafeSkipBackup == true {
return errs
}
// if UnsafeSkipBackup is false, check if the DataBackupPath valid
switch {
case cfg.DataBackupPath == "":
errs = append(errs, errors.New(EnvDataBackupPath + " must not be empty"))
case !filepath.IsAbs(cfg.DataBackupPath):
errs = append(errs, errors.New(cfg.DataBackupPath + " must be an absolute path"))
default:
switch info, err := os.Stat(cfg.DataBackupPath); {
case err != nil:
errs = append(errs, fmt.Errorf("%q must be a valid directory: %w", cfg.DataBackupPath, err))
case !info.IsDir():
errs = append(errs, fmt.Errorf("%q must be a valid directory", cfg.DataBackupPath))
}
}

return errs
}

Expand Down Expand Up @@ -305,13 +331,15 @@ func (cfg Config) DetailString() string {
{EnvRestartUpgrade, fmt.Sprintf("%t", cfg.RestartAfterUpgrade)},
{EnvInterval, fmt.Sprintf("%s", cfg.PollInterval)},
{EnvSkipBackup, fmt.Sprintf("%t", cfg.UnsafeSkipBackup)},
{EnvDataBackupPath, cfg.DataBackupPath},
{EnvPreupgradeMaxRetries, fmt.Sprintf("%d", cfg.PreupgradeMaxRetries)},
}
derivedEntries := []struct{ name, value string }{
{"Root Dir", cfg.Root()},
{"Upgrade Dir", cfg.BaseUpgradeDir()},
{"Genesis Bin", cfg.GenesisBin()},
{"Monitored File", cfg.UpgradeInfoFilePath()},
{"Data Backup Dir", cfg.DataBackupPath},
}

var sb strings.Builder
Expand Down
Loading

0 comments on commit 344be86

Please sign in to comment.