mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
Backups: Restore directly from archive
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
1 parent
51b0950ec4
commit
c770a7adfb
7 files changed
+178
-53
No files matched your search
@@ -30,7 +30,6 @@ from plinth.menu import main_menu
|
||||
from plinth.modules import storage
|
||||
|
||||
from . import api
|
||||
from .manifest import backup
|
||||
|
||||
version = 1
|
||||
|
||||
@@ -44,13 +43,14 @@ description = [
|
||||
|
||||
service = None
|
||||
|
||||
MANIFESTS_FOLDER = '/var/lib/plinth/backups-manifests/'
|
||||
|
||||
BACKUP_FOLDER_NAME = 'FreedomBox-backups'
|
||||
DEFAULT_BACKUP_LOCATION = ('/var/lib/freedombox/', _('Root Filesystem'))
|
||||
MANIFESTS_FOLDER = '/var/lib/plinth/backups-manifests/'
|
||||
REPOSITORY = '/var/lib/freedombox/borgbackup'
|
||||
SESSION_BACKUP_VARIABLE = 'fbx-backup-filestamp'
|
||||
# default backup path for temporary actions like imports or download
|
||||
TMP_BACKUP_PATH = '/tmp/freedombox-backup.tar.gz'
|
||||
# session variable name that stores when a backup file should be deleted
|
||||
SESSION_BACKUP_VARIABLE = 'fbx-backup-filestamp'
|
||||
|
||||
|
||||
def init():
|
||||
@@ -109,8 +109,10 @@ def create_archive(name, app_names):
|
||||
|
||||
|
||||
def delete_archive(name):
|
||||
# TODO: is name actually a path?
|
||||
actions.superuser_run('backups', ['delete', '--name', name])
|
||||
|
||||
|
||||
def delete_tmp_backup_file():
|
||||
if os.path.isfile(TMP_BACKUP_PATH):
|
||||
os.remove(TMP_BACKUP_PATH)
|
||||
@@ -121,21 +123,16 @@ def export_archive(name, location, tmp_dir=False):
|
||||
if tmp_dir:
|
||||
filepath = TMP_BACKUP_PATH
|
||||
else:
|
||||
location_path = get_location_path(location)
|
||||
filepath = get_archive_path(location_path,
|
||||
get_valid_filename(name) + '.tar.gz')
|
||||
filename = get_valid_filename(name) + '.tar.gz'
|
||||
filepath = get_exported_archive_path(location, filename)
|
||||
# TODO: that's a full path, not a filename; rename argument
|
||||
actions.superuser_run('backups',
|
||||
['export', '--name', name, '--filename', filepath])
|
||||
['export-tar', '--name', name, '--filename', filepath])
|
||||
|
||||
|
||||
def get_export_locations():
|
||||
"""Return a list of storage locations for exported backup archives."""
|
||||
locations = [{
|
||||
'path': '/var/lib/freedombox/',
|
||||
'label': _('Root Filesystem'),
|
||||
'device': '/'
|
||||
}]
|
||||
locations = [DEFAULT_BACKUP_LOCATION]
|
||||
if storage.is_running():
|
||||
devices = storage.udisks2.list_devices()
|
||||
for device in devices:
|
||||
@@ -175,14 +172,27 @@ def get_export_files():
|
||||
return export_files
|
||||
|
||||
|
||||
def get_archive_path(location, archive_name):
|
||||
def get_archive_path(archive_name):
|
||||
"""Get path of an archive"""
|
||||
return "::".join([REPOSITORY, archive_name])
|
||||
|
||||
|
||||
def get_exported_archive_path(location, archive_name):
|
||||
"""Get path of an exported archive"""
|
||||
return os.path.join(location, BACKUP_FOLDER_NAME, archive_name)
|
||||
|
||||
|
||||
def find_exported_archive(device, archive_name):
|
||||
"""Return the full path for the exported archive file."""
|
||||
location_path = get_location_path(device)
|
||||
return get_archive_path(location_path, archive_name)
|
||||
return get_exported_archive_path(location_path, archive_name)
|
||||
|
||||
|
||||
def get_archive_apps(path):
|
||||
"""Get list of apps included in an archive."""
|
||||
output = actions.superuser_run('backups',
|
||||
['get-archive-apps', '--path', path])
|
||||
return output.splitlines()
|
||||
|
||||
|
||||
def get_export_apps(filename):
|
||||
@@ -192,22 +202,36 @@ def get_export_apps(filename):
|
||||
return output.splitlines()
|
||||
|
||||
|
||||
def _restore_handler(packet):
|
||||
def _restore_exported_archive_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())
|
||||
actions.superuser_run('backups', ['restore-exported-archive',
|
||||
'--path', packet.label], input=locations_data.encode())
|
||||
|
||||
|
||||
def _restore_archive_handler(packet):
|
||||
"""Perform restore operation on packet."""
|
||||
locations = {'directories': packet.directories, 'files': packet.files}
|
||||
locations_data = json.dumps(locations)
|
||||
actions.superuser_run('backups', ['restore-archive', '--path',
|
||||
packet.label, '--destination', '/'], input=locations_data.encode())
|
||||
|
||||
|
||||
def restore_from_tmp(apps=None):
|
||||
"""Restore files from temporary backup file"""
|
||||
api.restore_apps(_restore_handler, app_names=apps, create_subvolume=False,
|
||||
backup_file=TMP_BACKUP_PATH)
|
||||
api.restore_apps(_restore_exported_archive_handler, app_names=apps,
|
||||
create_subvolume=False, backup_file=TMP_BACKUP_PATH)
|
||||
|
||||
|
||||
def restore_exported(device, archive_name, apps=None):
|
||||
"""Restore files from exported backup archive."""
|
||||
filename = find_exported_archive(device, archive_name)
|
||||
api.restore_apps(_restore_handler, app_names=apps, create_subvolume=False,
|
||||
backup_file=filename)
|
||||
api.restore_apps(_restore_exported_archive_handler, app_names=apps,
|
||||
create_subvolume=False, backup_file=filename)
|
||||
|
||||
|
||||
def restore(archive_path, apps=None):
|
||||
"""Restore files from a backup archive."""
|
||||
api.restore_apps(_restore_archive_handler, app_names=apps,
|
||||
create_subvolume=False, backup_file=archive_path)
|
||||
@@ -115,6 +115,7 @@ class Packet:
|
||||
self.scope = scope
|
||||
self.root = root
|
||||
self.apps = apps
|
||||
# TODO: label is an archive path -- rename
|
||||
self.label = label
|
||||
self.errors = []
|
||||
|
||||
|
||||
@@ -26,7 +26,8 @@ from django.core.validators import FileExtensionValidator
|
||||
from django.utils.translation import ugettext, ugettext_lazy as _
|
||||
|
||||
from . import api
|
||||
from . import get_export_locations, get_archive_path, get_location_path
|
||||
from . import get_export_locations, get_exported_archive_path, \
|
||||
get_location_path
|
||||
|
||||
|
||||
def _get_app_choices(apps):
|
||||
@@ -84,9 +85,8 @@ class RestoreFromTmpForm(forms.Form):
|
||||
"""Initialize the form with selectable apps."""
|
||||
apps = kwargs.pop('apps')
|
||||
super().__init__(*args, **kwargs)
|
||||
self.fields['selected_apps'].choices = [
|
||||
(app[0], app[1].name) for app in apps]
|
||||
self.fields['selected_apps'].initial = [app[0] for app in apps]
|
||||
self.fields['selected_apps'].choices = _get_app_choices(apps)
|
||||
self.fields['selected_apps'].initial = [app.name for app in apps]
|
||||
|
||||
|
||||
class RestoreForm(forms.Form):
|
||||
@@ -131,7 +131,7 @@ class UploadForm(forms.Form):
|
||||
location_path = get_location_path(location_device)
|
||||
# if other errors occured before, 'file' won't be in cleaned_data
|
||||
if (file and file.name):
|
||||
filepath = get_archive_path(location_path, file.name)
|
||||
filepath = get_exported_archive_path(location_path, file.name)
|
||||
if os.path.exists(filepath):
|
||||
raise forms.ValidationError(
|
||||
"File %s already exists" % file.name)
|
||||
|
||||
@@ -82,14 +82,20 @@
|
||||
<tr id="archive-{{ archive.name }}" class="archive">
|
||||
<td class="archive-name">{{ archive.name }}</td>
|
||||
<td class="archive-operations">
|
||||
<!-- TODO: use or remove
|
||||
<a class="archive-export btn btn-sm btn-default"
|
||||
href="{% url 'backups:export' archive.name %}">
|
||||
{% trans "Export" %}
|
||||
</a>
|
||||
-->
|
||||
<a class="archive-export btn btn-sm btn-default" target="_blank"
|
||||
href="{% url 'backups:export-and-download' archive.name %}">
|
||||
{% trans "Download" %}
|
||||
</a>
|
||||
<a class="archive-export btn btn-sm btn-default"
|
||||
href="{% url 'backups:restore-archive' archive.name %}">
|
||||
{% trans "Restore" %}
|
||||
</a>
|
||||
<a class="archive-delete btn btn-sm btn-default"
|
||||
href="{% url 'backups:delete' archive.name %}">
|
||||
<span class="glyphicon glyphicon-trash" aria-hidden="true">
|
||||
|
||||
@@ -22,7 +22,7 @@ from django.conf.urls import url
|
||||
|
||||
from .views import IndexView, CreateArchiveView, DownloadArchiveView, \
|
||||
DeleteArchiveView, ExportArchiveView, RestoreView, UploadArchiveView, \
|
||||
ExportAndDownloadView, RestoreFromTmpView
|
||||
ExportAndDownloadView, RestoreArchiveView, RestoreFromTmpView
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^sys/backups/$', IndexView.as_view(), name='index'),
|
||||
@@ -38,6 +38,8 @@ urlpatterns = [
|
||||
url(r'^sys/backups/upload/$', UploadArchiveView.as_view(), name='upload'),
|
||||
url(r'^sys/backups/restore/(?P<device>[^/]+)/(?P<name>[^/]+)/$',
|
||||
RestoreView.as_view(), name='restore'),
|
||||
url(r'^sys/backups/restore-archive/(?P<name>[^/]+)/$',
|
||||
RestoreArchiveView.as_view(), name='restore-archive'),
|
||||
url(r'^sys/backups/restore-from-tmp/$',
|
||||
RestoreFromTmpView.as_view(), name='restore-from-tmp'),
|
||||
]
|
||||
@@ -140,21 +140,15 @@ class ExportAndDownloadView(View):
|
||||
"""View to export and download an archive."""
|
||||
def get(self, request, name):
|
||||
name = unquote(name)
|
||||
with create_temporary_backup_file(name) as filename, \
|
||||
open(filename, 'rb') as file_handle:
|
||||
(content_type, encoding) = mimetypes.guess_type(filename)
|
||||
response = HttpResponse(File(file_handle),
|
||||
content_type=content_type)
|
||||
content_disposition = 'attachment; filename="%s"' % filename
|
||||
response['Content-Disposition'] = content_disposition
|
||||
if encoding:
|
||||
response['Content-Encoding'] = encoding
|
||||
|
||||
return response
|
||||
filename = "%s.tar.gz" % name
|
||||
with create_temporary_backup_file(name) as filepath:
|
||||
return _get_file_response(filepath, filename)
|
||||
|
||||
|
||||
class create_temporary_backup_file:
|
||||
"""Create a temporary backup file that gets deleted after using it"""
|
||||
# TODO: try using export-tar with FILE parameter '-' and reading stdout:
|
||||
# https://borgbackup.readthedocs.io/en/stable/usage/tar.html
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
@@ -287,7 +281,7 @@ class RestoreFromTmpView(SuccessMessageMixin, FormView):
|
||||
included_apps = self._get_included_apps()
|
||||
installed_apps = api.get_all_apps_for_backup()
|
||||
kwargs['apps'] = [
|
||||
app for app in installed_apps if app[0] in included_apps
|
||||
app for app in installed_apps if app.name in included_apps
|
||||
]
|
||||
return kwargs
|
||||
|
||||
@@ -301,3 +295,41 @@ class RestoreFromTmpView(SuccessMessageMixin, FormView):
|
||||
"""Restore files from the archive on valid form submission."""
|
||||
backups.restore_from_tmp(form.cleaned_data['selected_apps'])
|
||||
return super().form_valid(form)
|
||||
|
||||
|
||||
class RestoreArchiveView(SuccessMessageMixin, FormView):
|
||||
"""View to restore files from an archive."""
|
||||
form_class = forms.RestoreForm
|
||||
prefix = 'backups'
|
||||
template_name = 'backups_restore.html'
|
||||
success_url = reverse_lazy('backups:index')
|
||||
success_message = _('Restored files from backup.')
|
||||
|
||||
def _get_included_apps(self):
|
||||
"""Save some data used to instantiate the form."""
|
||||
name = unquote(self.kwargs['name'])
|
||||
archive_path = backups.get_archive_path(name)
|
||||
return backups.get_archive_apps(archive_path)
|
||||
|
||||
def get_form_kwargs(self):
|
||||
"""Pass additional keyword args for instantiating the form."""
|
||||
kwargs = super().get_form_kwargs()
|
||||
included_apps = self._get_included_apps()
|
||||
installed_apps = api.get_all_apps_for_backup()
|
||||
kwargs['apps'] = [
|
||||
app for app in installed_apps if app.name in included_apps
|
||||
]
|
||||
return kwargs
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
"""Return additional context for rendering the template."""
|
||||
context = super().get_context_data(**kwargs)
|
||||
context['title'] = _('Restore from backup')
|
||||
context['name'] = self.kwargs['name']
|
||||
return context
|
||||
|
||||
def form_valid(self, form):
|
||||
"""Restore files from the archive on valid form submission."""
|
||||
archive_path = backups.get_archive_path(self.kwargs['name'])
|
||||
backups.restore(archive_path, form.cleaned_data['selected_apps'])
|
||||
return super().form_valid(form)
|
||||
Reference in new issue
Block a user