#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import shlex import shutil import socket from pathlib import Path from p1st.exec_capture import execute_capture from p1st.exec_print_capture import execute_print_capture from p1st.exec_produce_chunks import execute_produce_chunks from p1st.transfer_inform import transfer_inform def test(): # test1() # test2() # test4() # test8() # test9() # test10() # test11() # test12() test13() def test1(): _init(1) returncode, out, err = execute_capture(['ls', '-la']) print(f'stdout:\n{out}\nstderr:\n{err}') print() returncode, out, err = execute_capture(['ls', '/foo/bar']) print(f'stdout:\n{out}\nstderr:\n{err}') def test2(): _init(2) returncode, out, err = execute_print_capture(['ls', '-la']) print() returncode, out, err = execute_print_capture(['ls', '/foo/bar']) def test4(): _init(4) transfer_inform( ['ls', 'test/4-rsync-error'], # rsync src to dst ['ls', 'test/4-ssh-error'], # ssh target-pc 'echo "OK" | nc -U "/path/to/unix-socket"' Path('test/4-UNIX-socket') ) def _test5_chunk_transfer_fun(chunk_file: Path, chunk_number: int, eof: bool, concat_script: Path, remote_target_file: Path): rsync_cmd = [str(concat_script), str(chunk_file), str(remote_target_file)] inform_cmd = ['ls', chunk_file.parent.joinpath(f'5.remote.EOF={eof}'), ] transfer_inform( rsync_cmd=rsync_cmd, inform_cmd=inform_cmd, user_input_file=chunk_file.parent.joinpath(f'5.SOCKET'), ) def test8(): repo_name = 'subprocess_util' child_name = 'test-subvolume' child_dir = '/mnt/backup/test-dir' child_path = f'{child_dir}/{child_name}' target_dir = '/mnt/data/test-dir' target_path = f'{target_dir}/{child_name}' ssh_target = 'rootnas' print(f'=== In one shell, connect with "ssh nas" ===') print(f'\tsudo mkdir {target_dir}') print(f'\tcd {repo_name} && make && sudo make clean && cd ..') print() print(f'\tsudo btrfs-receive-chunks {target_path}') print() print(f'=== In another shell, connect with "ssh odroid" ===') print(f'\tsudo mkdir {child_dir}') print(f'\tsudo btrfs subvolume create {child_path}.writeable') print(f'\techo foo | sudo tee {child_path}.writeable/bar') print(f'\tsudo btrfs subvolume snapshot -r {child_path}.writeable {child_path}') print(f'\tcd {repo_name} && make && sudo make clean && cd ..') print() print(f'\tsudo btrfs-send-chunks {child_path} {ssh_target} {target_path}') def test9(): child_path = '/mnt/backup/snap/blogger.privacy1st.de/20230104T2255' ssh_target = 'rootnas' target_path = '/mnt/data/test/blogger.privacy1st.de/20230104T2255' print(f'sudo btrfs-receive-chunks {target_path}') print(f'sudo btrfs-send-chunks {child_path} {ssh_target} {target_path}') def test10(): _init(10) source_file = Path('src/p1st/transfer_inform.py') chunk_dir = Path('test') target_file = Path('test/transfer_inform.py') def get_chunk_path(chunk_no: int): return chunk_dir.joinpath(f'{source_file.name}.CHUNK.{chunk_no}') def handle_chunk(chunk_no: int, last_chunk: bool): chunk_path = get_chunk_path(chunk_no) print(f'Handling chunk {chunk_path}') # Read chunk. chunk = chunk_path.read_bytes() # Append chunk to target. with target_file.open("ab") as f: f.write(chunk) # Delete chunk. chunk_path.unlink(missing_ok=False) execute_produce_chunks( command=['cat', str(source_file)], get_chunk_path=get_chunk_path, handle_chunk=handle_chunk, chunk_size=512, ) def test11(): repo_name = 'subprocess_util' child_name = 'test-subvolume' child_dir = '/mnt/backup/test-dir' child_path = f'{child_dir}/{child_name}' target_dir = '/mnt/data/test-dir' target_path = f'{target_dir}/{child_name}' ssh_target = 'rootnas' print(f'=== In one shell, connect with "ssh nas" ===') print(f'\tsudo mkdir {target_dir}') print(f'\tcd {repo_name} && make && sudo make clean && cd ..') print() print(f'\tsudo btrfs-receive-chunks-v2 {target_path}') print() print(f'=== In another shell, connect with "ssh odroid" ===') print(f'\tsudo mkdir {child_dir}') print(f'\tsudo btrfs subvolume create {child_path}.writeable') print(f'\techo foo | sudo tee {child_path}.writeable/bar') print(f'\tsudo btrfs subvolume snapshot -r {child_path}.writeable {child_path}') print(f'\tcd {repo_name} && make && sudo make clean && cd ..') print() print(f'\tsudo btrfs-send-chunks-v2 {child_path} {ssh_target} {target_path}') def test12(): child_path = '/mnt/backup/snap/blogger.privacy1st.de/20230104T2255' ssh_target = 'rootnas' target_path = '/mnt/data/test/blogger.privacy1st.de/20230104T2255' target_chunk_path = '/mnt/data/test/chunks' print(f'nas: sudo btrfs-receive-chunks-v2 --chunk-dir={target_chunk_path} {target_path}') print( f'odroid: sudo btrfs-send-chunks-v2 --chunk-dir=/mnt/backup/test/chunks --target-chunk-dir={target_chunk_path} {child_path} {ssh_target} {target_path}') def test13(): ssh_source = 'rootnas' source_path = '/mnt/data/snap/blogger.privacy1st.de/20230104T1622_u' source_chunk_dir = '/mnt/data/test/chunks' target_path = '/mnt/backup/test/blogger.privacy1st.de/20230104T1622_u' print(f'nas: sudo btrfs-send-chunks-v3 --chunk-dir={source_chunk_dir} {source_path}') print(f'odroid: sudo btrfs-receive-chunks-v3 {ssh_source} {source_chunk_dir} {target_path}') def _init(test_number: int): print(f"TEST {test_number}") test_dir = Path('test') if test_dir.exists(): shutil.rmtree('test') Path('test').mkdir(exist_ok=False) if __name__ == '__main__': test()