✨ backup: allow plugins to flag excludes among their data

Plugins may now implement the hook octoprint.plugin.backup.additional_excludes
to flag entries inside their data folder (or the whole data folder
in case of .) as to be excluded from backups. The list of currently
excluded folders is supplied to the hook handler as well in order
to make such a decision conditionally.

Implements #3720
This commit is contained in:
Gina Häußge committed 2020-09-10 18:04:38 +02:00
1 parent 0dd43d7bcc
commit 65eb45c56a
3 files changed
+114 -2

No files matched your search

+84
View File
@@ -57,6 +57,90 @@ It adds two new commands, ``backup:backup`` and ``backup:restore``.
The ``backup:backup`` command can be useful in combination with a cronjob to create backups in regular intervals.
.. _sec-bundledplugins-backup-hooks:
Hooks
-----
.. _sec-bundledplugins-backup-hooks-excludes:
octoprint.plugin.backup.additional_excludes
+++++++++++++++++++++++++++++++++++++++++++
.. py:function:: additional_excludes_hook(excludes, *args, **kwargs)
Use this to provide additional paths on your plugin's data folder to exclude from the backup. Your handler also
get a list of currently excluded sub paths in the base folder, so you can react to them. E.g. exclude things
in your data folder that relate to uploaded GCODE files if `uploads` is excluded, or exclude things that relate
to timelapses if `timelapse` is excluded.
Expects a list of additional paths relative to your plugin's data folder. If you return a single `.`, your whole
plugin's data folder will be excluded from the backup.
**Example 1**
The following example plugin will create two files ``foo.txt`` and ``bar.txt`` in its data folder, but flag
``foo.txt`` as not to be backed up.
.. code-block:: python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
import octoprint.plugin
import os
import io
class BackupExcludeTestPlugin(octoprint.plugin.OctoPrintPlugin):
def initialize(self):
with io.open(os.path.join(self.get_plugin_data_folder(), "foo.txt"), "w", encoding="utf-8") as f:
f.write("Hello\n")
with io.open(os.path.join(self.get_plugin_data_folder(), "bar.txt"), "w", encoding="utf-8") as f:
f.write("Hello\n")
def additional_excludes_hook(self, excludes, *args, **kwargs):
return ["foo.txt"]
__plugin_implementation__ = BackupExcludeTestPlugin()
__plugin_hooks__ = {
"octoprint.plugin.backup.additional_excludes": __plugin_implementation__.additional_excludes_hook
}
**Example 2**
In this example the plugin will create a file ``foo.txt`` in its data folder and flag its whole data folder as excluded from the
backup if uploaded GCODE files are also excluded:
.. code-block:: python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
import octoprint.plugin
import os
import io
class BackupExcludeTestPlugin(octoprint.plugin.OctoPrintPlugin):
def initialize(self):
with io.open(os.path.join(self.get_plugin_data_folder(), "foo.txt"), "w", encoding="utf-8") as f:
f.write("Hello\n")
def additional_excludes_hook(self, excludes, *args, **kwargs):
if "uploads" in excludes:
return ["."]
return []
__plugin_implementation__ = BackupExcludeTestPlugin()
__plugin_hooks__ = {
"octoprint.plugin.backup.additional_excludes": __plugin_implementation__.additional_excludes_hook
}
:param excludes list: A list of paths already flagged as excluded in the backup
:return: A list of paths to exclude, relative to your plugin's data folder
:rtype: list
.. _sec-bundledplugins-backup-sourcecode:
Source code
+7
View File
@@ -1125,6 +1125,13 @@ octoprint.filemanager.preprocessor
:return: The `file_object` as passed in or None, or a replaced version to use instead for further processing.
:rtype: AbstractFileWrapper or None
.. _sec-plugins-hook-plugin-backup-excludes:
octoprint.plugin.backup.additional_excludes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
See :ref:`here <sec-bundledplugins-backup-hooks-excludes>`.
.. _sec-plugins-hook-plugin-loginui-theming:
octoprint.plugin.loginui.theming
+23 -2
View File
@@ -183,6 +183,7 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin,
kwargs=dict(exclude=exclude,
settings=self._settings,
plugin_manager=self._plugin_manager,
logger=self._logger,
datafolder=self.get_plugin_data_folder(),
on_backup_start=on_backup_start,
on_backup_done=on_backup_done,
@@ -388,6 +389,7 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin,
self._create_backup(backup_file,
exclude=exclude,
settings=settings,
logger=self._logger,
plugin_manager=cli_group.plugin_manager,
datafolder=datafolder)
click.echo("Done.")
@@ -645,6 +647,7 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin,
exclude=None,
settings=None,
plugin_manager=None,
logger=None,
datafolder=None,
on_backup_start=None,
on_backup_done=None,
@@ -660,6 +663,24 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin,
if "timelapse" in exclude:
exclude.append("timelapse_tmp")
current_excludes = list(exclude)
additional_excludes = list()
plugin_data = settings.global_get_basefolder("data")
for plugin, hook in plugin_manager.get_hooks("octoprint.plugin.backup.additional_excludes").items():
try:
additional = hook(current_excludes)
if isinstance(additional, list):
if "." in additional:
current_excludes.append(os.path.join("data", plugin))
additional_excludes.append(os.path.join(plugin_data, plugin))
else:
current_excludes += map(lambda x: os.path.join("data", plugin, x), additional)
additional_excludes += map(lambda x: os.path.join(plugin_data, plugin, x), additional)
except Exception as ex:
logger.exception("Error while retrieving additional excludes "
"from plugin {name}".format(**locals()),
extra=dict(plugin=plugin))
configfile = settings._configfile
basedir = settings._basedir
@@ -717,10 +738,10 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin,
add_to_zip(settings.global_get_basefolder(folder),
"basedir/" + folder.replace("_", "/"),
ignored=[own_folder,])
ignored=[own_folder,] + additional_excludes)
# backup anything else that might be lying around in our basedir
add_to_zip(basedir, "basedir", ignored=defaults + [own_folder, ])
add_to_zip(basedir, "basedir", ignored=defaults + [own_folder, ] + additional_excludes)
# add list of installed plugins
plugins = []