refactoring
This commit is contained in:
@@ -439,4 +439,13 @@ ConfigDictTemplate = [
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "backup",
|
||||
"values": [
|
||||
{
|
||||
"option": "default_path",
|
||||
"default": "./modoboa_backup/"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
@@ -141,9 +141,10 @@ class PostgreSQL(Database):
|
||||
self.dbhost, dbname, dbuser, path)
|
||||
utils.exec_cmd(cmd, sudo_user=self.dbuser)
|
||||
|
||||
def dumpDatabase(self, dbname, dbuser, dbpassword, path):
|
||||
"""Dump DB to SQL file"""
|
||||
self._pgpass_done = False #Reset pgpass since we backup multiple db (different secret set)
|
||||
def dump_database(self, dbname, dbuser, dbpassword, path):
|
||||
"""Dump DB to SQL file."""
|
||||
# Reset pgpass since we backup multiple db (different secret set)
|
||||
self._pgpass_done = False
|
||||
self._setup_pgpass(dbname, dbuser, dbpassword)
|
||||
cmd = "pg_dump -h {} -d {} -U {} -O -w > {}".format(
|
||||
self.dbhost, dbname, dbuser, path)
|
||||
@@ -258,8 +259,9 @@ class MySQL(Database):
|
||||
"mysql -h {} -u {} -p{} {} < {}".format(
|
||||
self.dbhost, dbuser, dbpassword, dbname, path)
|
||||
)
|
||||
def dumpDatabase(self, dbname, dbuser, dbpassword, path):
|
||||
"""Dump DB to SQL file"""
|
||||
|
||||
def dump_database(self, dbname, dbuser, dbpassword, path):
|
||||
"""Dump DB to SQL file."""
|
||||
cmd = "mysqldump -h {} -u {} -p{} {} > {}".format(
|
||||
self.dbhost, dbuser, dbpassword, dbname, path)
|
||||
utils.exec_cmd(cmd, sudo_user=self.dbuser)
|
||||
|
||||
@@ -26,29 +26,20 @@ def install(appname, config, upgrade, restore):
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def backup(config, bashArg, nomail):
|
||||
def backup(config, silent_backup, backup_path, nomail):
|
||||
"""Backup instance"""
|
||||
script = importlib.import_module(
|
||||
"modoboa_installer.scripts.backup")
|
||||
try:
|
||||
script = importlib.import_module(
|
||||
"modoboa_installer.scripts.backup")
|
||||
except ImportError:
|
||||
print("Error importing backup")
|
||||
try:
|
||||
getattr(script, "Backup")(config, bashArg, nomail).run()
|
||||
getattr(script, "Backup")(
|
||||
config, silent_backup, backup_path, nomail).run()
|
||||
except utils.FatalError as inst:
|
||||
utils.printcolor(u"{}".format(inst), utils.RED)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def restore(restore):
|
||||
def restore_prep(restore):
|
||||
"""Restore instance"""
|
||||
try:
|
||||
script = importlib.import_module(
|
||||
"modoboa_installer.scripts.restore")
|
||||
except ImportError:
|
||||
print("Error importing restore")
|
||||
try:
|
||||
getattr(script, "Restore")(restore)
|
||||
except utils.FatalError as inst:
|
||||
utils.printcolor(u"{}".format(inst), utils.RED)
|
||||
sys.exit(1)
|
||||
script = importlib.import_module(
|
||||
"modoboa_installer.scripts.restore")
|
||||
getattr(script, "Restore")(restore)
|
||||
|
||||
@@ -78,12 +78,12 @@ class Amavis(base.Installer):
|
||||
if self.restore:
|
||||
utils.printcolor(
|
||||
"Trying to restore amavis database from backup", utils.MAGENTA)
|
||||
amavisDbBackupPath = os.path.join(
|
||||
amavis_db_backup_path = os.path.join(
|
||||
self.restore, "databases/amavis.sql")
|
||||
if os.path.isfile(amavisDbBackupPath):
|
||||
if os.path.isfile(amavis_db_backup_path):
|
||||
utils.printcolor(
|
||||
"Amavis database backup found ! Restoring...", utils.GREEN)
|
||||
return amavisDbBackupPath
|
||||
return amavis_db_backup_path
|
||||
utils.printcolor(
|
||||
"Amavis database backup not found, creating empty database", utils.RED)
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
"""Backup script for pre-installed instance"""
|
||||
"""Backup script for pre-installed instance."""
|
||||
|
||||
import os
|
||||
import pwd
|
||||
@@ -10,60 +10,52 @@ import sys
|
||||
from .. import database
|
||||
from .. import utils
|
||||
|
||||
# TODO: have version of each modoboa componenents saved into the config file to restore the same version
|
||||
|
||||
class Backup:
|
||||
"""
|
||||
Backup structure ( {optional} ):
|
||||
{{backup_folder}}
|
||||
||
|
||||
||--> installer.cfg
|
||||
||--> custom
|
||||
|--> { (copy of) /etc/amavis/conf.d/99-custom }
|
||||
|--> { (copy of) /etc/postfix/custom_whitelist.cidr }
|
||||
||--> databases
|
||||
|--> modoboa.sql
|
||||
|--> { amavis.sql }
|
||||
|--> { spamassassin.sql }
|
||||
||--> mails
|
||||
|--> vmails
|
||||
"""
|
||||
|
||||
class Backup():
|
||||
|
||||
# Backup structure ( {optional} ):
|
||||
# {{backup_folder}}
|
||||
# ||
|
||||
# ||--> installer.cfg
|
||||
# ||--> custom
|
||||
# |--> { (copy of) /etc/amavis/conf.d/99-custom }
|
||||
# |--> { (copy of) /etc/postfix/custom_whitelist.cidr }
|
||||
# ||--> databases
|
||||
# |--> modoboa.sql
|
||||
# |--> { amavis.sql }
|
||||
# |--> { spamassassin.sql }
|
||||
# ||--> mails
|
||||
# |--> vmails
|
||||
|
||||
def __init__(self, config, bashArg, nomail):
|
||||
def __init__(self, config, silent_backup, backup_path, nomail):
|
||||
self.config = config
|
||||
self.destinationPath = ""
|
||||
self.BACKUPDIRECTORY = ["custom/", "databases/"]
|
||||
self.destinationPath = backup_path
|
||||
self.nomail = nomail
|
||||
self.isBash = False
|
||||
self.bash = ""
|
||||
if bashArg != "NOBASH":
|
||||
self.isBash = True
|
||||
self.bash = bashArg
|
||||
self.silent_backup = silent_backup
|
||||
|
||||
def preparePath(self):
|
||||
pw = pwd.getpwnam("root")
|
||||
for dir in self.BACKUPDIRECTORY:
|
||||
for dir in ["custom/", "databases/"]:
|
||||
utils.mkdir_safe(os.path.join(self.destinationPath, dir),
|
||||
stat.S_IRWXU | stat.S_IRWXG, pw[2], pw[3])
|
||||
|
||||
def validatePath(self, path):
|
||||
"""Check basic condition for backup directory"""
|
||||
try:
|
||||
pathExists = os.path.exists(path)
|
||||
except:
|
||||
print("Provided path is not recognized...")
|
||||
"""Check basic condition for backup directory."""
|
||||
|
||||
path_exists = os.path.exists(path)
|
||||
|
||||
if path_exists and os.path.isfile(path):
|
||||
utils.printcolor(
|
||||
"Error, you provided a file instead of a directory!", utils.RED)
|
||||
return False
|
||||
|
||||
if pathExists and os.path.isfile(path):
|
||||
print("Error, you provided a file instead of a directory!")
|
||||
return False
|
||||
|
||||
if not pathExists:
|
||||
if not self.isBash:
|
||||
if not path_exists:
|
||||
if not self.silent_backup:
|
||||
createDir = input(
|
||||
f"\"{path}\" doesn't exists, would you like to create it ? [Y/n]\n").lower()
|
||||
|
||||
if self.isBash or (not self.isBash and (createDir == "y" or createDir == "yes")):
|
||||
if self.silent_backup or (not self.silent_backup and (createDir == "y" or createDir == "yes")):
|
||||
pw = pwd.getpwnam("root")
|
||||
utils.mkdir_safe(path, stat.S_IRWXU |
|
||||
stat.S_IRWXG, pw[2], pw[3])
|
||||
@@ -71,10 +63,10 @@ class Backup():
|
||||
return False
|
||||
|
||||
if len(os.listdir(path)) != 0:
|
||||
if not self.isBash:
|
||||
if not self.silent_backup:
|
||||
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")):
|
||||
if self.silent_backup or (not self.silent_backup and (delDir == "y" or delDir == "yes")):
|
||||
shutil.rmtree(path)
|
||||
else:
|
||||
return False
|
||||
@@ -84,31 +76,30 @@ class Backup():
|
||||
self.preparePath()
|
||||
return True
|
||||
|
||||
def setPath(self):
|
||||
"""Setup backup directory"""
|
||||
if self.isBash:
|
||||
if self.bash == "TRUE":
|
||||
def set_path(self):
|
||||
"""Setup backup directory."""
|
||||
if self.silent_backup:
|
||||
if self.destinationPath is None:
|
||||
date = datetime.datetime.now().strftime("%m_%d_%Y_%H_%M")
|
||||
path = f"/modoboa_backup/backup_{date}/"
|
||||
path = f"./modoboa_backup/backup_{date}/"
|
||||
self.validatePath(path)
|
||||
else:
|
||||
validate = self.validatePath(self.bash)
|
||||
if not validate:
|
||||
print("provided bash is not right, exiting...")
|
||||
print(f"Path provided : {self.bash}")
|
||||
if not self.validatePath(self.destinationPath):
|
||||
utils.printcolor(
|
||||
f"Path provided : {self.destinationPath}", utils.BLUE)
|
||||
sys.exit(1)
|
||||
else:
|
||||
user_value = None
|
||||
while (user_value == '' or user_value == None or not self.validatePath(user_value)):
|
||||
print("Enter backup path, please provide an empty folder.")
|
||||
print("CTRL+C to cancel")
|
||||
while user_value == "" or user_value is None or not self.validatePath(user_value):
|
||||
utils.printcolor(
|
||||
"Enter backup path, please provide an empty folder.", utils.MAGENTA)
|
||||
utils.printcolor("CTRL+C to cancel", utils.MAGENTA)
|
||||
user_value = utils.user_input("-> ")
|
||||
|
||||
def backupConfigFile(self):
|
||||
def config_file_backup(self):
|
||||
utils.copy_file("installer.cfg", self.destinationPath)
|
||||
|
||||
def backupMails(self):
|
||||
|
||||
def mail_backup(self):
|
||||
if self.nomail:
|
||||
utils.printcolor(
|
||||
"Skipping mail backup, no-mail argument provided", utils.MAGENTA)
|
||||
@@ -131,7 +122,7 @@ class Backup():
|
||||
shutil.copytree(home_path, dst)
|
||||
utils.printcolor("Mail backup complete!", utils.GREEN)
|
||||
|
||||
def backupCustomConfig(self):
|
||||
def custom_config_backup(self):
|
||||
"""Custom config :
|
||||
- Amavis : /etc/amavis/conf.d/99-custom
|
||||
- Postwhite : /etc/postwhite.conf
|
||||
@@ -140,61 +131,57 @@ class Backup():
|
||||
"Backing up some custom configuration...", utils.MAGENTA)
|
||||
|
||||
custom_path = os.path.join(
|
||||
self.destinationPath, self.BACKUPDIRECTORY[0])
|
||||
self.destinationPath, "custom")
|
||||
|
||||
"""AMAVIS"""
|
||||
amavis_custom = "/etc/amavis/conf.d/99-custom"
|
||||
if os.path.isfile(amavis_custom):
|
||||
utils.copy_file(amavis_custom, custom_path)
|
||||
utils.printcolor("Amavis custom configuration saved!", utils.GREEN)
|
||||
# AMAVIS
|
||||
if (self.config.has_option("amavis", "enabled") and
|
||||
self.config.getboolean("amavis", "enabled")):
|
||||
amavis_custom = "/etc/amavis/conf.d/99-custom"
|
||||
if os.path.isfile(amavis_custom):
|
||||
utils.copy_file(amavis_custom, custom_path)
|
||||
utils.printcolor(
|
||||
"Amavis custom configuration saved!", utils.GREEN)
|
||||
|
||||
"""POSTWHITE"""
|
||||
postswhite_custom = "/etc/postwhite.conf"
|
||||
if os.path.isfile(postswhite_custom):
|
||||
utils.copy_file(postswhite_custom, custom_path)
|
||||
utils.printcolor("Postwhite configuration saved!", utils.GREEN)
|
||||
# POSTWHITE
|
||||
if (self.config.has_option("postwhite", "enabled") and
|
||||
self.config.getboolean("postwhite", "enabled")):
|
||||
postswhite_custom = os.path.join(self.config.get(
|
||||
"postwhite", "config_dir", "postwhite.conf"))
|
||||
if os.path.isfile(postswhite_custom):
|
||||
utils.copy_file(postswhite_custom, custom_path)
|
||||
utils.printcolor(
|
||||
"Postwhite configuration saved!", utils.GREEN)
|
||||
|
||||
def backupDBs(self):
|
||||
def database_backup(self):
|
||||
"""Backing up databases"""
|
||||
|
||||
utils.printcolor("Backing up databases...", utils.MAGENTA)
|
||||
|
||||
dump_path = os.path.join(self.destinationPath, self.BACKUPDIRECTORY[1])
|
||||
self.database_dump("modoboa")
|
||||
self.database_dump("amavis")
|
||||
self.database_dump("spamassassin")
|
||||
|
||||
def database_dump(self, app_name):
|
||||
|
||||
dump_path = os.path.join(self.destinationPath, "backup")
|
||||
backend = database.get_backend(self.config)
|
||||
|
||||
"""Modoboa"""
|
||||
dbname = self.config.get("modoboa", "dbname")
|
||||
dbuser = self.config.get("modoboa", "dbuser")
|
||||
dbpasswd = self.config.get("modoboa", "dbpassword")
|
||||
backend.dumpDatabase(dbname, dbuser, dbpasswd,
|
||||
os.path.join(dump_path, "modoboa.sql"))
|
||||
if app_name == "modoboa" or (self.config.has_option(app_name, "enabled") and
|
||||
self.config.getboolean(app_name, "enabled")):
|
||||
dbname = self.config.get(app_name, "dbname")
|
||||
dbuser = self.config.get(app_name, "dbuser")
|
||||
dbpasswd = self.config.get(app_name, "dbpassword")
|
||||
backend.dump_database(dbname, dbuser, dbpasswd,
|
||||
os.path.join(dump_path, f"{app_name}.sql"))
|
||||
|
||||
"""Amavis"""
|
||||
if (self.config.has_option("amavis", "enabled") and
|
||||
self.config.getboolean("amavis", "enabled")):
|
||||
dbname = self.config.get("amavis", "dbname")
|
||||
dbuser = self.config.get("amavis", "dbuser")
|
||||
dbpasswd = self.config.get("amavis", "dbpassword")
|
||||
backend.dumpDatabase(dbname, dbuser, dbpasswd,
|
||||
os.path.join(dump_path, "amavis.sql"))
|
||||
|
||||
"""SpamAssassin"""
|
||||
if (self.config.has_option("spamassassin", "enabled") and
|
||||
self.config.getboolean("spamassassin", "enabled")):
|
||||
dbname = self.config.get("spamassassin", "dbname")
|
||||
dbuser = self.config.get("spamassassin", "dbuser")
|
||||
dbpasswd = self.config.get("spamassassin", "dbpassword")
|
||||
backend.dumpDatabase(dbname, dbuser, dbpasswd,
|
||||
os.path.join(dump_path, "spamassassin.sql"))
|
||||
|
||||
def backupCompletion(self):
|
||||
def backup_completed(self):
|
||||
utils.printcolor("Backup process done, your backup is availible here:"
|
||||
f"--> {self.destinationPath}", utils.GREEN)
|
||||
|
||||
def run(self):
|
||||
self.setPath()
|
||||
self.backupConfigFile()
|
||||
self.backupMails()
|
||||
self.backupCustomConfig()
|
||||
self.backupDBs()
|
||||
self.backupCompletion()
|
||||
self.set_path()
|
||||
self.config_file_backup()
|
||||
self.mail_backup()
|
||||
self.custom_config_backup()
|
||||
self.database_backup()
|
||||
self.backup_completed()
|
||||
|
||||
@@ -161,6 +161,10 @@ class Installer(object):
|
||||
"""Tasks to execute before the installer starts."""
|
||||
pass
|
||||
|
||||
def restore(self):
|
||||
"""Tasks to execute to restore files/databases."""
|
||||
pass
|
||||
|
||||
def post_run(self):
|
||||
"""Additionnal tasks."""
|
||||
pass
|
||||
|
||||
@@ -5,29 +5,22 @@ from .. import utils
|
||||
|
||||
class Restore:
|
||||
def __init__(self, restore):
|
||||
"""Restoring pre-check (backup integriety)"""
|
||||
"""REQUIRED : modoboa.sql"""
|
||||
"""OPTIONAL : mails/, custom/, amavis.sql, spamassassin.sql"""
|
||||
"""Only checking required"""
|
||||
"""
|
||||
Restoring pre-check (backup integriety)
|
||||
REQUIRED : modoboa.sql
|
||||
OPTIONAL : mails/, custom/, amavis.sql, spamassassin.sql
|
||||
Only checking required
|
||||
"""
|
||||
|
||||
try:
|
||||
if not os.path.isdir(restore):
|
||||
utils.printcolor(
|
||||
"Provided path is not a directory !", utils.RED)
|
||||
sys.exit(1)
|
||||
except:
|
||||
utils.printcolor("Provided path is not right...", utils.RED)
|
||||
if not os.path.isdir(restore):
|
||||
utils.printcolor(
|
||||
"Provided path is not a directory !", utils.RED)
|
||||
sys.exit(1)
|
||||
|
||||
try:
|
||||
modobasql_file = os.path.join(restore, "databases/modoboa.sql")
|
||||
if not os.path.isfile(modobasql_file):
|
||||
utils.printcolor(
|
||||
modobasql_file + " not found, please check your backup", utils.RED)
|
||||
sys.exit(1)
|
||||
except:
|
||||
utils.printcolor(modobasql_file +
|
||||
" not found, please check your backup", utils.RED)
|
||||
modobasql_file = os.path.join(restore, "databases/modoboa.sql")
|
||||
if not os.path.isfile(modobasql_file):
|
||||
utils.printcolor(
|
||||
modobasql_file + " not found, please check your backup", utils.RED)
|
||||
sys.exit(1)
|
||||
|
||||
# Everything seems allright here, proceding...
|
||||
|
||||
Reference in New Issue
Block a user