-
Notifications
You must be signed in to change notification settings - Fork 0
/
backee.py
executable file
·74 lines (55 loc) · 1.91 KB
/
backee.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
import argparse
import socket
import logging
from backee.parser.config_parser import parse_config
from backee.logger.loggers import (
setup_default_loggers,
setup_config_loggers,
setup_uncaught_exceptions_logger,
)
from backee.backup.backup import backup
log = logging.getLogger(__name__)
def main():
setup_uncaught_exceptions_logger()
setup_default_loggers()
args = _get_args()
config = parse_config(args.config)
setup_config_loggers(config.loggers)
_get_lock("backee")
backup(config.name, config.backup_items, config.backup_servers)
def _get_args():
parser = argparse.ArgumentParser(
description="backee is a script for backup directories, files, databases and docker data volumes."
)
config_default_path = "backee/config.yml"
parser.add_argument(
"-c",
"--config",
action="store",
default=config_default_path,
type=str,
help=f"path to config file (default: {config_default_path})",
)
return parser.parse_args()
def _get_lock(process_name: str):
"""
A technique that is handy on a Linux system is using domain sockets.
It is atomic and avoids the problem of having lock files
lying around if your process gets sent a SIGKILL.
"""
# Without holding a reference to our socket somewhere it gets garbage
# collected when the function exits
_get_lock._lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
try:
# The null byte (\0) means the socket is created
# in the abstract namespace instead of being created
# on the file system itself.
_get_lock._lock_socket.bind("\0" + process_name)
log.debug("lock acquired")
except socket.error as socker_error:
raise OSError(
"Only one instance of the script is allowed to run"
) from socker_error
if __name__ == "__main__":
main()