Skip to content

Commit

Permalink
Refactoring and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
YannikBramkamp committed Dec 30, 2020
1 parent 4088387 commit 0fd3910
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
16 changes: 12 additions & 4 deletions pkg/restic/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Client struct {
Config *Config
}

// NewResticClient creates a new restic client with the given hostname and backup paths
func NewResticClient(logger *log.Entry, hostname string, backupPaths ...string) (*Client, error) {
conf := &Config{
Global: &GlobalOptions{
Expand Down Expand Up @@ -55,6 +56,7 @@ func NewResticClient(logger *log.Entry, hostname string, backupPaths ...string)
}, nil
}

// DoResticBackup executes initBackup and CreateBackup with the settings from c
func (c *Client) DoResticBackup(ctx context.Context) error {
c.Logger.Info("running 'restic backup'")

Expand All @@ -77,6 +79,7 @@ func (c *Client) DoResticBackup(ctx context.Context) error {
return nil
}

// DoResticForget executes Forget with the settings from c
func (c *Client) DoResticForget(ctx context.Context) error {
c.Logger.Info("running 'restic forget'")

Expand All @@ -92,7 +95,8 @@ func (c *Client) DoResticForget(ctx context.Context) error {
return nil
}

func (c *Client) ListSnapshots(ctx context.Context) error {
// DoResticListSnapshots executes ListSnapshots with the settings from c
func (c *Client) DoResticListSnapshots(ctx context.Context) error {
c.Logger.Info("running 'restic snapshots'")

output, err := ListSnapshots(ctx, c.Config.Global, c.Config.Snapshots)
Expand All @@ -107,6 +111,7 @@ func (c *Client) ListSnapshots(ctx context.Context) error {
return nil
}

// DoResticCheck executes Check with the settings from c
func (c *Client) DoResticCheck(ctx context.Context) error {
c.Logger.Info("running 'restic check'")

Expand All @@ -118,7 +123,8 @@ func (c *Client) DoResticCheck(ctx context.Context) error {
return nil
}

func (c *Client) PruneRepo(ctx context.Context) error {
// DoResticPruneRepo executes Prune with the settings from c
func (c *Client) DoResticPruneRepo(ctx context.Context) error {
c.Logger.Info("running 'restic prune")

output, err := Prune(ctx, c.Config.Global)
Expand All @@ -129,7 +135,8 @@ func (c *Client) PruneRepo(ctx context.Context) error {
return nil
}

func (c *Client) RebuildIndex(ctx context.Context) error {
// DoResticRebuildIndex executes RebuildIndex with the settings from c
func (c *Client) DoResticRebuildIndex(ctx context.Context) error {
c.Logger.Info("running 'restic rebuild-index'")

output, err := RebuildIndex(ctx, c.Config.Global)
Expand All @@ -140,7 +147,8 @@ func (c *Client) RebuildIndex(ctx context.Context) error {
return nil
}

func (c *Client) ResticTag(ctx context.Context) error {
// DoResticTag executes Tag with the settings from c
func (c *Client) DoResticTag(ctx context.Context) error {
c.Logger.Info("running 'restic rebuild-index'")

output, err := Tag(ctx, c.Config.Global, c.Config.Tags)
Expand Down
2 changes: 1 addition & 1 deletion pkg/restic/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func init() {
forgetConcreteSnapshotPattern = regexp.MustCompile(`^removed snapshot ([0-9a-z].*)$`)
}

// InitBackup executes "restic init"
// initBackup executes "restic init"
func initBackup(ctx context.Context, globalOpts *GlobalOptions) ([]byte, error) {
cmd := cli.CommandType{
Binary: binary,
Expand Down
12 changes: 8 additions & 4 deletions pkg/source/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func getGenericBackendForKind(kind string) (Generic, error) {
}
}

// DoBackupForKind performs the appropriate backup action for given arguments.
// It also executes any given restic commands after files have been backed up,
func DoBackupForKind(ctx context.Context, kind string, cleanup, useRestic, useResticForget, listResticSnapshots,
resticCheck, resticPrune, rebuildIndex, resticTags bool) error {
logKind := log.WithFields(
Expand Down Expand Up @@ -80,6 +82,8 @@ func DoBackupForKind(ctx context.Context, kind string, cleanup, useRestic, useRe
return err
}

// execute any applicable restic commands

err = resticClient.DoResticBackup(ctx)
if err != nil {
return err
Expand All @@ -93,28 +97,28 @@ func DoBackupForKind(ctx context.Context, kind string, cleanup, useRestic, useRe
}

if listResticSnapshots {
err = resticClient.ListSnapshots(ctx)
err = resticClient.DoResticListSnapshots(ctx)
if err != nil {
return err
}
}

if resticPrune {
err = resticClient.PruneRepo(ctx)
err = resticClient.DoResticPruneRepo(ctx)
if err != nil {
return err
}
}

if resticTags {
err = resticClient.ResticTag(ctx)
err = resticClient.DoResticTag(ctx)
if err != nil {
return err
}
}

if rebuildIndex {
err = resticClient.RebuildIndex(ctx)
err = resticClient.DoResticRebuildIndex(ctx)
if err != nil {
return err
}
Expand Down

0 comments on commit 0fd3910

Please sign in to comment.