[enh] Allow to filter which app to backup/restore

This commit is contained in:
Jérôme Lebleu 2015-09-30 21:51:28 +02:00
parent 94a2b3ccf2
commit 85626134ef
2 changed files with 39 additions and 4 deletions

View file

@ -590,6 +590,9 @@ backup:
--hooks: --hooks:
help: List of backup hooks names to execute help: List of backup hooks names to execute
nargs: "*" nargs: "*"
--apps:
help: List of application names to backup
nargs: "*"
--ignore-apps: --ignore-apps:
help: Do not backup apps help: Do not backup apps
action: store_true action: store_true
@ -606,6 +609,9 @@ backup:
--hooks: --hooks:
help: List of restauration hooks names to execute help: List of restauration hooks names to execute
nargs: "*" nargs: "*"
--apps:
help: List of application names to restore
nargs: "*"
--ignore-apps: --ignore-apps:
help: Do not restore apps help: Do not restore apps
action: store_true action: store_true

View file

@ -42,7 +42,7 @@ logger = getActionLogger('yunohost.backup')
def backup_create(name=None, description=None, output_directory=None, def backup_create(name=None, description=None, output_directory=None,
no_compress=False, hooks=[], ignore_apps=False): no_compress=False, hooks=[], apps=[], ignore_apps=False):
""" """
Create a backup local archive Create a backup local archive
@ -52,6 +52,7 @@ def backup_create(name=None, description=None, output_directory=None,
output_directory -- Output directory for the backup output_directory -- Output directory for the backup
no_compress -- Do not create an archive file no_compress -- Do not create an archive file
hooks -- List of backup hooks names to execute hooks -- List of backup hooks names to execute
apps -- List of application names to backup
ignore_apps -- Do not backup apps ignore_apps -- Do not backup apps
""" """
@ -126,8 +127,22 @@ def backup_create(name=None, description=None, output_directory=None,
# Add apps backup hook # Add apps backup hook
if not ignore_apps: if not ignore_apps:
from yunohost.app import app_info from yunohost.app import app_info
# Filter applications to backup
apps_list = set(os.listdir('/etc/yunohost/apps'))
apps_filtered = set()
if apps:
for a in apps:
if a not in apps_list:
logger.warning("app '%s' not found", a)
msignals.display(m18n.n('unbackup_app', a), 'warning')
else:
apps_filtered.add(a)
else:
apps_filtered = apps_list
try: try:
for app_id in os.listdir('/etc/yunohost/apps'): for app_id in apps_filtered:
hook = '/etc/yunohost/apps/%s/scripts/backup' % app_id hook = '/etc/yunohost/apps/%s/scripts/backup' % app_id
if os.path.isfile(hook): if os.path.isfile(hook):
hook_add(app_id, hook) hook_add(app_id, hook)
@ -192,13 +207,14 @@ def backup_create(name=None, description=None, output_directory=None,
msignals.display(m18n.n('backup_complete'), 'success') msignals.display(m18n.n('backup_complete'), 'success')
def backup_restore(name, hooks=[], ignore_apps=False, force=False): def backup_restore(name, hooks=[], apps=[], ignore_apps=False, force=False):
""" """
Restore from a local backup archive Restore from a local backup archive
Keyword argument: Keyword argument:
name -- Name of the local backup archive name -- Name of the local backup archive
hooks -- List of restoration hooks names to execute hooks -- List of restoration hooks names to execute
apps -- List of application names to restore
ignore_apps -- Do not restore apps ignore_apps -- Do not restore apps
force -- Force restauration on an already installed system force -- Force restauration on an already installed system
@ -270,7 +286,20 @@ def backup_restore(name, hooks=[], ignore_apps=False, force=False):
# Add apps restore hook # Add apps restore hook
if not ignore_apps: if not ignore_apps:
for app_id in info['apps'].keys(): # Filter applications to restore
apps_list = set(info['apps'].keys())
apps_filtered = set()
if apps:
for a in apps:
if a not in apps_list:
logger.warning("app '%s' not found", a)
msignals.display(m18n.n('unrestore_app', a), 'warning')
else:
apps_filtered.add(a)
else:
apps_filtered = apps_list
for app_id in apps_filtered:
hook = "/etc/yunohost/apps/%s/scripts/restore" % app_id hook = "/etc/yunohost/apps/%s/scripts/restore" % app_id
if os.path.isfile(hook): if os.path.isfile(hook):
hook_add(app_id, hook) hook_add(app_id, hook)