backup-tool/app/specs/directory.py

59 lines
1.5 KiB
Python

from .spec import Spec
from pathlib import Path
from typing import Union
import subprocess
from datetime import datetime
class DirectorySpec(Spec):
"""A spec for backing up a local directory."""
_SKEL = {
"source": None,
"command": "tar -czf '{destination}/{filename}' .",
}
def __init__(
self,
name: str,
source: Union[str, Path],
destination: Union[str, Path],
limit: int,
command: str,
extension: str,
notify=None,
):
super().__init__(name, destination, limit, extension, notify)
self.source = source if type(source) == Path else Path(source)
# Check existence of source directory
if not self.source.exists() or not self.source.is_dir():
raise NotADirectoryError(
"{} doesn't exist or isn't a directory.".format(self.source)
)
self.command = command
def backup(self):
# Remove excess backups
self.remove_backups()
# Run actual backup command
filename = "{}.{}".format(
datetime.now().strftime("%Y-%m-%d_%H-%M-%S"), self.extension
)
# TODO add logging stuff
process = subprocess.run(
self.command.format(
destination=self.destination,
filename=filename,
),
cwd=self.source,
shell=True,
)
if self.notifier:
self.notifier.notify("backup", self.name, process.returncode)