DKIM keys restore, Radicale backup/restore, fixes

This commit is contained in:
Spitap
2022-10-25 16:58:57 +02:00
parent 4c1f8710b5
commit 5c9d5c9a03
8 changed files with 62 additions and 22 deletions

View File

@@ -42,13 +42,14 @@ class Amavis(base.Installer):
def get_config_files(self): def get_config_files(self):
"""Return appropriate config files.""" """Return appropriate config files."""
if package.backend.FORMAT == "deb": if package.backend.FORMAT == "deb":
amavis_custom_configuration = os.path.join( if self.restore is not None:
self.restore, "custom/99-custom") amavis_custom_configuration = os.path.join(
if self.restore and os.path.isfile(amavis_custom_configuration): self.restore, "custom/99-custom")
utils.copy_file(amavis_custom_configuration, os.path.join( if os.path.isfile(amavis_custom_configuration):
self.config_dir, "/conf.d")) utils.copy_file(amavis_custom_configuration, os.path.join(
utils.printcolor( self.config_dir, "conf.d"))
"Custom amavis configuration restored.", utils.GREEN) utils.printcolor(
"Custom amavis configuration restored.", utils.GREEN)
return [ return [
"conf.d/05-node_id", "conf.d/15-content_filter_mode", "conf.d/05-node_id", "conf.d/15-content_filter_mode",
"conf.d/50-user"] "conf.d/50-user"]

View File

@@ -22,6 +22,7 @@ class Backup:
|--> { (copy of) /etc/postfix/custom_whitelist.cidr } |--> { (copy of) /etc/postfix/custom_whitelist.cidr }
|--> { (copy of) dkim folder } |--> { (copy of) dkim folder }
|--> {dkim.pem}... |--> {dkim.pem}...
|--> { (copy of) radicale home_dir }
||--> databases ||--> databases
|--> modoboa.sql |--> modoboa.sql
|--> { amavis.sql } |--> { amavis.sql }
@@ -144,6 +145,7 @@ class Backup:
def custom_config_backup(self): def custom_config_backup(self):
"""Custom config : """Custom config :
- DKIM keys: {{keys_storage_dir}} - DKIM keys: {{keys_storage_dir}}
- Radicale collection (calendat, contacts): {{home_dir}}
- Amavis : /etc/amavis/conf.d/99-custom - Amavis : /etc/amavis/conf.d/99-custom
- Postwhite : /etc/postwhite.conf - Postwhite : /etc/postwhite.conf
Feel free to suggest to add others!""" Feel free to suggest to add others!"""
@@ -158,9 +160,20 @@ class Backup:
self.config.getboolean("opendkim", "enabled")): self.config.getboolean("opendkim", "enabled")):
dkim_keys = self.config.get( dkim_keys = self.config.get(
"opendkim", "keys_storage_dir", fallback="/var/lib/dkim") "opendkim", "keys_storage_dir", fallback="/var/lib/dkim")
shutil.copytree(dkim_keys, os.path.join(custom_path, "dkim")) if os.path.isdir(dkim_keys):
utils.printcolor( shutil.copytree(dkim_keys, os.path.join(custom_path, "dkim"))
"DKIM keys saved!", utils.GREEN) utils.printcolor(
"DKIM keys saved!", utils.GREEN)
# Radicale Collections
if (self.config.has_option("radicale", "enabled") and
self.config.getboolean("radicale", "enabled")):
radicale_backup = os.path.join(self.config.get(
"radicale", "home_dir", fallback="/srv/radicale"), "collections")
if os.path.isdir(radicale_backup):
shutil.copytree(radicale_backup, os.path.join(
custom_path, "radicale"))
utils.printcolor("Radicale files saved", utils.GREEN)
# AMAVIS # AMAVIS
if (self.config.has_option("amavis", "enabled") and if (self.config.has_option("amavis", "enabled") and

View File

@@ -89,7 +89,7 @@ class Dovecot(base.Installer):
def post_run(self): def post_run(self):
"""Additional tasks.""" """Additional tasks."""
mail_dir = os.path.join(self.restore, "mails/") mail_dir = os.path.join(self.restore, "mails/")
if self.restore and len(os.listdir(mail_dir)) > 0: if self.restore is not None and len(os.listdir(mail_dir)) > 0:
utils.printcolor( utils.printcolor(
"Copying mail backup over dovecot directory.", utils.GREEN) "Copying mail backup over dovecot directory.", utils.GREEN)
@@ -103,7 +103,7 @@ class Dovecot(base.Installer):
for filename in filenames: for filename in filenames:
shutil.chown(os.path.join(dirpath, filename), shutil.chown(os.path.join(dirpath, filename),
self.user, self.user) self.user, self.user)
elif self.restore: elif self.restore is not None:
utils.printcolor( utils.printcolor(
"It seems that mails were not backed up, skipping mail restoration.", utils.MAGENTA) "It seems that mails were not backed up, skipping mail restoration.", utils.MAGENTA)

View File

@@ -193,7 +193,7 @@ class Modoboa(base.Installer):
self.config.get("amavis", "dbname"), self.dbuser) self.config.get("amavis", "dbname"), self.dbuser)
def get_sql_schema_path(self): def get_sql_schema_path(self):
if self.restore: if self.restore is not None:
db_dump_path = self._restore_database_dump("modoboa") db_dump_path = self._restore_database_dump("modoboa")
if db_dump_path is not None: if db_dump_path is not None:
return db_dump_path return db_dump_path

View File

@@ -46,6 +46,18 @@ class Opendkim(base.Installer):
stat.S_IROTH | stat.S_IXOTH, stat.S_IROTH | stat.S_IXOTH,
target[1], target[2] 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(Opendkim, self).install_config_files()
def get_template_context(self): def get_template_context(self):

View File

@@ -47,13 +47,15 @@ class Postwhite(base.Installer):
self.install_from_archive(SPF_TOOLS_REPOSITORY, install_dir) self.install_from_archive(SPF_TOOLS_REPOSITORY, install_dir)
postw_dir = self.install_from_archive( postw_dir = self.install_from_archive(
POSTWHITE_REPOSITORY, install_dir) POSTWHITE_REPOSITORY, install_dir)
postwhite_backup_configuration = os.path.join( # Attempt to restore config file from backup
self.restore, "custom/postwhite.conf") if self.restore is not None:
if self.restore and os.path.isfile(postwhite_backup_configuration): postwhite_backup_configuration = os.path.join(
utils.copy_file(postwhite_backup_configuration, "/etc") self.restore, "custom/postwhite.conf")
utils.printcolor( if os.path.isfile(postwhite_backup_configuration):
"postwhite.conf restored from backup", utils.GREEN) utils.copy_file(postwhite_backup_configuration, self.config_dir)
else: utils.printcolor(
utils.copy_file(os.path.join(postw_dir, "postwhite.conf"), "/etc") "postwhite.conf restored from backup", utils.GREEN)
else:
utils.copy_file(os.path.join(postw_dir, "postwhite.conf"), self.config_dir)
postw_bin = os.path.join(postw_dir, "postwhite") postw_bin = os.path.join(postw_dir, "postwhite")
utils.exec_cmd("{} /etc/postwhite.conf".format(postw_bin)) utils.exec_cmd("{} /etc/postwhite.conf".format(postw_bin))

View File

@@ -1,6 +1,7 @@
"""Radicale related tasks.""" """Radicale related tasks."""
import os import os
import shutil
import stat import stat
from .. import package from .. import package
@@ -70,6 +71,17 @@ class Radicale(base.Installer):
stat.S_IROTH | stat.S_IXOTH, stat.S_IROTH | stat.S_IXOTH,
0, 0 0, 0
) )
# Attempt to restore radicale collections from backup
if self.restore is not None:
radicale_backup = os.path.join(
self.restore, "custom/radicale")
if os.path.isdir(radicale_backup):
restore_target = os.path.join(self.home_dir, "collections")
if os.path.isdir(restore_target):
shutil.rmtree(restore_target)
shutil.copytree(radicale_backup, restore_target)
utils.printcolor(
"Radicale collections restored from backup", utils.GREEN)
super(Radicale, self).install_config_files() super(Radicale, self).install_config_files()
def post_run(self): def post_run(self):

View File

@@ -25,7 +25,7 @@ class Spamassassin(base.Installer):
def get_sql_schema_path(self): def get_sql_schema_path(self):
"""Return SQL schema.""" """Return SQL schema."""
if self.restore: if self.restore is not None:
db_dump_path = self._restore_database_dump("spamassassin") db_dump_path = self._restore_database_dump("spamassassin")
if db_dump_path is not None: if db_dump_path is not None:
return db_dump_path return db_dump_path