From 79900212ae6c7f2c61119784d4d54613953cbf9e Mon Sep 17 00:00:00 2001 From: Rizqi Date: Tue, 23 Jun 2026 01:41:24 +0700 Subject: [PATCH] refactor: optimize zstd compression with multi-threading, reduced compression level, and automatic cleanup of original files --- backup_core.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/backup_core.py b/backup_core.py index ef8b9dc..12e9426 100644 --- a/backup_core.py +++ b/backup_core.py @@ -506,7 +506,8 @@ def verify_backup_checksums(dest_dir): def maybe_compress(path): try: import subprocess - rc = subprocess.run(['zstd', '-19', path], check=False) + # Use level 3 (default), multi-threaded compression (threads=0), and delete original file on success (--rm) + rc = subprocess.run(['zstd', '-3', '--threads=0', '--rm', path], check=False) if rc.returncode == 0: return path + '.zst' except FileNotFoundError: @@ -515,11 +516,18 @@ def maybe_compress(path): import zstandard as zstd out_path = path + '.zst' with open(path, 'rb') as ifh, open(out_path, 'wb') as ofh: - cctx = zstd.ZstdCompressor(level=19) + # Use level 3, multi-threaded compression (threads=0 uses all cores) + cctx = zstd.ZstdCompressor(level=3, threads=0) cctx.copy_stream(ifh, ofh) + # Delete original file on success to save local storage space + try: + os.remove(path) + print(f"Removed original file after compression: {path}") + except Exception as e: + print(f"Warning: Failed to remove original file {path} after compression: {e}") return out_path - except Exception: - print('Compression not available; skipping') + except Exception as e: + print(f'Compression not available; skipping: {e}') return path