bash option

This commit is contained in:
Spitap
2022-07-21 18:00:50 +02:00
parent 74de6a9bb1
commit 5318fa279b
3 changed files with 25 additions and 13 deletions

View File

@@ -24,7 +24,7 @@ def install(appname, config, upgrade):
utils.printcolor(u"{}".format(inst), utils.RED) utils.printcolor(u"{}".format(inst), utils.RED)
sys.exit(1) sys.exit(1)
def backup(config): def backup(config, isBash):
"""Backup instance""" """Backup instance"""
try: try:
script = importlib.import_module( script = importlib.import_module(
@@ -32,7 +32,7 @@ def backup(config):
except ImportError: except ImportError:
print("Error importing backup") print("Error importing backup")
try: try:
getattr(script, "Backup")(config).run() getattr(script, "Backup")(config, isBash).run()
except utils.FatalError as inst: except utils.FatalError as inst:
utils.printcolor(u"{}".format(inst), utils.RED) utils.printcolor(u"{}".format(inst), utils.RED)
sys.exit(1) sys.exit(1)

View File

@@ -4,6 +4,7 @@ import os
import pwd import pwd
import shutil import shutil
import stat import stat
import datetime
from .. import database from .. import database
from .. import utils from .. import utils
@@ -26,8 +27,9 @@ class Backup():
#||--> mails #||--> mails
# |--> vmails # |--> vmails
def __init__(self, config): def __init__(self, config, isBash):
self.config = config self.config = config
self.isBash = isBash
self.destinationPath = "" self.destinationPath = ""
self.BACKUPDIRECTORY = ["mails/", "custom/", "databases/"] self.BACKUPDIRECTORY = ["mails/", "custom/", "databases/"]
@@ -49,17 +51,19 @@ class Backup():
return False return False
if not os.path.exists(path): if not os.path.exists(path):
if not self.isBash:
createDir = input(f"\"{path}\" doesn't exists, would you like to create it ? [Y/n]\n").lower() createDir = input(f"\"{path}\" doesn't exists, would you like to create it ? [Y/n]\n").lower()
if createDir == "y" or createDir == "yes": if self.isBash or (not self.isBash and (createDir == "y" or createDir == "yes")):
pw = pwd.getpwnam("root") pw = pwd.getpwnam("root")
utils.mkdir_safe(path, stat.S_IRWXU | stat.S_IRWXG, pw[2], pw[3]) utils.mkdir_safe(path, stat.S_IRWXU | stat.S_IRWXG, pw[2], pw[3])
else: else:
return False return False
if len(os.listdir(path)) != 0: if len(os.listdir(path)) != 0:
delDir = input("Warning : backup folder is not empty, it will be purged if you continue... [Y/n]").lower() if not self.isBash:
if delDir == "y" or delDir == "yes": delDir = input("Warning : backup folder is not empty, it will be purged if you continue... [Y/n]\n").lower()
if self.isBash or (not self.isBash and (delDir == "y" or delDir == "yes")):
shutil.rmtree(path) shutil.rmtree(path)
else: else:
return False return False
@@ -75,6 +79,11 @@ class Backup():
def setPath(self): def setPath(self):
"""Setup backup directory""" """Setup backup directory"""
if self.isBash:
date = datetime.datetime.now().strftime("%m_%d_%Y_%H_%M")
path = f"/modoboa_backup/backup_{date}/"
self.validatePath(path)
else:
user_value = None user_value = None
while (user_value == '' or user_value == None or not self.validatePath(user_value)): while (user_value == '' or user_value == None or not self.validatePath(user_value)):
print("Enter backup path, please provide an empty folder.") print("Enter backup path, please provide an empty folder.")

5
run.py
View File

@@ -80,6 +80,9 @@ def main(input_args):
parser.add_argument( parser.add_argument(
"--beta", action="store_true", default=False, "--beta", action="store_true", default=False,
help="Install latest beta release of Modoboa instead of the stable one") help="Install latest beta release of Modoboa instead of the stable one")
parser.add_argument(
"--bash", action="store_true", default=False,
help="(backup only) - For script usage, No interaction will be required")
parser.add_argument("domain", type=str, parser.add_argument("domain", type=str,
help="The main domain of your future mail server") help="The main domain of your future mail server")
args = parser.parse_args(input_args) args = parser.parse_args(input_args)
@@ -104,7 +107,7 @@ def main(input_args):
upgrade_disclaimer(config) upgrade_disclaimer(config)
elif args.backup: elif args.backup:
backup_disclamer() backup_disclamer()
scripts.backup(config) scripts.backup(config, args.bash)
return return
else: else:
installation_disclaimer(args, config) installation_disclaimer(args, config)