From d4240be827e9527f6bb5a950b2563f495ae3849a Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sun, 13 Mar 2022 20:11:05 +0100 Subject: ... --- hosts/vidhar/borg/copy.py | 165 ++++++++++++++++++++++------------------------ 1 file changed, 79 insertions(+), 86 deletions(-) (limited to 'hosts/vidhar/borg/copy.py') diff --git a/hosts/vidhar/borg/copy.py b/hosts/vidhar/borg/copy.py index 324fea0b..4e9599b8 100755 --- a/hosts/vidhar/borg/copy.py +++ b/hosts/vidhar/borg/copy.py @@ -171,94 +171,87 @@ def copy_archive(src_repo_path, dst_repo_path, entry): else: print('Mounted', file=stderr) while True: - try: - with tqdm(total=total_size, unit_scale=True, unit_divisor=1024, unit='B', smoothing=0.01, disable=None, dynamic_ncols=True, maxinterval=0.5, miniters=1) as progress: - seen = 0 - env = os.environ.copy() - create_args = ['borg', - 'create', - '--lock-wait=600', - '--one-file-system', - '--compression=auto,zstd,10', - '--chunker-params=10,23,16,4095', - '--files-cache=ctime,size', - '--show-rc', - # '--remote-ratelimit=20480', - '--log-json', - '--progress', - '--list', - '--filter=AMEi-x?', - '--stats' - ] - archive_time = datetime.strptime(entry["time"], "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=tzlocal()).astimezone(tzutc()) - create_args += [f'--timestamp={archive_time.strftime("%Y-%m-%dT%H:%M:%S")}'] - if cache_suffix: - env['BORG_FILES_CACHE_SUFFIX'] = cache_suffix - else: - create_args += ['--files-cache=disabled'] - create_args += [f'{dst_repo_path}::{entry["name"]}', '.'] - - with subprocess.Popen(create_args, stdin=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True, env=env, preexec_fn=lambda: as_borg(caps={Cap.DAC_READ_SEARCH}, cwd=dir)) as proc: - last_list = None - last_list_time = None - for line in proc.stderr: - try: - json_line = json.loads(line) - except json.decoder.JSONDecodeError: - tqdm.write(line) - continue - - t = '' - if 'time' in json_line and stderr.isatty(): - ts = datetime.fromtimestamp(json_line['time']).replace(tzinfo=tzlocal()) - t = f'{ts.isoformat(timespec="minutes")} ' - if json_line['type'] == 'archive_progress': - if last_list_time is None or ((datetime.now() - last_list_time) // timedelta(seconds=3)) % 2 == 1: - if 'path' in json_line and json_line['path']: - progress.set_description(f'… {json_line["path"]}', refresh=False) - else: - progress.set_description(None, refresh=False) - elif last_list is not None: - progress.set_description(last_list, refresh=False) - nfiles=json_line["nfiles"] - if total_files is not None: - nfiles=f'{json_line["nfiles"]}/{total_files}' - progress.set_postfix(compressed=naturalsize(json_line['compressed_size'], binary=True), deduplicated=naturalsize(json_line['deduplicated_size'], binary=True), nfiles=nfiles, refresh=False) - progress.update(json_line["original_size"] - seen) - seen = json_line["original_size"] - elif json_line['type'] == 'file_status': - # tqdm.write(t + f'{json_line["status"]} {json_line["path"]}') - last_list = f'{json_line["status"]} {json_line["path"]}' - last_list_time = datetime.now() + with tqdm(total=total_size, unit_scale=True, unit_divisor=1024, unit='B', smoothing=0.01, disable=None, dynamic_ncols=True, maxinterval=0.5, miniters=1) as progress: + seen = 0 + env = os.environ.copy() + create_args = ['borg', + 'create', + '--lock-wait=600', + '--one-file-system', + '--compression=auto,zstd,10', + '--chunker-params=10,23,16,4095', + '--files-cache=ctime,size', + '--show-rc', + # '--remote-ratelimit=20480', + '--log-json', + '--progress', + '--list', + '--filter=AMEi-x?', + '--stats' + ] + archive_time = datetime.strptime(entry["time"], "%Y-%m-%dT%H:%M:%S.%f").replace(tzinfo=tzlocal()).astimezone(tzutc()) + create_args += [f'--timestamp={archive_time.strftime("%Y-%m-%dT%H:%M:%S")}'] + if cache_suffix: + env['BORG_FILES_CACHE_SUFFIX'] = cache_suffix + else: + create_args += ['--files-cache=disabled'] + create_args += [f'{dst_repo_path}::{entry["name"]}', '.'] + + with subprocess.Popen(create_args, stdin=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True, env=env, preexec_fn=lambda: as_borg(caps={Cap.DAC_READ_SEARCH}, cwd=dir)) as proc: + last_list = None + last_list_time = None + for line in proc.stderr: + try: + json_line = json.loads(line) + except json.decoder.JSONDecodeError: + tqdm.write(line) + continue + + t = '' + if 'time' in json_line and stderr.isatty(): + ts = datetime.fromtimestamp(json_line['time']).replace(tzinfo=tzlocal()) + t = f'{ts.isoformat(timespec="minutes")} ' + if json_line['type'] == 'archive_progress': + if last_list_time is None or ((datetime.now() - last_list_time) // timedelta(seconds=3)) % 2 == 1: + if 'path' in json_line and json_line['path']: + progress.set_description(f'… {json_line["path"]}', refresh=False) + else: + progress.set_description(None, refresh=False) + elif last_list is not None: progress.set_description(last_list, refresh=False) - if not stderr.isatty(): - print(last_list, file=stderr) - elif (json_line['type'] == 'log_message' or json_line['type'] == 'progress_message' or json_line['type'] == 'progress_percent') and ('message' in json_line or 'msgid' in json_line): - if 'message' in json_line: - tqdm.write(t + json_line['message']) - elif 'msgid' in json_line: - tqdm.write(t + json_line['msgid']) - else: - tqdm.write(t + line) - progress.set_description(None) - if proc.wait() != 0: + nfiles=json_line["nfiles"] + if total_files is not None: + nfiles=f'{json_line["nfiles"]}/{total_files}' + progress.set_postfix(compressed=naturalsize(json_line['compressed_size'], binary=True), deduplicated=naturalsize(json_line['deduplicated_size'], binary=True), nfiles=nfiles, refresh=False) + progress.update(json_line["original_size"] - seen) + seen = json_line["original_size"] + elif json_line['type'] == 'file_status': + # tqdm.write(t + f'{json_line["status"]} {json_line["path"]}') + last_list = f'{json_line["status"]} {json_line["path"]}' + last_list_time = datetime.now() + progress.set_description(last_list, refresh=False) + if not stderr.isatty(): + print(last_list, file=stderr) + elif (json_line['type'] == 'log_message' or json_line['type'] == 'progress_message' or json_line['type'] == 'progress_percent') and ('message' in json_line or 'msgid' in json_line): + if 'message' in json_line: + tqdm.write(t + json_line['message']) + elif 'msgid' in json_line: + tqdm.write(t + json_line['msgid']) + else: + tqdm.write(t + line) + progress.set_description(None) + if proc.wait() != 0: + dst = None + try: + dst = read_repo(args.target) + except (subprocess.CalledProcessError, json.decoder.JSONDecodeError) as err: + print(err, file=stderr) continue - except subprocess.CalledProcessError as err: - print(err, file=stderr) - - dst = None - try: - dst = read_repo(args.target) - except (subprocess.CalledProcessError, json.decoder.JSONDecodeError) as err: - print(err, file=stderr) - continue - else: - if any(map(lambda other: entry['name'] == other['name'], dst)): - break - - continue - else: - break + else: + if any(map(lambda other: entry['name'] == other['name'], dst)): + break + + continue mount_proc.terminate() os._exit(0) else: -- cgit v1.2.3