Compare commits
18 Commits
b2365ea824
...
4d39d84733
Author | SHA1 | Date | |
---|---|---|---|
4d39d84733 | |||
e5b9bc0446 | |||
ec4c4509dc | |||
26162a9e38 | |||
bd581fd8d7 | |||
e77432e3ab | |||
001a654e37 | |||
c5574edc54 | |||
50e4d68717 | |||
c7830ceb6f | |||
b6f859efbb | |||
7f14698824 | |||
2a9a98172f | |||
282215cf9c | |||
ae7a14b6f1 | |||
8acdb20e5b | |||
5582744073 | |||
84d606fa80 |
6
CHANGELOG.md
Normal file
6
CHANGELOG.md
Normal file
@ -0,0 +1,6 @@
|
||||
# Change log
|
||||
|
||||
## 2.0.0 (unreleased)
|
||||
|
||||
- Rewrite from Bash to Python
|
||||
- Add support for push notifications (#24)
|
19
backupbot.py
19
backupbot.py
@ -42,7 +42,7 @@ sys.excepthook = handle_exception
|
||||
@click.option('-l', '--log', 'loglevel')
|
||||
@click.option('-m', '--machine-logs', 'machine_logs', is_flag=True)
|
||||
@click.option('service', '--host', '-h', envvar='SERVICE')
|
||||
@click.option('repository', '--repo', '-r', envvar='RESTIC_REPOSITORY', required=True)
|
||||
@click.option('repository', '--repo', '-r', envvar='RESTIC_REPOSITORY')
|
||||
def cli(loglevel, service, repository, machine_logs):
|
||||
global SERVICE
|
||||
if service:
|
||||
@ -54,21 +54,25 @@ def cli(loglevel, service, repository, machine_logs):
|
||||
if not isinstance(numeric_level, int):
|
||||
raise ValueError('Invalid log level: %s' % loglevel)
|
||||
logger.setLevel(numeric_level)
|
||||
logHandler = logging.StreamHandler()
|
||||
if machine_logs:
|
||||
logHandler = logging.StreamHandler()
|
||||
formatter = jsonlogger.JsonFormatter(
|
||||
"%(levelname)s %(filename)s %(lineno)s %(process)d %(message)s", rename_fields={"levelname": "message_type"})
|
||||
logHandler.setFormatter(formatter)
|
||||
logger.addHandler(logHandler)
|
||||
logger.addHandler(logHandler)
|
||||
|
||||
export_secrets()
|
||||
init_repo()
|
||||
|
||||
|
||||
def init_repo():
|
||||
repo = os.environ['RESTIC_REPOSITORY']
|
||||
if repo:= os.environ.get('RESTIC_REPOSITORY_FILE'):
|
||||
# RESTIC_REPOSITORY_FILE and RESTIC_REPOSITORY are mutually exclusive
|
||||
del os.environ['RESTIC_REPOSITORY']
|
||||
else:
|
||||
repo = os.environ['RESTIC_REPOSITORY']
|
||||
restic.repository = repo
|
||||
logger.debug(f"set restic repository location: {repo}")
|
||||
restic.repository = repo
|
||||
restic.password_file = '/var/run/secrets/restic_password'
|
||||
try:
|
||||
restic.cat.config()
|
||||
@ -146,7 +150,7 @@ def copy_secrets(apps):
|
||||
if (app_name in apps and
|
||||
(app_secs := s.attrs['Spec']['TaskTemplate']['ContainerSpec'].get('Secrets'))):
|
||||
if not container_by_service.get(s.name):
|
||||
logger.error(
|
||||
logger.warning(
|
||||
f"Container {s.name} is not running, secrets can not be copied.")
|
||||
continue
|
||||
container_id = container_by_service[s.name].id
|
||||
@ -157,6 +161,7 @@ def copy_secrets(apps):
|
||||
f"For the secret {sec['SecretName']} the file {src} does not exist for {s.name}")
|
||||
continue
|
||||
dst = SECRET_PATH + sec['SecretName']
|
||||
logger.debug("Copy Secret {sec['SecretName']}")
|
||||
copyfile(src, dst)
|
||||
|
||||
|
||||
@ -184,6 +189,8 @@ def run_commands(commands):
|
||||
def backup_volumes(backup_paths, apps, retries, dry_run=False):
|
||||
while True:
|
||||
try:
|
||||
logger.info("Start volume backup")
|
||||
logger.debug(backup_paths)
|
||||
result = restic.backup(backup_paths, dry_run=dry_run, tags=apps)
|
||||
logger.summary("backup finished", extra=result)
|
||||
return
|
||||
|
Loading…
x
Reference in New Issue
Block a user