Merge pull request #1205 from YunoHost/backup-create-dry-run

Add a --dry-run option to backup_create to fetch an estimate of the backup size
This commit is contained in:
Alexandre Aubin 2021-04-17 01:56:20 +02:00 committed by GitHub
commit 3f892c7f02
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 20 deletions

View file

@ -876,6 +876,9 @@ backup:
--apps:
help: List of application names to backup (or all if none given)
nargs: "*"
--dry-run:
help: "'Simulate' the backup and return the size details per item to backup"
action: store_true
### backup_restore()
restore:

View file

@ -95,6 +95,7 @@
"backup_copying_to_organize_the_archive": "Copying {size:s}MB to organize the archive",
"backup_couldnt_bind": "Could not bind {src:s} to {dest:s}.",
"backup_created": "Backup created",
"backup_create_size_estimation": "The archive will contain about {size} of data.",
"backup_creation_failed": "Could not create the backup archive",
"backup_csv_addition_failed": "Could not add files to backup into the CSV file",
"backup_csv_creation_failed": "Could not create the CSV file needed for restoration",

View file

@ -538,8 +538,8 @@ class BackupManager:
# Add unlisted files from backup tmp dir
self._add_to_list_to_backup("backup.csv")
self._add_to_list_to_backup("info.json")
if len(self.apps_return) > 0:
self._add_to_list_to_backup("apps")
for app in self.apps_return.keys():
self._add_to_list_to_backup(f"apps/{app}")
if os.path.isdir(os.path.join(self.work_dir, "conf")):
self._add_to_list_to_backup("conf")
if os.path.isdir(os.path.join(self.work_dir, "data")):
@ -792,7 +792,9 @@ class BackupManager:
self.size_details["apps"][app_key] = 0
for row in self.paths_to_backup:
if row["dest"] != "info.json":
if row["dest"] == "info.json":
continue
size = disk_usage(row["source"])
# Add size to apps details
@ -803,6 +805,7 @@ class BackupManager:
if row["dest"].startswith("apps/" + app_key):
self.size_details["apps"][app_key] += size
break
# OR Add size to the correct system element
elif category == "data" or category == "conf":
for system_key in self.system_return:
@ -2166,7 +2169,7 @@ class CustomBackupMethod(BackupMethod):
@is_unit_operation()
def backup_create(
operation_logger,
name=None, description=None, methods=[], output_directory=None, system=[], apps=[]
name=None, description=None, methods=[], output_directory=None, system=[], apps=[], dry_run=False
):
"""
Create a backup local archive
@ -2248,8 +2251,15 @@ def backup_create(
# Collect files to be backup (by calling app backup script / system hooks)
backup_manager.collect_files()
if dry_run:
return {
"size": backup_manager.size,
"size_details": backup_manager.size_details
}
# Apply backup methods on prepared files
logger.info(m18n.n("backup_actually_backuping"))
logger.info(m18n.n("backup_create_size_estimation", size=binary_to_human(backup_manager.size) + "B"))
backup_manager.backup()
logger.success(m18n.n("backup_created"))