Improved path generation, path mistake proofing

This commit is contained in:
Spitap
2022-09-26 13:39:28 +02:00
parent a192cbcbd0
commit 6726f5b1a2
2 changed files with 19 additions and 3 deletions

View File

@@ -116,7 +116,7 @@ Command::
$ sudo ./run.py --silent-backup <your domain> $ sudo ./run.py --silent-backup <your domain>
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. You can supply a custom path.

View File

@@ -5,6 +5,7 @@ import pwd
import shutil import shutil
import stat import stat
import sys import sys
import datetime
from .. import database from .. import database
from .. import utils from .. import utils
@@ -53,15 +54,29 @@ class Backup:
utils.mkdir_safe(path, stat.S_IRWXU | utils.mkdir_safe(path, stat.S_IRWXU |
stat.S_IRWXG, pw[2], pw[3]) stat.S_IRWXG, pw[2], pw[3])
else: else:
utils.printcolor(
"Error, backup dir not present.", utils.RED
)
return False return False
if len(os.listdir(path)) != 0: if len(os.listdir(path)) != 0:
if not self.silent_backup: if not self.silent_backup:
delete_dir = input( delete_dir = input(
"Warning : backup folder is not empty, it will be purged if you continue... [Y/n]\n").lower() "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")): 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: else:
utils.printcolor(
"Error, backup dir not clean.", utils.RED
)
return False return False
self.backup_path = path self.backup_path = path
@@ -70,7 +85,6 @@ class Backup:
for dir in ["custom/", "databases/"]: for dir in ["custom/", "databases/"]:
utils.mkdir_safe(os.path.join(self.backup_path, dir), utils.mkdir_safe(os.path.join(self.backup_path, dir),
stat.S_IRWXU | stat.S_IRWXG, pw[2], pw[3]) stat.S_IRWXU | stat.S_IRWXG, pw[2], pw[3])
return True return True
def set_path(self): def set_path(self):
@@ -81,6 +95,8 @@ class Backup:
path = self.config.get("backup", "default_path") path = self.config.get("backup", "default_path")
else: else:
path = f"./modoboa_backup/" 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) self.validate_path(path)
else: else:
if not self.validate_path(self.backup_path): if not self.validate_path(self.backup_path):