Vikas Nale 0bd733ecb8 - Handle GZIP in database backup process, so that all the backups are zipped before uploaded to s3
- Add fag to check if emails should be send on process complete like did for GIT and S3
2025-07-05 11:23:27 +05:30

31 lines
824 B
Python

import gzip
import shutil
import os
def gzip_file(source_file, delete_original=False):
"""
Compress any file using gzip.
Args:
source_file (str): Full path to the file to compress.
delete_original (bool): Whether to delete the original file after compression.
Returns:
str: Path to the gzipped file.
"""
if not os.path.isfile(source_file):
raise FileNotFoundError(f"File not found: {source_file}")
gzipped_file = source_file + '.gz'
with open(source_file, 'rb') as f_in:
with gzip.open(gzipped_file, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
if delete_original:
os.remove(source_file)
print(f"Original file deleted: {source_file}")
print(f"Gzipped file created: {gzipped_file}")
return gzipped_file