mirror of
https://github.com/YunoHost/yunohost.git
synced 2024-09-03 20:06:10 +02:00
[fix] Backup without info.json (#342)
* [fix] Backup without info.json * Add test of archive restore with no info.json * Fix exception handling in backup_delete when info.json is missng
This commit is contained in:
parent
02ea0c0656
commit
3ede5fc39d
2 changed files with 37 additions and 10 deletions
|
@ -2193,7 +2193,11 @@ def backup_list(with_info=False, human_readable=False):
|
||||||
if result and with_info:
|
if result and with_info:
|
||||||
d = OrderedDict()
|
d = OrderedDict()
|
||||||
for a in result:
|
for a in result:
|
||||||
|
try:
|
||||||
d[a] = backup_info(a, human_readable=human_readable)
|
d[a] = backup_info(a, human_readable=human_readable)
|
||||||
|
except MoulinetteError, e:
|
||||||
|
logger.warning('%s: %s' % (a, e.strerror))
|
||||||
|
|
||||||
result = d
|
result = d
|
||||||
|
|
||||||
return {'archives': result}
|
return {'archives': result}
|
||||||
|
@ -2231,9 +2235,16 @@ def backup_info(name, with_details=False, human_readable=False):
|
||||||
if not os.path.exists(info_file):
|
if not os.path.exists(info_file):
|
||||||
tar = tarfile.open(archive_file, "r:gz")
|
tar = tarfile.open(archive_file, "r:gz")
|
||||||
info_dir = info_file + '.d'
|
info_dir = info_file + '.d'
|
||||||
|
try:
|
||||||
tar.extract('info.json', path=info_dir)
|
tar.extract('info.json', path=info_dir)
|
||||||
tar.close()
|
except KeyError:
|
||||||
|
logger.debug("unable to retrieve '%s' inside the archive",
|
||||||
|
info_file, exc_info=1)
|
||||||
|
raise MoulinetteError(errno.EIO, m18n.n('backup_invalid_archive'))
|
||||||
|
else:
|
||||||
shutil.move(os.path.join(info_dir, 'info.json'), info_file)
|
shutil.move(os.path.join(info_dir, 'info.json'), info_file)
|
||||||
|
finally:
|
||||||
|
tar.close()
|
||||||
os.rmdir(info_dir)
|
os.rmdir(info_dir)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -2281,21 +2292,21 @@ def backup_delete(name):
|
||||||
name -- Name of the local backup archive
|
name -- Name of the local backup archive
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
if name not in backup_list()["archives"]:
|
||||||
|
raise MoulinetteError(errno.EIO, m18n.n('backup_archive_name_unknown',
|
||||||
|
name=name))
|
||||||
|
|
||||||
hook_callback('pre_backup_delete', args=[name])
|
hook_callback('pre_backup_delete', args=[name])
|
||||||
|
|
||||||
archive_file = '%s/%s.tar.gz' % (ARCHIVES_PATH, name)
|
archive_file = '%s/%s.tar.gz' % (ARCHIVES_PATH, name)
|
||||||
|
|
||||||
info_file = "%s/%s.info.json" % (ARCHIVES_PATH, name)
|
info_file = "%s/%s.info.json" % (ARCHIVES_PATH, name)
|
||||||
|
|
||||||
for backup_file in [archive_file, info_file]:
|
for backup_file in [archive_file, info_file]:
|
||||||
if not os.path.isfile(backup_file) and not os.path.islink(backup_file):
|
|
||||||
raise MoulinetteError(errno.EIO,
|
|
||||||
m18n.n('backup_archive_name_unknown', name=backup_file))
|
|
||||||
try:
|
try:
|
||||||
os.remove(backup_file)
|
os.remove(backup_file)
|
||||||
except:
|
except:
|
||||||
logger.debug("unable to delete '%s'", backup_file, exc_info=1)
|
logger.debug("unable to delete '%s'", backup_file, exc_info=1)
|
||||||
raise MoulinetteError(errno.EIO,
|
logger.warning(m18n.n('backup_delete_error', path=backup_file))
|
||||||
m18n.n('backup_delete_error', path=backup_file))
|
|
||||||
|
|
||||||
hook_callback('post_backup_delete', args=[name])
|
hook_callback('post_backup_delete', args=[name])
|
||||||
|
|
||||||
|
|
|
@ -634,5 +634,21 @@ def _test_backup_and_restore_app(app):
|
||||||
|
|
||||||
assert app_is_installed(app)
|
assert app_is_installed(app)
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
# Some edge cases #
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
def test_restore_archive_with_no_json(mocker):
|
||||||
|
|
||||||
|
# Create a backup with no info.json associated
|
||||||
|
os.system("touch /tmp/afile")
|
||||||
|
os.system("tar -czvf /home/yunohost.backup/archives/badbackup.tar.gz /tmp/afile")
|
||||||
|
|
||||||
|
assert "badbackup" in backup_list()["archives"]
|
||||||
|
|
||||||
|
mocker.spy(m18n, "n")
|
||||||
|
with pytest.raises(MoulinetteError):
|
||||||
|
backup_restore(auth, name="badbackup", force=True,
|
||||||
|
ignore_system=False, ignore_apps=False)
|
||||||
|
m18n.n.assert_any_call('backup_invalid_archive')
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue