From 28268c58ebd3b22ec019af95c0c2fbe6a0b2f163 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Mon, 5 Apr 2021 20:45:28 +0200 Subject: [PATCH 1/3] Add a --dry-run option to backup_create that returns the size of items that will be backed up --- data/actionsmap/yunohost.yml | 3 +++ locales/en.json | 1 + src/yunohost/backup.py | 9 ++++++++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/data/actionsmap/yunohost.yml b/data/actionsmap/yunohost.yml index 290952aa3..fad9b57d3 100644 --- a/data/actionsmap/yunohost.yml +++ b/data/actionsmap/yunohost.yml @@ -890,6 +890,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: diff --git a/locales/en.json b/locales/en.json index 0058880c3..c9c440d5c 100644 --- a/locales/en.json +++ b/locales/en.json @@ -93,6 +93,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", diff --git a/src/yunohost/backup.py b/src/yunohost/backup.py index 50bd617b7..4987d5871 100644 --- a/src/yunohost/backup.py +++ b/src/yunohost/backup.py @@ -2138,7 +2138,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 @@ -2220,8 +2220,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")) From 93166741ee476850fe72ccc179bf7b21ae5954d0 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Mon, 5 Apr 2021 20:45:51 +0200 Subject: [PATCH 2/3] Simplify indentation --- src/yunohost/backup.py | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/yunohost/backup.py b/src/yunohost/backup.py index 4987d5871..247b99325 100644 --- a/src/yunohost/backup.py +++ b/src/yunohost/backup.py @@ -793,25 +793,28 @@ class BackupManager: self.size_details["apps"][app_key] = 0 for row in self.paths_to_backup: - if row["dest"] != "info.json": - size = disk_usage(row["source"]) + if row["dest"] == "info.json": + continue - # Add size to apps details - splitted_dest = row["dest"].split("/") - category = splitted_dest[0] - if category == "apps": - for app_key in self.apps_return: - 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: - if row["dest"].startswith(system_key.replace("_", "/")): - self.size_details["system"][system_key] += size - break + size = disk_usage(row["source"]) - self.size += size + # Add size to apps details + splitted_dest = row["dest"].split("/") + category = splitted_dest[0] + if category == "apps": + for app_key in self.apps_return: + 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: + if row["dest"].startswith(system_key.replace("_", "/")): + self.size_details["system"][system_key] += size + break + + self.size += size return self.size From e5b4d2aa733648ce936ef06137305eedb2838d1e Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Mon, 5 Apr 2021 20:46:33 +0200 Subject: [PATCH 3/3] Trick to add all the apps/ folder such that they are correctly attributed to the corresponding app when we compute the size_details later --- src/yunohost/backup.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/yunohost/backup.py b/src/yunohost/backup.py index 247b99325..9a95cd55a 100644 --- a/src/yunohost/backup.py +++ b/src/yunohost/backup.py @@ -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")):