extend download to download the secrets or all app volumes at once
Some checks failed
continuous-integration/drone/pr Build is failing
Some checks failed
continuous-integration/drone/pr Build is failing
This commit is contained in:
parent
d47ff6caac
commit
328adffdc1
74
backupbot.py
74
backupbot.py
@ -195,7 +195,10 @@ def ls(snapshot, path):
|
|||||||
|
|
||||||
|
|
||||||
def list_files(snapshot, path):
|
def list_files(snapshot, path):
|
||||||
cmd = restic.cat.base_command() + ['ls', snapshot]
|
cmd = restic.cat.base_command() + ['ls']
|
||||||
|
if SERVICE:
|
||||||
|
cmd = cmd + ['--tag', SERVICE]
|
||||||
|
cmd.append(snapshot)
|
||||||
if path:
|
if path:
|
||||||
cmd.append(path)
|
cmd.append(path)
|
||||||
output = restic.internal.command_executor.execute(cmd)
|
output = restic.internal.command_executor.execute(cmd)
|
||||||
@ -207,18 +210,63 @@ def list_files(snapshot, path):
|
|||||||
@cli.command()
|
@cli.command()
|
||||||
@click.option('snapshot', '--snapshot', '-s', envvar='SNAPSHOT', default='latest')
|
@click.option('snapshot', '--snapshot', '-s', envvar='SNAPSHOT', default='latest')
|
||||||
@click.option('path', '--path', '-p', envvar='INCLUDE_PATH')
|
@click.option('path', '--path', '-p', envvar='INCLUDE_PATH')
|
||||||
def download(snapshot, path):
|
@click.option('volumes', '--volumes', '-v', is_flag=True)
|
||||||
path = path.removesuffix('/')
|
@click.option('secrets', '--secrets', '-c', is_flag=True)
|
||||||
files = list_files(snapshot, path)
|
def download(snapshot, path, volumes, secrets):
|
||||||
filetype = [f.get('type') for f in files if f.get('path') == path][0]
|
if sum(map(bool, [path, volumes, secrets])) != 1:
|
||||||
cmd = restic.cat.base_command() + ['dump', snapshot, path]
|
logging.error("Please specify exactly one of '--path', '--volumes', '--secrets'")
|
||||||
output = subprocess.run(cmd, capture_output=True).stdout
|
exit(1)
|
||||||
filename = "/tmp/" + Path(path).name
|
if path:
|
||||||
if filetype == 'dir':
|
path = path.removesuffix('/')
|
||||||
filename = filename + ".tar"
|
files = list_files(snapshot, path)
|
||||||
with open(filename, "wb") as file:
|
filetype = [f.get('type') for f in files if f.get('path') == path][0]
|
||||||
file.write(output)
|
filename = "/tmp/" + Path(path).name
|
||||||
print(filename)
|
if filetype == 'dir':
|
||||||
|
filename = filename + ".tar"
|
||||||
|
output = dump(snapshot, path)
|
||||||
|
with open(filename, "wb") as file:
|
||||||
|
file.write(output)
|
||||||
|
print(filename)
|
||||||
|
elif volumes:
|
||||||
|
if not SERVICE:
|
||||||
|
logging.error("Please specify '--host' when using '--volumes'")
|
||||||
|
exit(1)
|
||||||
|
filename = f"/tmp/{SERVICE}.tar"
|
||||||
|
files = list_files(snapshot, VOLUME_PATH)
|
||||||
|
for f in files[1:]:
|
||||||
|
path = f[ 'path' ]
|
||||||
|
if SERVICE in path and f['type'] == 'dir':
|
||||||
|
content = dump(snapshot, path)
|
||||||
|
# Concatenate tar files (extract with tar -xi)
|
||||||
|
with open(filename, "ab") as file:
|
||||||
|
file.write(content)
|
||||||
|
elif secrets:
|
||||||
|
if not SERVICE:
|
||||||
|
logging.error("Please specify '--host' when using '--secrets'")
|
||||||
|
exit(1)
|
||||||
|
filename = f"/tmp/SECRETS_{SERVICE}.json"
|
||||||
|
files = list_files(snapshot, SECRET_PATH)
|
||||||
|
secrets = {}
|
||||||
|
for f in files[1:]:
|
||||||
|
path = f[ 'path' ]
|
||||||
|
if SERVICE in path and f['type'] == 'file':
|
||||||
|
secret = dump(snapshot, path).decode()
|
||||||
|
secrets[path.removeprefix(SERVICE)] = secret
|
||||||
|
with open(filename, "w") as file:
|
||||||
|
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]
|
||||||
|
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}")
|
||||||
|
exit(1)
|
||||||
|
return output.stdout
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Loading…
x
Reference in New Issue
Block a user