import tarfile import shutil import os import glob def extract_tar_gz(source_file, destination_dir, extract_path): # Ensure destination directory exists os.makedirs(destination_dir, exist_ok=True) # Extract files from tar.gz file with tarfile.open(source_file, "r:gz") as tar: for member in tar.getmembers(): if member.name.startswith(extract_path): tar.extract(member, destination_dir) def main(): # Source file pattern source_pattern = "/mnt/server_backup/restore/*.tar.gz" # Destination directory destination_dir = "/home/ccalifice/" # Path inside the tarfile to extract extract_path = "home/ccalifce/" # Get list of tar.gz files tar_files = glob.glob(source_pattern) if not tar_files: print("No matching tar.gz files found.") return # Copy and extract each tar.gz file for source_file in tar_files: try: extract_tar_gz(source_file, destination_dir, extract_path) print(f"Extraction successful for {source_file}.") except Exception as e: print(f"Error extracting {source_file}: {e}") if __name__ == "__main__": main()