Browse Source

Add log trucate flag and timestamps to logs

Nathaniel van Diepen 1 year ago
parent
commit
672a2c129a
2 changed files with 14 additions and 6 deletions
  1. 13 6
      backup/__init__.py
  2. 1 0
      etc/backup.d/backup.yml

+ 13 - 6
backup/__init__.py

@@ -8,6 +8,7 @@ import sys
 import time
 import yaml
 
+from datetime import datetime
 from glob import glob
 from slugify import slugify
 
@@ -185,7 +186,7 @@ class Job(object):
             if not os.path.exists(path):
                 os.makedirs(path, exist_ok=True)
 
-            self._logfile = open(self.logpath, 'w')
+            self._logfile = open(self.logpath, 'w' if Backup.truncateLogs else 'a')
 
         return self._logfile
 
@@ -265,8 +266,8 @@ class Job(object):
         return self._post
 
     def log(self, text):
-        text = '[Backup {0} Job #{1}] {2}\n'.format(
-                self._backup.name, self.index, text)
+        text = '[{0}] (Backup {1} Job #{2}) {3}\n'.format(
+                datetime.now().isoformat(), self._backup.name, self.index, text)
         print(text, end='')
         self._backup.logfile.write(text)
         self._backup.logfile.flush()
@@ -291,7 +292,10 @@ class Job(object):
             raise StateException('Invalid state to start {}'.format(self))
 
         args = self.args
-        self.logfile.write(' '.join([shlex.quote(x) for x in args]) + '\n')
+        self.logfile.write(
+            "[{0}] {1} &\n".format(
+                datetime.now().isoformat(),
+                ' '.join([shlex.quote(x) for x in args])))
         self.logfile.flush()
         self._process = subprocess.Popen(
                 args, stdout=self.logfile, stderr=subprocess.STDOUT,
@@ -321,6 +325,7 @@ class Backup(object):
     instances = {}
     _queue = []
     logTransitions = False
+    truncateLogs = True
     engine = 'rdiff-backup'
     logdir = '/var/log/backup.d'
     BLOCKED = 0
@@ -416,12 +421,13 @@ class Backup(object):
             if not os.path.exists(path):
                 os.makedirs(path, exist_ok=True)
 
-            self._logfile = open(self.logpath, 'w+')
+            self._logfile = open(self.logpath, 'w' if Backup.truncateLogs else 'a')
 
         return self._logfile
 
     def log(self, text):
-        text = '[Backup {0}] {1}\n'.format(self.name, text)
+        text = '[{0}] (Backup {1}) {2}\n'.format(
+            datetime.now().isoformat(), self.name, text)
         print(text, end='')
         self.logfile.write(text)
         self.logfile.flush()
@@ -596,6 +602,7 @@ def main(args):
             Backup.verbosity = config()['verbosity']
 
         Backup.logTransitions = Job.logTransitions = True
+        Backup.truncateLogs = "truncateLogs" in config() and config["truncateLogs"]
         Backup.load(sources().keys())
         Backup.start()
         Backup.wait()

+ 1 - 0
etc/backup.d/backup.yml

@@ -3,5 +3,6 @@ engine: rdiff-backup
 verbosity: 3
 maxthreads: 4
 logdir: var/log/backup.d
+truncatelogs: true
 sources:
  - sources.d