Browse Source

acme_tlsa_mail: create backup of destination file, not the source file, make backups optional but the default

Helmut Pozimski 7 years ago
parent
commit
96fa329c63
1 changed files with 18 additions and 15 deletions
  1. 18 15
      acme_tlsa_mail.py

+ 18 - 15
acme_tlsa_mail.py

@@ -128,7 +128,7 @@ def create_tlsa_hash(cert):
     return hexdigest
 
 
-def copy_file(source, destination):
+def copy_file(source, destination, backup=True):
     """
     Copies a file from the given source file to
     the given destionation and creates a copy of the
@@ -136,28 +136,31 @@ def copy_file(source, destination):
 
     :param source: source file path
     :type source: str
-    :param destination: destionation file path
+    :param destination: destination file path
     :type destination: str
+    :param backup: whether to take a backup of the destination file before \
+    overwriting it
+    :type backup: bool
     :return: success
     :rtype: bool
     """
-    backup_file = source + ".bak_%s" % datetime.datetime.now().strftime(
+    backup_file = destination + ".bak_%s" % datetime.datetime.now().strftime(
         "%Y%m%d%H%M%S")
+    if backup:
+        try:
+            shutil.copy(destination, backup_file)
+        except IOError:
+            LOGGER.error("Creating of backup file for %s failed!", source)
+            return False
     try:
-        shutil.copy(source, backup_file)
+        shutil.copy(source, destination)
     except IOError:
-        LOGGER.error("Creating of backup file for %s failed!", source)
-        return False
+        LOGGER.error("Copying of file %s to %s failed!",
+                     source, destination)
     else:
-        try:
-            shutil.copy(source, destination)
-        except IOError:
-            LOGGER.error("Copying of file %s to %s failed!",
-                         source, destination)
-        else:
-            os.chmod(destination, 0o0644)
-            os.chown(destination, 0, 0)
-            return True
+        os.chmod(destination, 0o0644)
+        os.chown(destination, 0, 0)
+        return True
 
 
 def get_tsig_key():