Fixed restore mode

This commit is contained in:
Antoine Nguyen
2022-11-09 10:30:44 +01:00
parent d6f9a5b913
commit 37633008cb
12 changed files with 103 additions and 63 deletions

View File

@@ -17,7 +17,7 @@ def load_app_script(appname):
return script
def install(appname, config, upgrade, restore):
def install(appname: str, config, upgrade: bool, archive_path: str):
"""Install an application."""
if (config.has_option(appname, "enabled") and
not config.getboolean(appname, "enabled")):
@@ -26,9 +26,9 @@ def install(appname, config, upgrade, restore):
utils.printcolor("Installing {}".format(appname), utils.MAGENTA)
script = load_app_script(appname)
try:
getattr(script, appname.capitalize())(config, upgrade, restore).run()
getattr(script, appname.capitalize())(config, upgrade, archive_path).run()
except utils.FatalError as inst:
utils.printcolor(u"{}".format(inst), utils.RED)
utils.error("{}".format(inst))
sys.exit(1)
@@ -43,7 +43,7 @@ def backup(appname, config, path):
try:
getattr(script, appname.capitalize())(config, False, False).backup(path)
except utils.FatalError as inst:
utils.printcolor(u"{}".format(inst), utils.RED)
utils.error("{}".format(inst))
sys.exit(1)

View File

@@ -92,8 +92,8 @@ class Amavis(base.Installer):
def post_run(self):
"""Additional tasks."""
install("spamassassin", self.config, self.upgrade, self.restore)
install("clamav", self.config, self.upgrade, self.restore)
install("spamassassin", self.config, self.upgrade, self.archive_path)
install("clamav", self.config, self.upgrade, self.archive_path)
def custom_backup(self, path):
"""Backup custom configuration if any."""
@@ -109,7 +109,7 @@ class Amavis(base.Installer):
if package.backend.FORMAT != "deb":
return
amavis_custom_configuration = os.path.join(
self.restore, "custom/99-custom")
self.archive_path, "custom/99-custom")
if os.path.isfile(amavis_custom_configuration):
utils.copy_file(amavis_custom_configuration, os.path.join(
self.config_dir, "conf.d"))

View File

@@ -20,11 +20,11 @@ class Installer(object):
with_db = False
config_files = []
def __init__(self, config, upgrade, restore):
def __init__(self, config, upgrade: bool, archive_path: str):
"""Get configuration."""
self.config = config
self.upgrade = upgrade
self.restore = restore
self.archive_path = archive_path
if self.config.has_section(self.appname):
self.app_config = dict(self.config.items(self.appname))
self.dbengine = self.config.get("database", "engine")
@@ -61,7 +61,7 @@ class Installer(object):
utils.MAGENTA
)
database_backup_path = os.path.join(
self.restore, f"databases/{self.appname}.sql")
self.archive_path, f"databases/{self.appname}.sql")
if os.path.isfile(database_backup_path):
utils.success(f"SQL dump found in backup for {self.appname}!")
return database_backup_path
@@ -81,7 +81,7 @@ class Installer(object):
self.backend.create_user(self.dbuser, self.dbpasswd)
self.backend.create_database(self.dbname, self.dbuser)
schema = None
if self.restore:
if self.archive_path:
schema = self.get_sql_schema_from_backup()
if not schema:
schema = self.get_sql_schema_path()
@@ -188,9 +188,9 @@ class Installer(object):
if not self.upgrade:
self.setup_database()
self.install_config_files()
if self.restore:
self.restore()
self.post_run()
if self.archive_path:
self.restore()
self.restart_daemon()
def _dump_database(self, backup_path: str):

View File

@@ -153,7 +153,7 @@ class Dovecot(base.Installer):
def restore(self):
"""Restore emails."""
home_dir = self.config.get("dovecot", "home_dir")
mail_dir = os.path.join(self.restore, "mails/")
mail_dir = os.path.join(self.archive_path, "mails/")
if len(os.listdir(mail_dir)) > 0:
utils.success("Copying mail backup over dovecot directory.")
if os.path.exists(home_dir):

View File

@@ -47,19 +47,7 @@ class Opendkim(base.Installer):
stat.S_IROTH | stat.S_IXOTH,
target[1], target[2]
)
# Restore dkim keys from backup if restoring
if self.restore is not None:
dkim_keys_backup = os.path.join(
self.restore, "custom/dkim")
if os.path.isdir(dkim_keys_backup):
for file in os.listdir(dkim_keys_backup):
file_path = os.path.join(dkim_keys_backup, file)
if os.path.isfile(file_path):
utils.copy_file(file_path, self.config.get(
"opendkim", "keys_storage_dir", fallback="/var/lib/dkim"))
utils.printcolor(
"DKIM keys restored from backup", utils.GREEN)
super(Opendkim, self).install_config_files()
super().install_config_files()
def get_template_context(self):
"""Additional variables."""
@@ -123,6 +111,18 @@ class Opendkim(base.Installer):
utils.exec_cmd(
"perl -pi -e '{}' /lib/systemd/system/opendkim.service".format(pattern))
def restore(self):
"""Restore keys."""
dkim_keys_backup = os.path.join(
self.archive_path, "custom/dkim")
if os.path.isdir(dkim_keys_backup):
for file in os.listdir(dkim_keys_backup):
file_path = os.path.join(dkim_keys_backup, file)
if os.path.isfile(file_path):
utils.copy_file(file_path, self.config.get(
"opendkim", "keys_storage_dir", fallback="/var/lib/dkim"))
utils.success("DKIM keys restored from backup")
def custom_backup(self, path):
"""Backup DKIM keys."""
storage_dir = self.config.get(

View File

@@ -97,7 +97,7 @@ class Postfix(base.Installer):
utils.exec_cmd("postalias {}".format(aliases_file))
# Postwhite
install("postwhite", self.config, self.upgrade, self.restore)
install("postwhite", self.config, self.upgrade, self.archive_path)
def backup(self, path):
"""Launch postwhite backup."""

View File

@@ -61,7 +61,7 @@ class Postwhite(base.Installer):
def restore(self):
"""Restore config files."""
postwhite_backup_configuration = os.path.join(
self.restore, "custom/postwhite.conf")
self.archive_path, "custom/postwhite.conf")
if os.path.isfile(postwhite_backup_configuration):
utils.copy_file(postwhite_backup_configuration, self.config_dir)
utils.success("postwhite.conf restored from backup")

View File

@@ -26,7 +26,7 @@ class Radicale(base.Installer):
def __init__(self, *args, **kwargs):
"""Get configuration."""
super(Radicale, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
self.venv_path = self.config.get("radicale", "venv_path")
def _setup_venv(self):
@@ -76,7 +76,7 @@ class Radicale(base.Installer):
def restore(self):
"""Restore collections."""
radicale_backup = os.path.join(
self.restore, "custom/radicale")
self.archive_path, "custom/radicale")
if os.path.isdir(radicale_backup):
restore_target = os.path.join(self.home_dir, "collections")
if os.path.isdir(restore_target):