Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions pkg/cli/cmds/etcd_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ var EtcdSnapshotFlags = []cli.Flag{
Usage: "(db) Compress etcd snapshot",
Destination: &ServerConfig.EtcdSnapshotCompress,
},
&cli.IntFlag{
Name: "snapshot-retention,etcd-snapshot-retention",
Usage: "(db) Number of snapshots to retain.",
Destination: &ServerConfig.EtcdSnapshotRetention,
Value: defaultSnapshotRentention,
},
&cli.BoolFlag{
Name: "s3,etcd-s3",
Usage: "(db) Enable backup to S3",
Expand Down Expand Up @@ -140,12 +146,7 @@ func NewEtcdSnapshotCommands(delete, list, prune, save func(ctx *cli.Context) er
SkipFlagParsing: false,
SkipArgReorder: true,
Action: prune,
Flags: append(EtcdSnapshotFlags, &cli.IntFlag{
Name: "snapshot-retention",
Usage: "(db) Number of snapshots to retain.",
Destination: &ServerConfig.EtcdSnapshotRetention,
Value: defaultSnapshotRentention,
}),
Flags: EtcdSnapshotFlags,
},
},
Flags: EtcdSnapshotFlags,
Expand Down
57 changes: 32 additions & 25 deletions pkg/etcd/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,38 @@ var (
)

// snapshotDir ensures that the snapshot directory exists, and then returns its path.
// Only the default snapshot directory will be created; user-specified non-default
// snapshot directories must already exist.
func snapshotDir(config *config.Control, create bool) (string, error) {
if config.EtcdSnapshotDir == "" {
// we have to create the snapshot dir if we are using
// the default snapshot dir if it doesn't exist
defaultSnapshotDir := filepath.Join(config.DataDir, "db", "snapshots")
s, err := os.Stat(defaultSnapshotDir)
if err != nil {
if create && os.IsNotExist(err) {
if err := os.MkdirAll(defaultSnapshotDir, 0700); err != nil {
return "", err
}
return defaultSnapshotDir, nil
defaultSnapshotDir := filepath.Join(config.DataDir, "db", "snapshots")
snapshotDir := config.EtcdSnapshotDir

if snapshotDir == "" {
snapshotDir = defaultSnapshotDir
}

// Disable creation if not using the default snapshot dir.
// Non-default snapshot dirs must be created by the user.
if snapshotDir != defaultSnapshotDir {
create = false
}

s, err := os.Stat(snapshotDir)
if err != nil {
if os.IsNotExist(err) && create {
if err := os.MkdirAll(snapshotDir, 0700); err != nil {
return "", err
}
return "", err
}
if s.IsDir() {
return defaultSnapshotDir, nil
return snapshotDir, nil
}
return "", err
}

if !s.IsDir() {
return "", fmt.Errorf("%s is not a directory", snapshotDir)
}
return config.EtcdSnapshotDir, nil

return snapshotDir, nil
}

// preSnapshotSetup checks to see if the necessary components are in place
Expand Down Expand Up @@ -248,12 +260,6 @@ func (e *ETCD) Snapshot(ctx context.Context) error {
return errors.Wrap(err, "failed to get etcd-snapshot-dir")
}

if info, err := os.Stat(snapshotDir); err != nil {
return errors.Wrapf(err, "failed to stat etcd-snapshot-dir %s", snapshotDir)
} else if !info.IsDir() {
return fmt.Errorf("etcd-snapshot-dir %s is not a directory", snapshotDir)
}

cfg, err := getClientConfig(ctx, e.config)
if err != nil {
return errors.Wrap(err, "failed to get config for etcd snapshot")
Expand Down Expand Up @@ -438,7 +444,7 @@ func (e *ETCD) listLocalSnapshots() (map[string]snapshotFile, error) {
snapshots := make(map[string]snapshotFile)
snapshotDir, err := snapshotDir(e.config, true)
if err != nil {
return snapshots, errors.Wrap(err, "failed to get the snapshot dir")
return snapshots, errors.Wrap(err, "failed to get etcd-snapshot-dir")
}

if err := filepath.Walk(snapshotDir, func(path string, file os.FileInfo, err error) error {
Expand Down Expand Up @@ -502,8 +508,9 @@ func (e *ETCD) initS3IfNil(ctx context.Context) error {
func (e *ETCD) PruneSnapshots(ctx context.Context) error {
snapshotDir, err := snapshotDir(e.config, false)
if err != nil {
return errors.Wrap(err, "failed to get the snapshot dir")
return errors.Wrap(err, "failed to get etcd-snapshot-dir")
}

if err := snapshotRetention(e.config.EtcdSnapshotRetention, e.config.EtcdSnapshotName, snapshotDir); err != nil {
logrus.Errorf("Error applying snapshot retention policy: %v", err)
}
Expand Down Expand Up @@ -551,7 +558,7 @@ func (e *ETCD) ListSnapshots(ctx context.Context) (map[string]snapshotFile, erro
func (e *ETCD) DeleteSnapshots(ctx context.Context, snapshots []string) error {
snapshotDir, err := snapshotDir(e.config, false)
if err != nil {
return errors.Wrap(err, "failed to get the snapshot dir")
return errors.Wrap(err, "failed to get etcd-snapshot-dir")
}
if e.config.EtcdS3 {
if err := e.initS3IfNil(ctx); err != nil {
Expand Down