From c7f9377206ceaaa9e03671973aaad5052eb5a216 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Thu, 29 Oct 2020 09:25:22 +0100 Subject: repository: log backup time and size stats --- lbup/repository.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lbup/repository.py b/lbup/repository.py index 377be61..d3a6b33 100644 --- a/lbup/repository.py +++ b/lbup/repository.py @@ -3,6 +3,7 @@ import logging import os import os.path import subprocess +import datetime class StepResult: success = None @@ -32,6 +33,8 @@ class Repo: data_dir = None lock_name = 'lock' + sizestat_name = 'size' + _logger = None def __init__(self, bup_dir = None, data_dir = None, logger = None): @@ -55,6 +58,12 @@ class Repo: self.bup_dir = bup_dir self.data_dir = data_dir + def _bup_repo_size(self): + ret = subprocess.run(['du', '--summarize', '--bytes', '--dereference-args', + '--exclude=midx*', '--exclude=bup.bloom', self.bup_dir], + capture_output = True, check = True) + return int(ret.stdout.split()[0].strip()) + def backup(self, tgts, *, gen_par2 = True, dry_run = False): """ Backup the supplied targets. @@ -70,6 +79,13 @@ class Repo: fcntl.lockf(lockfile, fcntl.LOCK_EX) try: for tgt in tgts: + # make sure the per-target data dir exists + data_dir = os.path.join(self.data_dir, tgt.name) + os.makedirs(data_dir, 0o700, exist_ok = True) + + # measure the pre-backup size + size_pre = self._bup_repo_size() + self._logger.info('Backing up %s...' % tgt.name) try: res = tgt.save(dry_run) @@ -79,6 +95,14 @@ class Repo: else: self._logger.info('Backing up %s done' % tgt.name) + # measure the post-backup size + size_post = self._bup_repo_size() + + backup_time = datetime.datetime.now(datetime.timezone.utc) + + with open(os.path.join(data_dir, self.sizestat_name), 'a') as f: + f.write('%s %d\n' % (backup_time.isoformat(timespec = 'seconds'), size_post - size_pre)) + result.target_results[tgt.name] = res if gen_par2: -- cgit v1.2.3