forked from coop-cloud/backup-bot-two
chore: formatting
This commit is contained in:
parent
359140781e
commit
79d19e7ac5
23
backupbot.py
23
backupbot.py
|
@ -57,7 +57,7 @@ def export_secrets():
|
|||
if env.endswith('FILE') and not "COMPOSE_FILE" in env:
|
||||
logging.debug(f"exported secret: {env}")
|
||||
with open(os.environ[env]) as file:
|
||||
secret = file.read()
|
||||
secret = file.read()
|
||||
os.environ[env.removesuffix('_FILE')] = secret
|
||||
# logging.debug(f"Read secret value: {secret}")
|
||||
|
||||
|
@ -166,9 +166,11 @@ def restore(snapshot, target, noninteractive):
|
|||
if not noninteractive:
|
||||
snapshot_date = datetime.fromisoformat(snapshots[0]['time'])
|
||||
delta = datetime.now(tz=timezone.utc) - snapshot_date
|
||||
print(f"You are going to restore Snapshot {snapshot} of {service_paths} at {target}")
|
||||
print(
|
||||
f"You are going to restore Snapshot {snapshot} of {service_paths} at {target}")
|
||||
print(f"This snapshot is {delta} old")
|
||||
print(f"THIS COMMAND WILL IRREVERSIBLY OVERWRITES {target}{service_paths.removeprefix('/')}")
|
||||
print(
|
||||
f"THIS COMMAND WILL IRREVERSIBLY OVERWRITES {target}{service_paths.removeprefix('/')}")
|
||||
prompt = input("Type YES (uppercase) to continue: ")
|
||||
if prompt != 'YES':
|
||||
logging.error("Restore aborted")
|
||||
|
@ -221,7 +223,7 @@ def list_files(snapshot, path):
|
|||
logging.error(err_msg)
|
||||
exit(1)
|
||||
else:
|
||||
raise error
|
||||
raise error
|
||||
output = output.replace('}\n{', '}|{')
|
||||
results = list(map(json.loads, output.split('|')))
|
||||
return results
|
||||
|
@ -234,7 +236,8 @@ def list_files(snapshot, path):
|
|||
@click.option('secrets', '--secrets', '-c', is_flag=True)
|
||||
def download(snapshot, path, volumes, secrets):
|
||||
if sum(map(bool, [path, volumes, secrets])) != 1:
|
||||
logging.error("Please specify exactly one of '--path', '--volumes', '--secrets'")
|
||||
logging.error(
|
||||
"Please specify exactly one of '--path', '--volumes', '--secrets'")
|
||||
exit(1)
|
||||
if path:
|
||||
path = path.removesuffix('/')
|
||||
|
@ -254,7 +257,7 @@ def download(snapshot, path, volumes, secrets):
|
|||
filename = f"/tmp/{SERVICE}.tar"
|
||||
files = list_files(snapshot, VOLUME_PATH)
|
||||
for f in files[1:]:
|
||||
path = f[ 'path' ]
|
||||
path = f['path']
|
||||
if SERVICE in path and f['type'] == 'dir':
|
||||
content = dump(snapshot, path)
|
||||
# Concatenate tar files (extract with tar -xi)
|
||||
|
@ -268,7 +271,7 @@ def download(snapshot, path, volumes, secrets):
|
|||
files = list_files(snapshot, SECRET_PATH)
|
||||
secrets = {}
|
||||
for f in files[1:]:
|
||||
path = f[ 'path' ]
|
||||
path = f['path']
|
||||
if SERVICE in path and f['type'] == 'file':
|
||||
secret = dump(snapshot, path).decode()
|
||||
secret_name = path.removeprefix(f'{SECRET_PATH}{SERVICE}_')
|
||||
|
@ -277,15 +280,17 @@ def download(snapshot, path, volumes, secrets):
|
|||
json.dump(secrets, file)
|
||||
print(filename)
|
||||
|
||||
|
||||
def dump(snapshot, path):
|
||||
cmd = restic.cat.base_command() + ['dump']
|
||||
if SERVICE:
|
||||
cmd = cmd + ['--tag', SERVICE]
|
||||
cmd = cmd +[snapshot, path]
|
||||
cmd = cmd + [snapshot, path]
|
||||
logging.debug(f"Dumping {path} from snapshot '{snapshot}'")
|
||||
output = subprocess.run(cmd, capture_output=True)
|
||||
if output.returncode:
|
||||
logging.error(f"error while dumping {path} from snapshot '{snapshot}': {output.stderr}")
|
||||
logging.error(
|
||||
f"error while dumping {path} from snapshot '{snapshot}': {output.stderr}")
|
||||
exit(1)
|
||||
return output.stdout
|
||||
|
||||
|
|
Loading…
Reference in New Issue