refactor(backups): use single repo and multiplex by tags

restic-rewrite-api
Houkime 2023-05-17 17:48:06 +00:00
parent a6b3a5e590
commit deb857bca9
2 changed files with 18 additions and 13 deletions

View File

@ -8,4 +8,5 @@ class LocalFileBackup(AbstractBackupProvider):
# login and key args are for compatibility with generic provider methods. They are ignored. # login and key args are for compatibility with generic provider methods. They are ignored.
def __init__(self, filename: str, login: str = "", key: str = ""): def __init__(self, filename: str, login: str = "", key: str = ""):
super().__init__() super().__init__()
self.backuper = ResticBackuper("", "", f":local:{filename}/") self.backuper = ResticBackuper("", "", ":local:")
self.backuper.set_creds("", "", filename)

View File

@ -18,15 +18,17 @@ class ResticBackuper(AbstractBackuper):
self.type = type self.type = type
self.account = "" self.account = ""
self.key = "" self.key = ""
self.repo = ""
def set_creds(self, account: str, key: str): def set_creds(self, account: str, key: str, repo: str):
self.account = account self.account = account
self.key = key self.key = key
self.repo = repo
def restic_repo(self, repository_name: str) -> str: def restic_repo(self) -> str:
# https://restic.readthedocs.io/en/latest/030_preparing_a_new_repo.html#other-services-via-rclone # https://restic.readthedocs.io/en/latest/030_preparing_a_new_repo.html#other-services-via-rclone
# https://forum.rclone.org/t/can-rclone-be-run-solely-with-command-line-options-no-config-no-env-vars/6314/5 # https://forum.rclone.org/t/can-rclone-be-run-solely-with-command-line-options-no-config-no-env-vars/6314/5
return f"rclone:{self.type}{repository_name}/sfbackup" return f"rclone:{self.type}{self.repo}"
def rclone_args(self): def rclone_args(self):
return "rclone.args=serve restic --stdio" + self.backend_rclone_args() return "rclone.args=serve restic --stdio" + self.backend_rclone_args()
@ -44,16 +46,23 @@ class ResticBackuper(AbstractBackuper):
def _password_command(self): def _password_command(self):
return f"echo {LocalBackupSecret.get()}" return f"echo {LocalBackupSecret.get()}"
def restic_command(self, repo_name: str, *args): def restic_command(self, *args, branch_name: str = ""):
command = [ command = [
"restic", "restic",
"-o", "-o",
self.rclone_args(), self.rclone_args(),
"-r", "-r",
self.restic_repo(repo_name), self.restic_repo(),
"--password-command", "--password-command",
self._password_command(), self._password_command(),
] ]
if branch_name != "":
command.extend(
[
"--tag",
branch_name,
]
)
if args != []: if args != []:
command.extend(ResticBackuper.__flatten_list(args)) command.extend(ResticBackuper.__flatten_list(args))
return command return command
@ -78,10 +87,10 @@ class ResticBackuper(AbstractBackuper):
assert not isinstance(folders, str) assert not isinstance(folders, str)
backup_command = self.restic_command( backup_command = self.restic_command(
repo_name,
"backup", "backup",
"--json", "--json",
folders, folders,
branch_name=repo_name,
) )
with subprocess.Popen( with subprocess.Popen(
backup_command, backup_command,
@ -115,7 +124,6 @@ class ResticBackuper(AbstractBackuper):
def init(self, repo_name): def init(self, repo_name):
init_command = self.restic_command( init_command = self.restic_command(
repo_name,
"init", "init",
) )
with subprocess.Popen( with subprocess.Popen(
@ -130,7 +138,6 @@ class ResticBackuper(AbstractBackuper):
def is_initted(self, repo_name: str) -> bool: def is_initted(self, repo_name: str) -> bool:
command = self.restic_command( command = self.restic_command(
repo_name,
"check", "check",
"--json", "--json",
) )
@ -147,7 +154,6 @@ class ResticBackuper(AbstractBackuper):
Size of a snapshot Size of a snapshot
""" """
command = self.restic_command( command = self.restic_command(
repo_name,
"stats", "stats",
snapshot_id, snapshot_id,
"--json", "--json",
@ -169,7 +175,6 @@ class ResticBackuper(AbstractBackuper):
# I do not alter the signature yet because maybe this can be # I do not alter the signature yet because maybe this can be
# changed with flags # changed with flags
restore_command = self.restic_command( restore_command = self.restic_command(
repo_name,
"restore", "restore",
snapshot_id, snapshot_id,
"--target", "--target",
@ -190,7 +195,6 @@ class ResticBackuper(AbstractBackuper):
raises Value Error if repo does not exist raises Value Error if repo does not exist
""" """
listing_command = self.restic_command( listing_command = self.restic_command(
repo_name,
"snapshots", "snapshots",
"--json", "--json",
) )
@ -217,7 +221,7 @@ class ResticBackuper(AbstractBackuper):
snapshot = Snapshot( snapshot = Snapshot(
id=restic_snapshot["short_id"], id=restic_snapshot["short_id"],
created_at=restic_snapshot["time"], created_at=restic_snapshot["time"],
service_name=repo_name, service_name=restic_snapshot["tags"][0],
) )
snapshots.append(snapshot) snapshots.append(snapshot)