Added volume spec

recovery-function
Jef Roosens 2021-01-15 21:47:09 +01:00
parent 419fef8ddc
commit 2bc4c22c5f
4 changed files with 63 additions and 9 deletions

View File

@ -2,7 +2,7 @@ from __future__ import annotations
import yaml
from pathlib import Path
from typing import List, Union
from specs import Spec, DirectorySpec
from specs import Spec, DirectorySpec, VolumeSpec
import skeleton
@ -10,7 +10,7 @@ def read_specs_file(path: Union[str, Path]) -> List[Spec]:
with open(path, "r") as yaml_file:
data = yaml.safe_load(yaml_file)
categories = [("directories", DirectorySpec)]
categories = [("directories", DirectorySpec), ("volumes", VolumeSpec)]
specs = []

View File

@ -1,2 +1,3 @@
from .spec import Spec
from .directory import DirectorySpec
from .volume import VolumeSpec

View File

@ -0,0 +1,58 @@
from .spec import Spec
from typing import Union
from pathlib import Path
from datetime import datetime
import subprocess
class VolumeSpec(Spec):
"""
A spec for backup up a Docker volume.
"""
_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)

View File

@ -22,15 +22,10 @@ specs:
# Each spec has a name, which can be used in the destination field
# e.g. in the default value
test-spec:
source: '/some/path'
destination: '/some/other/{name}'
volume: 'media_nefarious-config'
destination: '/tmp/to'
limit: 7
test-2:
source: '/path/to'
destination: '/to/some/other/path'
limit: 2
containers:
nextcloud_app:
cmd: ''