From 6726f5b1a232d30438b6a5ea175141523d1eb6c4 Mon Sep 17 00:00:00 2001 From: Spitap Date: Mon, 26 Sep 2022 13:39:28 +0200 Subject: [PATCH] Improved path generation, path mistake proofing --- README.rst | 2 +- modoboa_installer/scripts/backup.py | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 8038ba7..e2af8c7 100644 --- a/README.rst +++ b/README.rst @@ -116,7 +116,7 @@ Command:: $ sudo ./run.py --silent-backup -This mode is the silent batch mode, when executed, it will create /modoboa_backup/ and each time you execute it, it will create a new backup directory with current time. +This mode is the silent batch mode, when executed, it will create /modoboa_backup/ and each time you execute it, it will create a new backup directory with current date and time. You can supply a custom path. diff --git a/modoboa_installer/scripts/backup.py b/modoboa_installer/scripts/backup.py index 54d9f5a..9c7e53a 100644 --- a/modoboa_installer/scripts/backup.py +++ b/modoboa_installer/scripts/backup.py @@ -5,6 +5,7 @@ import pwd import shutil import stat import sys +import datetime from .. import database from .. import utils @@ -53,15 +54,29 @@ class Backup: utils.mkdir_safe(path, stat.S_IRWXU | stat.S_IRWXG, pw[2], pw[3]) else: + utils.printcolor( + "Error, backup dir not present.", utils.RED + ) return False if len(os.listdir(path)) != 0: if not self.silent_backup: delete_dir = input( "Warning : backup folder is not empty, it will be purged if you continue... [Y/n]\n").lower() + if self.silent_backup or (not self.silent_backup and (delete_dir == "y" or delete_dir == "yes")): - shutil.rmtree(path) + try: + os.remove(os.path.join(path, "installer.cfg")) + except FileNotFoundError: + pass + + shutil.rmtree(os.path.join(path, "custom"),ignore_errors=False) + shutil.rmtree(os.path.join(path, "mails"), ignore_errors=False) + shutil.rmtree(os.path.join(path, "databases"), ignore_errors=False) else: + utils.printcolor( + "Error, backup dir not clean.", utils.RED + ) return False self.backup_path = path @@ -70,7 +85,6 @@ class Backup: for dir in ["custom/", "databases/"]: utils.mkdir_safe(os.path.join(self.backup_path, dir), stat.S_IRWXU | stat.S_IRWXG, pw[2], pw[3]) - return True def set_path(self): @@ -81,6 +95,8 @@ class Backup: path = self.config.get("backup", "default_path") else: path = f"./modoboa_backup/" + date = datetime.datetime.now().strftime("%m_%d_%Y_%H_%M") + path = os.path.join(path, f"backup_{date}") self.validate_path(path) else: if not self.validate_path(self.backup_path):