backup-tool/app/specs/volume.py

59 lines
1.4 KiB
Python
Raw Normal View History

2021-01-15 21:47:09 +01:00
from .spec import Spec
from typing import Union
from pathlib import Path
from datetime import datetime
import subprocess
class VolumeSpec(Spec):
"""
2021-01-15 22:00:56 +01:00
A spec for backing up a Docker volume.
2021-01-15 21:47:09 +01:00
"""
_SKEL = {
"volume": None,
"image": "alpine:latest",
"command": "tar -czf '/to/{filename}' .",
}
def __init__(
self,
name: str,
volume: str,
image: str,
destination: Union[str, Path],
limit: int,
command: str,
extension: str,
notify=None,
):
super().__init__(name, destination, limit, extension, notify)
self.volume = volume
self.image = image
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
)
process = subprocess.run(
"docker run --rm -v '{}:/from' -v '{}:/to' -w /from '{}' {}".format(
self.volume,
self.destination,
self.image,
self.command.format(
filename=filename,
),
),
shell=True,
)
if self.notifier:
self.notifier.notify("backup", self.name, process.returncode)