Added container spec
parent
2bc4c22c5f
commit
310ac3784e
|
@ -2,7 +2,7 @@ from __future__ import annotations
|
||||||
import yaml
|
import yaml
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List, Union
|
from typing import List, Union
|
||||||
from specs import Spec, DirectorySpec, VolumeSpec
|
from specs import Spec, DirectorySpec, VolumeSpec, ContainerSpec
|
||||||
import skeleton
|
import skeleton
|
||||||
|
|
||||||
|
|
||||||
|
@ -10,7 +10,11 @@ def read_specs_file(path: Union[str, Path]) -> List[Spec]:
|
||||||
with open(path, "r") as yaml_file:
|
with open(path, "r") as yaml_file:
|
||||||
data = yaml.safe_load(yaml_file)
|
data = yaml.safe_load(yaml_file)
|
||||||
|
|
||||||
categories = [("directories", DirectorySpec), ("volumes", VolumeSpec)]
|
categories = [
|
||||||
|
("directories", DirectorySpec),
|
||||||
|
("volumes", VolumeSpec),
|
||||||
|
("containers", ContainerSpec),
|
||||||
|
]
|
||||||
|
|
||||||
specs = []
|
specs = []
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
from .spec import Spec
|
from .spec import Spec
|
||||||
from .directory import DirectorySpec
|
from .directory import DirectorySpec
|
||||||
from .volume import VolumeSpec
|
from .volume import VolumeSpec
|
||||||
|
from .container import ContainerSpec
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
from .spec import Spec
|
||||||
|
from typing import Union
|
||||||
|
from pathlib import Path
|
||||||
|
from datetime import datetime
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
|
class ContainerSpec(Spec):
|
||||||
|
"""
|
||||||
|
A spec for backing up via a container.
|
||||||
|
"""
|
||||||
|
|
||||||
|
_SKEL = {"container": None, "command": None, "mountpoint": "/from"}
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
name: str,
|
||||||
|
container: str,
|
||||||
|
destination: Union[str, Path],
|
||||||
|
limit: int,
|
||||||
|
command: str,
|
||||||
|
extension: str,
|
||||||
|
mountpoint: str,
|
||||||
|
notify=None,
|
||||||
|
):
|
||||||
|
super().__init__(name, destination, limit, extension, notify)
|
||||||
|
|
||||||
|
self.container = container
|
||||||
|
self.mountpoint = mountpoint
|
||||||
|
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 exec -t '{}' {} > '{}/{}'".format(
|
||||||
|
self.container, self.command, self.destination, filename
|
||||||
|
),
|
||||||
|
shell=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
if self.notifier:
|
||||||
|
self.notifier.notify("backup", self.name, process.returncode)
|
|
@ -7,7 +7,7 @@ import subprocess
|
||||||
|
|
||||||
class VolumeSpec(Spec):
|
class VolumeSpec(Spec):
|
||||||
"""
|
"""
|
||||||
A spec for backup up a Docker volume.
|
A spec for backing up a Docker volume.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_SKEL = {
|
_SKEL = {
|
||||||
|
|
10
backups.yaml
10
backups.yaml
|
@ -27,9 +27,13 @@ specs:
|
||||||
limit: 7
|
limit: 7
|
||||||
|
|
||||||
containers:
|
containers:
|
||||||
nextcloud_app:
|
gogs-db:
|
||||||
cmd: ''
|
container: 'test_db_1'
|
||||||
extension: 'tar.gz'
|
destination: '/tmp/to'
|
||||||
|
limit: 7
|
||||||
|
command: 'pg_dump -U gogs gogs'
|
||||||
|
extension: 'sql'
|
||||||
|
|
||||||
directories:
|
directories:
|
||||||
test:
|
test:
|
||||||
destination: "/home/jjr/test"
|
destination: "/home/jjr/test"
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
defaults:
|
||||||
|
directories:
|
||||||
|
notify:
|
||||||
|
title: 'yeet'
|
||||||
|
events:
|
||||||
|
- 'random'
|
||||||
|
|
||||||
|
limit: 7
|
||||||
|
|
||||||
|
specs:
|
||||||
|
directories:
|
||||||
|
test:
|
||||||
|
source: '/tmp/from'
|
||||||
|
destination: '/tmp/to'
|
Loading…
Reference in New Issue