scrubber

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2018 License: MIT Imports: 9 Imported by: 0

README

Scrubber

Build Status

Scrubber provides an easy way to clean up old files in a directory.

It is especially useful on platforms where logrotate is not easily available or disk space is sparse.

Configuration

You can specify directories to cleanup in a toml configuration file. You can define one or more strategies used for each directory.

title = "Log scrubber example config"

[[directory]]
name = "Apache Logs"
path = "/var/logs/apache"
exclude = ["zip"]

    [[directory.strategy]]
    type = "size"
    action = "delete"
    limit = "100M"

    [[directory.strategy]]
    type = "age"
    action = "delete"
    limit = "1y"

    [[directory.strategy]]
    type = "age"
    action = "zip"
    limit = "1d"

[[directory]]
name = "Backups"
path = "/var/backups/yourapp"
include = ["tar.gz"]

    [[directory.strategy]]
    type = "age"
    action = "delete"
    limit = "1y"
Directory

The following options are available for each directory:

Option Description
name A descriptive name for this directory.
path The full path to the directory.
include (Optional) Define what files should be included. All files without a matching extension will be ignored.
exclude (Optional) Define what files should be excluded. All files with matching extension will be ignored.

You can either specify a include or a exclude rule but never both.

Strategy

The following options are available for each strategy:

Option Possible values Description
type age and size If the files should be selected by their age (last modified) or their size.
action delete and zip If matching files should be deleted or zipped. The zip action will remove the original file. Make sure to also exclude zip files from this rule so created zip files won't be cleaned up on subsequent runs.
limit A file size or age Define the max. age as 1y, 1d, 2h or the file size as 1M, 1GB, 1000B. Supported units for the age are m, h, d, w, y. Supported units for the size are B, KB, MB, GB, TB, PB.

Run

You can run scrubber from the command line. The following options are available:

Param Default Description
-config scrubber.config.toml The path to your configuration file.
-pretend false If specified scrubber will log actions but not execute them.
# Check your config and see what will be done
./scrubber -config scrubber.config.toml -pretend
# Execute the action
./scrubber -config scrubber.config.toml

Build

To generate your own scrubber binary execute the following steps:

dep ensure
go build

Documentation

Overview

Package scrubber provides an easy way to clean up old files in a directory.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Filesystem

type Filesystem interface {
	Name(file os.FileInfo) string
	FullPath(file os.FileInfo, dir string) string
	Remove(path string) error
	Open(name string) (*os.File, error)
	Create(name string) (*os.File, error)
	Stat(name string) (os.FileInfo, error)
	ListFiles(path string) ([]os.FileInfo, error)
	Ext(file os.FileInfo) string
}

Filesystem represents the minimal fs implementation we expect.

type OSFilesystem

type OSFilesystem struct {
}

OSFilesystem proxies calls to the underlying os and file library calls.

func (OSFilesystem) Create

func (fs OSFilesystem) Create(name string) (*os.File, error)

Create creates a file on the filesystem.

func (OSFilesystem) Ext

func (fs OSFilesystem) Ext(file os.FileInfo) string

Ext returns a file's extension.

func (OSFilesystem) FullPath

func (fs OSFilesystem) FullPath(file os.FileInfo, dir string) string

FullPath combines a file's name and it's path to a full path string.

func (OSFilesystem) ListFiles

func (fs OSFilesystem) ListFiles(path string) ([]os.FileInfo, error)

ListFiles returns a os.FileInfo for every file in a directory.

func (OSFilesystem) Name

func (fs OSFilesystem) Name(file os.FileInfo) string

Name returns the name of a file.

func (OSFilesystem) Open

func (fs OSFilesystem) Open(name string) (*os.File, error)

Open reads a file from the filesystem.

func (OSFilesystem) Remove

func (fs OSFilesystem) Remove(path string) error

Remove deletes a file from the filesystem.

func (OSFilesystem) Stat

func (fs OSFilesystem) Stat(name string) (os.FileInfo, error)

Stat returns information to a specific file.

type Scrubber

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

Scrubber holds the configuration and a filesystem handle.

func New

func New(c *TomlConfig, fs Filesystem, log logger, pretend bool) *Scrubber

New returns a new instance of Scrubber.

func (Scrubber) Scrub

func (s Scrubber) Scrub() ([]os.FileInfo, error)

Scrub performs the actual cleanup.

type Strategy

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

Strategy represents an action to take with files.

type StrategyAction

type StrategyAction string

StrategyAction represents the action that should be taken for matching files.

const (
	// ActionTypeDelete is used to delete old files.
	ActionTypeDelete StrategyAction = "delete"
	// ActionTypeZip is used to zip old files.
	ActionTypeZip StrategyAction = "zip"
)

type StrategyConfig

type StrategyConfig struct {
	Type   StrategyType
	Action StrategyAction
	Limit  string
}

StrategyConfig holds all specified strategies for a single Directory.

type StrategyType

type StrategyType string

StrategyType defines how to decide what files should be cleaned up.

const (
	// StrategyTypeAge makes files past a certain age to be deleted.
	StrategyTypeAge StrategyType = "age"
	// StrategyTypeSize makes files past a certain size to be deleted.
	StrategyTypeSize StrategyType = "size"
)

type TomlConfig

type TomlConfig struct {
	Title       string
	Directories []directory `toml:"directory"`
}

TomlConfig holds the complete structure of the scrubber config file.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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