Commit 87a54c18 authored by James Valleroy's avatar James Valleroy

backups: Use backups API for restore

Signed-off-by: James Valleroy's avatarJames Valleroy <jvalleroy@mailbox.org>
parent cf23f6b1
...@@ -25,6 +25,7 @@ import glob ...@@ -25,6 +25,7 @@ import glob
import json import json
import os import os
import subprocess import subprocess
import sys
import tarfile import tarfile
REPOSITORY = '/var/lib/freedombox/borgbackup' REPOSITORY = '/var/lib/freedombox/borgbackup'
...@@ -170,12 +171,26 @@ def subcommand_get_export_apps(arguments): ...@@ -170,12 +171,26 @@ def subcommand_get_export_apps(arguments):
def subcommand_restore(arguments): def subcommand_restore(arguments):
"""Restore files from an exported archive.""" """Restore files from an exported archive."""
prev_dir = os.getcwd() locations_data = ''.join(sys.stdin)
try: locations = json.loads(locations_data)
os.chdir('/')
subprocess.run(['tar', 'xf', arguments.filename], check=True) found_file = False
finally: with tarfile.open(arguments.filename) as t:
os.chdir(prev_dir) for member in t.getmembers():
path = '/' + member.name
if path in locations['files']:
t.extract(member, '/')
found_file = True
else:
for d in locations['directories']:
if path.startswith(d):
t.extract(member, '/')
found_file = True
break
if not found_file:
print('No matching files or directories found in archive:', locations)
sys.exit(1)
def main(): def main():
......
...@@ -29,7 +29,7 @@ from plinth import actions ...@@ -29,7 +29,7 @@ from plinth import actions
from plinth.menu import main_menu from plinth.menu import main_menu
from plinth.modules import udiskie from plinth.modules import udiskie
from .backups import backup_apps from .backups import backup_apps, restore_apps
version = 1 version = 1
...@@ -162,13 +162,24 @@ def get_export_apps(filename): ...@@ -162,13 +162,24 @@ def get_export_apps(filename):
return output.splitlines() return output.splitlines()
def _restore_handler(packet):
"""Perform restore operation on packet."""
locations = {
'directories': packet.directories,
'files': packet.files
}
locations_data = json.dumps(locations)
actions.superuser_run(
'backups', ['restore', '--filename', packet.label],
input=locations_data.encode())
def restore_exported(label, name, apps=None): def restore_exported(label, name, apps=None):
"""Restore files from exported backup archive.""" """Restore files from exported backup archive."""
filename = find_exported_archive(label, name) filename = find_exported_archive(label, name)
if filename: if filename:
# TODO: Use backups API. restore_apps(_restore_handler, app_names=apps, create_subvolume=False,
actions.superuser_run( backup_file=filename)
'backups', ['restore', '--filename', filename])
else: else:
raise FileNotFoundError( raise FileNotFoundError(
errno.ENOENT, os.strerror(errno.ENOENT), filename) errno.ENOENT, os.strerror(errno.ENOENT), filename)
...@@ -152,7 +152,8 @@ def backup_apps(backup_handler, app_names=None, label=None): ...@@ -152,7 +152,8 @@ def backup_apps(backup_handler, app_names=None, label=None):
_lockdown_apps(apps, lockdown=False) _lockdown_apps(apps, lockdown=False)
def restore_apps(restore_handler, app_names=None, create_subvolume=True): def restore_apps(restore_handler, app_names=None, create_subvolume=True,
backup_file=None):
"""Restore data belonging to a set of applications.""" """Restore data belonging to a set of applications."""
if not app_names: if not app_names:
apps = _list_of_all_apps_for_backup() apps = _list_of_all_apps_for_backup()
...@@ -171,7 +172,7 @@ def restore_apps(restore_handler, app_names=None, create_subvolume=True): ...@@ -171,7 +172,7 @@ def restore_apps(restore_handler, app_names=None, create_subvolume=True):
restore_root = '/' restore_root = '/'
subvolume = False subvolume = False
packet = Packet('restore', 'apps', restore_root, manifests) packet = Packet('restore', 'apps', restore_root, manifests, backup_file)
_run_operation(restore_handler, packet) _run_operation(restore_handler, packet)
if subvolume: if subvolume:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment