grebakker

API

API

grebakker - greyrat's backupper for hackers.

Log

Handles logging of performed actions in CSV or JSON format.

__init__(name, restart, log_format, off)

Initialize the Log object.

Parameters:

Name Type Description Default
name str

Path to the log file

required
restart bool

If True, overwrites the existing log file

required
log_format str

Format of the log ('csv' or 'json')

required
off bool

If True, disables logging

required

write(act, src, dst, duration)

Write a log entry about a performed action.

Parameters:

Name Type Description Default
act str

Action performed

required
src str

Source path

required
dst str

Destination path

required
duration str

Duration of the action

required

error(error)

Write a log entry about an error.

Parameters:

Name Type Description Default
error str

The error occured

required

close()

Close the log file.

Grebakker

Perform backup operations.

__init__(dest, log, verbosity)

Initialize the Grebakker object.

Parameters:

Name Type Description Default
dest str

Destination directory for backups

required
log Log

Log object for recording actions

required
verbosity int

Verbosity level for output

required

_action_begin(mml_action, path, level)

Report the beginning of an action, return starting time.

Parameters:

Name Type Description Default
mml_action str

Description of the action

required
path str

Path involved in the action

required
level int

Indentation level

required

Returns:

Type Description
datetime

Start time of the action.

_action_end(action, path, dst, level, t1)

Report the end of an action and logs it.

Parameters:

Name Type Description Default
action str

Action performed

required
path str

Source path

required
dst str

Destination path

required
level int

Indentation level

required
t1 datetime

Start time of the action

required

_yield_files(src, exclude)

Yield files from the source directory, excluding specified patterns.

Parameters:

Name Type Description Default
src str

Source directory.

required
exclude List[str]

List of patterns to exclude.

required

Yields:

Name Type Description
source_path str

Relative path to each file

_i(level)

Return indentation spaces for the given output level.

Parameters:

Name Type Description Default
level int

Indentation level.

required

Returns:

Type Description
str

Indentation spaces

_destination_exists(action, src, dst, level)

Determine the destination path for an action.

Parameters:

Name Type Description Default
action str

Action being performed

required
src str

Source path

required
dst str

Destination path

required
level int

Indentation level

required

Returns:

Type Description
str or None

Destination path or None if the destination file exists.

copy(src_root, item, dst_root, level)

Copy files or directories from source to destination.

Parameters:

Name Type Description Default
src_root str

Root source directory

required
item Union[str, Dict[str, str]]

Item to copy. If dict, may contain 'name' and 'exclude'

required
dst_root str

Root destination directory

required
level int

Reporting level

required

Raises:

Type Description
FileNotFoundError

If the source path does not exist.

compress(root, item, dst_root, level)

Compress files or directories into a ZIP archive.

Parameters:

Name Type Description Default
root str

Root source directory

required
item Union[str, Dict[str, str]]

Item to compress. If dict, may contain 'name' and 'exclude'

required
dst_root str

Root destination directory

required
level int

Reporting level

required

Raises:

Type Description
FileNotFoundError

If the source path does not exist

decompress(root, item, dst_root, level)

Decompress ZIP archives.

Parameters:

Name Type Description Default
root str

Root source directory

required
item Union[str, Dict[str, str]]

Item to compress. If dict, may contain 'name' and 'exclude'

required
dst_root str

Root destination directory

required
level int

Reporting level

required

Raises:

Type Description
FileNotFoundError

If the source path does not exist

run(action, root, level=0)

Perform an action.

Parameters:

Name Type Description Default
action str

The action to perform

required
root str

Root source directory

required
level int

Reporting level

0

main(arguments=[])

Run grebaker using the given parameters.

Parameters:

Name Type Description Default
arguments List[str]

A list of command line arguments

[]

Returns:

Type Description
int

The exit code (0 for success)

script_run()

Execute from command line.