Refactor backup script

dev
Marko Semet 2020-04-09 18:43:43 +02:00
parent 6c2e2661c3
commit 52b61aafaa
2 changed files with 20 additions and 15 deletions

View File

@ -27,6 +27,23 @@ class ConfigAndBackup():
instance_hash = self._hash_instance()
return list(map(lambda x: x[len(instance_hash) + 1:], filter(lambda x: x.startswith(instance_hash), archives)))
async def _run_backup(self, name: bytes, *args, path:bytes=None):
# Check if exists (when true delete it)
archive_name = b"%s::%s" % (paths.BACKUP_DIR.encode(), name)
if name in await self._list_installed_hashes():
process = await asyncio.create_subprocess_exec(b"borg", b"delete", archive_name)
await process.wait()
if process.returncode != 0:
raise RuntimeError("Backup can't be created.")
# Create backup
if path is None:
path = self.__instance.get_instance_path().encode()
process = await asyncio.create_subprocess_exec(b"borg", b"create", b"-C", b"zstd,5", archive_name, *args, cwd=path)
await process.wait()
if process.returncode != 0:
raise RuntimeError("Backup can't be created.")
def __init__(self, instance):
# Set values
self.__instance = instance
@ -47,17 +64,5 @@ class ConfigAndBackup():
if process.returncode != 0:
raise RuntimeError("Borg repo is broken.")
async def gen_wine_backup(self, name:str):
# Check if exists (when true delete it)
archive_name = b"%s::%s_%s" % (paths.BACKUP_DIR.encode(), self._hash_instance().encode(), name.encode())
if name in await self._list_installed_hashes():
process = await asyncio.create_subprocess_exec(b"borg", b"delete", archive_name)
await process.wait()
if process.returncode != 0:
raise RuntimeError("Backup can't be created.")
# Create backup
process = await asyncio.create_subprocess_exec(b"borg", b"create", b"-C", b"zstd,5", archive_name, b".", cwd=self.__instance.get_instance_path().encode())
await process.wait()
if process.returncode != 0:
raise RuntimeError("Backup can't be created.")
async def gen_install_backup(self, name:str):
await self._run_backup(name.encode(), b".")

View File

@ -130,7 +130,7 @@ class Instance():
# Gen backup and return
next_hash = self.__gen_hash(last_hash, name, await step.content_to_hash(self.__wine, self))
await self.__backup.gen_wine_backup(next_hash)
await self.__backup.gen_install_backup(next_hash)
return next_hash.encode("UTF-8")
async def install(self):