timepolicy

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2023 License: MIT Imports: 14 Imported by: 0

README

timepolicy

Implement retention policies on the command line across a variety of object and storage types.

Command Line Usage

Pipe lines that start with a time and configure --policy specifications to control which line entries match.

( echo '2023-01-01 backup-20230101.tar.gz' ; echo '2023-01-02 backup-20230102.tar.gz' ) \
  | go run ./cmd/timepolicy \
    --policy='1y;by=month' \
    --time=YYYY-MM-DD \
    --write='$2'
#> backup-20230102.tar.gz

See timepolicy --help for full documentation and features.

Installation

Binaries for Linux, macOS, and Windows can be downloaded from the Releases page. A Homebrew recipe is also available for Linux and macOS.

brew install dpb587/timepolicy/latest
Examples

Prune Google Cloud snapshots based on creation time...

gcloud compute snapshots list --format='value(creationTimestamp, name)' \
  | timepolicy \
      --policy='1y;by=month // within 1 year, keep newest per month' \
      --policy='28d;by=day  // within 28 days, keep newest per day' \
      --policy='7d;by=hour  // within 7 days, keep newest per hour' \
      --write='$2' \
      --invert \
  | xargs -- \
      echo gcloud compute snapshots delete

Prune local backup files based on date in file name (e.g. Hubitat_2023-05-04~2.3.5.131.lzf)...

find . -name '*.lzf' \
  | sed -E 's#./(Hubitat_(..........).+)#\2 \1#' \
  | timepolicy \
      --policy='1y;by=month // within 1 year, keep newest by month' \
      --policy='14d;by=day  // within 14 days, keep newest by day' \
      --ts='YYYY-MM-DD' \
      --write='$2' \
      --invert \
  | xargs -n1 -- \
      echo rm

List selected files from Amazon S3 and total size based on modification date...

aws s3api list-objects --bucket acme-backup-us-west-1 \
  --output=text \
  --query='Contents[*].[LastModified, Size, Key]' \
  | timepolicy \
      --policy='14d;by=day;oldest // within 14 days, keep oldest by day' \
  | tee >( cut -f3 ) \
  | cut -f2 | paste -sd+ - | bc | numfmt --to=iec-i

Futures

  • support reading policies from files
  • expand unit tests

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Now = time.Now
View Source
var SpacesEntryFieldSplitter = EntryFieldSplitterFunc(func(v string, limit int) ([]string, error) {
	return reEntryFieldSplitterSpaces.Split(v, limit), nil
})

Functions

This section is empty.

Types

type Entry

type Entry struct {
	Raw    string
	Fields []string
	Time   time.Time
}

func (*Entry) Eval

func (e *Entry) Eval(prg cel.Program) (ref.Val, *cel.EvalDetails, error)

type EntryFieldSplitterFunc

type EntryFieldSplitterFunc func(v string, limit int) ([]string, error)

type EntryScanner

type EntryScanner interface {
	Scan() bool
	Err() error
	Entry() *Entry
	EntryOffset() int
}

func NewCSVEntryScanner

func NewCSVEntryScanner(r *csv.Reader, timeField int, timeParser TimeParserFunc) EntryScanner

func NewGenericEntryScanner

func NewGenericEntryScanner(s *bufio.Scanner, fieldSplitter EntryFieldSplitterFunc, fieldsLimit int, timeField int, timeParser TimeParserFunc) EntryScanner

type EntryWriter

type EntryWriter interface {
	EntriesWritten() int64
	WriteEntry(e *Entry) error
}

func NewDiscardEntryWriter

func NewDiscardEntryWriter() EntryWriter

func NewEntryFieldWriter

func NewEntryFieldWriter(w io.Writer, field int) EntryWriter

func NewEntryWriter

func NewEntryWriter(w io.Writer) EntryWriter

type PolicySelection

type PolicySelection struct {
	// contains filtered or unexported fields
}

func NewPolicySelection

func NewPolicySelection(spec *PolicySpec, evictions EntryWriter) *PolicySelection

func (*PolicySelection) Entries

func (p *PolicySelection) Entries() []*Entry

func (*PolicySelection) EvaluateEntry

func (p *PolicySelection) EvaluateEntry(e *Entry) (bool, error)

type PolicySelectionSet

type PolicySelectionSet struct {
	// contains filtered or unexported fields
}

func NewPolicySelectionSet

func NewPolicySelectionSet(specs []*PolicySpec, evictions EntryWriter) *PolicySelectionSet

func (*PolicySelectionSet) Entries

func (p *PolicySelectionSet) Entries() []*Entry

func (*PolicySelectionSet) EvaluateEntry

func (p *PolicySelectionSet) EvaluateEntry(e *Entry) (bool, error)

type PolicySpec

type PolicySpec struct {
	// contains filtered or unexported fields
}

func ParsePolicySpecString

func ParsePolicySpecString(defaultName string, raw string) (*PolicySpec, error)

func (*PolicySpec) MatchEntry

func (ps *PolicySpec) MatchEntry(e *Entry) (bool, error)

func (*PolicySpec) String

func (ps *PolicySpec) String() string

type TimeParserFunc

type TimeParserFunc func(v string) (time.Time, error)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL