Update readme files to reflect changes

This commit is contained in:
Craig
2025-04-23 16:21:53 +01:00
parent db117456dc
commit 7bd4927b3c
3 changed files with 55 additions and 27 deletions

View File

@@ -16,6 +16,32 @@ def tar_filter(filters, tarinfo):
def should_filter_file(filters, file_path):
return any([filter_name in str(file_path) for filter_name in filters])
def create_zip_archive(archive_path: Path, inputs: list, ignore_patterns: list):
with zipfile.ZipFile(archive_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for file_or_dir in inputs:
file_or_dir = Path(file_or_dir).expanduser().resolve()
print(f"Compressing: {file_or_dir}")
if file_or_dir.is_file():
if not should_filter_file(ignore_patterns, file_or_dir):
zipf.write(file_or_dir, arcname=file_or_dir.name)
else:
for root, dirs, files in os.walk(file_or_dir):
root_path = Path(root)
for file in files:
file_path = root_path / file
if not should_filter_file(ignore_patterns, file_path):
relative_path = file_path.relative_to(file_or_dir.parent)
zipf.write(file_path, arcname=str(relative_path))
def create_tar_archive(archive_path: Path, inputs: list, ignore_patterns: list):
filter_function = partial(tar_filter, ignore_patterns)
with tarfile.open(archive_path, "w:xz") as f:
for file_or_dir in inputs:
file_or_dir = Path(file_or_dir).expanduser().resolve()
print(f"Compressing: {file_or_dir}")
f.add(file_or_dir, filter=filter_function)
def local_backup(output_dir: Path, backup_name: str, inputs: list, ignore_patterns: list, archive_type="tar"):
current_time = datetime.datetime.now().isoformat()
start = time.time()
@@ -23,31 +49,11 @@ def local_backup(output_dir: Path, backup_name: str, inputs: list, ignore_patter
if archive_type == "zip":
archive_path = output_dir / f"{backup_name}-{current_time}.zip"
print(archive_path)
with zipfile.ZipFile(archive_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for file_or_dir in inputs:
file_or_dir = Path(file_or_dir).expanduser().resolve()
print(f"Compressing: {file_or_dir}")
if file_or_dir.is_file():
if not should_filter_file(ignore_patterns, file_or_dir):
zipf.write(file_or_dir, arcname=file_or_dir.name)
else:
for root, dirs, files in os.walk(file_or_dir):
root_path = Path(root)
for file in files:
file_path = root_path / file
if not should_filter_file(ignore_patterns, file_path):
relative_path = file_path.relative_to(file_or_dir.parent)
zipf.write(file_path, arcname=str(relative_path))
create_zip_archive(archive_path, inputs, ignore_patterns)
else:
archive_path = output_dir / f"{backup_name}-{current_time}.tar.gz"
filter_function = partial(tar_filter, ignore_patterns)
print(archive_path)
with tarfile.open(archive_path, "w:xz") as f:
for file_or_dir in inputs:
file_or_dir = Path(file_or_dir).expanduser().resolve()
print(f"Compressing: {file_or_dir}")
f.add(file_or_dir, filter=filter_function)
create_tar_archive(archive_path, inputs, ignore_patterns)
print(f"Total time: {(time.time() - start) / 60}mins")