Merge pull request #198 from YunoHost/fix-618-custom-backup-directory

[fix] Correctly handle custom backup directory
This commit is contained in:
Laurent Peuch 2016-12-16 00:31:55 +01:00 committed by GitHub
commit 198bc51128
2 changed files with 31 additions and 4 deletions

View file

@ -43,6 +43,7 @@
"backup_action_required": "You must specify something to save", "backup_action_required": "You must specify something to save",
"backup_app_failed": "Unable to back up the app '{app:s}'", "backup_app_failed": "Unable to back up the app '{app:s}'",
"backup_archive_app_not_found": "App '{app:s}' not found in the backup archive", "backup_archive_app_not_found": "App '{app:s}' not found in the backup archive",
"backup_archive_broken_link": "Unable to access backup archive (broken link to {path:s})",
"backup_archive_hook_not_exec": "Hook '{hook:s}' not executed in this backup", "backup_archive_hook_not_exec": "Hook '{hook:s}' not executed in this backup",
"backup_archive_name_exists": "The backup's archive name already exists", "backup_archive_name_exists": "The backup's archive name already exists",
"backup_archive_name_unknown": "Unknown local backup archive named '{name:s}'", "backup_archive_name_unknown": "Unknown local backup archive named '{name:s}'",

View file

@ -119,6 +119,8 @@ def backup_create(name=None, description=None, output_directory=None,
env_var['CAN_BIND'] = 0 env_var['CAN_BIND'] = 0
else: else:
output_directory = archives_path output_directory = archives_path
# Create archives directory if it does not exists
if not os.path.isdir(archives_path): if not os.path.isdir(archives_path):
os.mkdir(archives_path, 0750) os.mkdir(archives_path, 0750)
@ -287,7 +289,7 @@ def backup_create(name=None, description=None, output_directory=None,
raise MoulinetteError(errno.EIO, raise MoulinetteError(errno.EIO,
m18n.n('backup_archive_open_failed')) m18n.n('backup_archive_open_failed'))
# Add files to the arvhice # Add files to the archive
try: try:
tar.add(tmp_dir, arcname='') tar.add(tmp_dir, arcname='')
tar.close() tar.close()
@ -297,10 +299,22 @@ def backup_create(name=None, description=None, output_directory=None,
raise MoulinetteError(errno.EIO, raise MoulinetteError(errno.EIO,
m18n.n('backup_creation_failed')) m18n.n('backup_creation_failed'))
# FIXME : it looks weird that the "move info file" is not enabled if
# user activated "no_compress" ... or does it really means
# "dont_keep_track_of_this_backup_in_history" ?
# Move info file # Move info file
shutil.move(tmp_dir + '/info.json', shutil.move(tmp_dir + '/info.json',
'{:s}/{:s}.info.json'.format(archives_path, name)) '{:s}/{:s}.info.json'.format(archives_path, name))
# If backuped to a non-default location, keep a symlink of the archive
# to that location
if output_directory != archives_path:
link = "%s/%s.tar.gz" % (archives_path, name)
os.symlink(archive_file, link)
# Clean temporary directory # Clean temporary directory
if tmp_dir != output_directory: if tmp_dir != output_directory:
_clean_tmp_dir() _clean_tmp_dir()
@ -601,11 +615,23 @@ def backup_info(name, with_details=False, human_readable=False):
""" """
archive_file = '%s/%s.tar.gz' % (archives_path, name) archive_file = '%s/%s.tar.gz' % (archives_path, name)
if not os.path.isfile(archive_file):
# Check file exist (even if it's a broken symlink)
if not os.path.lexists(archive_file):
raise MoulinetteError(errno.EIO, raise MoulinetteError(errno.EIO,
m18n.n('backup_archive_name_unknown', name=name)) m18n.n('backup_archive_name_unknown', name=name))
# If symlink, retrieve the real path
if os.path.islink(archive_file):
archive_file = os.path.realpath(archive_file)
# Raise exception if link is broken (e.g. on unmounted external storage)
if not os.path.exists(archive_file):
raise MoulinetteError(errno.EIO,
m18n.n('backup_archive_broken_link', path=archive_file))
info_file = "%s/%s.info.json" % (archives_path, name) info_file = "%s/%s.info.json" % (archives_path, name)
try: try:
with open(info_file) as f: with open(info_file) as f:
# Retrieve backup info # Retrieve backup info