summaryrefslogtreecommitdiff
path: root/hosts
diff options
context:
space:
mode:
Diffstat (limited to 'hosts')
-rwxr-xr-xhosts/vidhar/borg/copy.py26
1 files changed, 19 insertions, 7 deletions
diff --git a/hosts/vidhar/borg/copy.py b/hosts/vidhar/borg/copy.py
index 679c9ad1..db907297 100755
--- a/hosts/vidhar/borg/copy.py
+++ b/hosts/vidhar/borg/copy.py
@@ -33,17 +33,20 @@ parser.add_argument('target', metavar='REPO_OR_ARCHIVE')
33args = parser.parse_args() 33args = parser.parse_args()
34 34
35halo_args = { 35halo_args = {
36 'stream': sys.stderr, 36 'stream': stderr,
37 'enabled': sys.stderr.isatty(), 37 'enabled': stderr.isatty(),
38 'spinner': 'arc' 38 'spinner': 'arc'
39} 39}
40 40
41def read_repo(path): 41def read_repo(path):
42 with Halo(text=f'Listing {path}', **halo_args) as sp: 42 with Halo(text=f'Listing {path}', **halo_args) as sp:
43 res = None 43 res = None
44 with subprocess.Popen(['borg', 'list', '--info', '--lock-wait', '120', '--json', path], stdout=subprocess.PIPE) as proc: 44 with subprocess.Popen(['borg', 'list', '--debug', '--lock-wait', '120', '--json', path], stdout=subprocess.PIPE) as proc:
45 res = json.load(proc.stdout)['archives'] 45 res = json.load(proc.stdout)['archives']
46 sp.succeed(f'{len(res)} archives in {path}') 46 if sp.enabled:
47 sp.succeed(f'{len(res)} archives in {path}')
48 else:
49 print(f'{len(res)} archives in {path}', file=stderr)
47 return res 50 return res
48 51
49class ToSync: 52class ToSync:
@@ -73,7 +76,10 @@ def copy_archive(src_repo_path, dst_repo_path, entry):
73 repo_id = json.load(proc.stdout)['repository']['id'] 76 repo_id = json.load(proc.stdout)['repository']['id']
74 if repo_id: 77 if repo_id:
75 cache_suffix = f'{repo_id}_{match.group(1)}' 78 cache_suffix = f'{repo_id}_{match.group(1)}'
76 sp.succeed(f'Will process {entry["name"]} ({dateutil.parser.isoparse(entry["start"])}, cache_suffix={cache_suffix})') 79 if sp.enabled:
80 sp.succeed(f'Will process {entry["name"]} ({dateutil.parser.isoparse(entry["start"])}, cache_suffix={cache_suffix})')
81 else:
82 print(f'Will process {entry["name"]} ({dateutil.parser.isoparse(entry["start"])}, cache_suffix={cache_suffix})', file=stderr)
77 with TemporaryDirectory(prefix=f'borg-mount_{entry["name"]}_') as tmpdir: 83 with TemporaryDirectory(prefix=f'borg-mount_{entry["name"]}_') as tmpdir:
78 child = os.fork() 84 child = os.fork()
79 if child == 0: 85 if child == 0:
@@ -105,7 +111,10 @@ def copy_archive(src_repo_path, dst_repo_path, entry):
105 stats = json.load(proc.stdout)['archives'][0]['stats'] 111 stats = json.load(proc.stdout)['archives'][0]['stats']
106 total_size = stats['original_size'] 112 total_size = stats['original_size']
107 total_files = stats['nfiles'] 113 total_files = stats['nfiles']
108 sp.succeed(f'{total_files} files, {naturalsize(total_size, binary=True)}') 114 if sp.enabled:
115 sp.succeed(f'{total_files} files, {naturalsize(total_size, binary=True)}')
116 else:
117 print(f'{total_files} files, {naturalsize(total_size, binary=True)}', file=stderr)
109 # print(f'Mounting to {dir}', file=stderr) 118 # print(f'Mounting to {dir}', file=stderr)
110 with subprocess.Popen(['borg', 'mount', '--foreground', '--progress', '--lock-wait', '120', f'{src_repo_path}::{entry["name"]}', dir]) as mount_proc: 119 with subprocess.Popen(['borg', 'mount', '--foreground', '--progress', '--lock-wait', '120', f'{src_repo_path}::{entry["name"]}', dir]) as mount_proc:
111 with Halo(text='Waiting for mount', **halo_args) as sp: 120 with Halo(text='Waiting for mount', **halo_args) as sp:
@@ -117,7 +126,10 @@ def copy_archive(src_repo_path, dst_repo_path, entry):
117 elif datetime.now() - wait_start > timedelta(minutes=10): 126 elif datetime.now() - wait_start > timedelta(minutes=10):
118 ret.check_returncode() 127 ret.check_returncode()
119 sleep(0.1) 128 sleep(0.1)
120 sp.succeed('Mounted') 129 if sp.enabled:
130 sp.succeed('Mounted')
131 else:
132 print('Mounted', file=stderr)
121 while True: 133 while True:
122 try: 134 try:
123 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: 135 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: