Browse Source

acme_updater: create backup of destination file, not the source file, make backups optional but the default (fixes #4)

Helmut Pozimski 7 years ago
parent
commit
91cab69bf6
1 changed files with 17 additions and 14 deletions
  1. 17 14
      acme_updater.py

+ 17 - 14
acme_updater.py

@@ -92,7 +92,7 @@ def parse_asn1_time(timestamp):
     return date
 
 
-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
@@ -102,26 +102,29 @@ def copy_file(source, destination):
     :type source: str
     :param destination: destionation 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
 
 for vhost in os.listdir(VHOSTS_DIRECTORY):
     if vhost.endswith(".conf"):