SecureTar

2026.4.1 · active · verified Thu Apr 16

SecureTar is a Python library designed for handling encrypted tarfile backups. It acts as a streaming wrapper around Python's standard `tarfile` module, providing robust encryption capabilities. The library is actively maintained with frequent updates, often introducing significant breaking changes related to its file format and API, notably driven by advancements in cryptographic standards and its use in projects like Home Assistant.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create an encrypted tar archive using `SecureTarFile` and add a file to it using `atomic_contents_add`. It then shows how to open and extract content from the created secure archive, requiring the correct password.

import os
from pathlib import Path
from securetar import SecureTarFile, atomic_contents_add

# Create a dummy file for backup
dummy_file_path = Path("my_data.txt")
dummy_file_path.write_text("This is some sensitive data.")

# Define backup path and a password
backup_file_path = Path("my_secure_backup.tar")
backup_password = os.environ.get('SECURETAR_PASSWORD', 'a_very_secret_password_123')

print(f"Creating secure backup to {backup_file_path}...")

# Create a secure tar archive
with SecureTarFile(backup_file_path, 'w', password=backup_password) as tar_file:
    atomic_contents_add(tar_file, dummy_file_path, arcname=dummy_file_path.name)

print(f"Backup created: {backup_file_path}")

# Verify by attempting to read it
print(f"Attempting to read from {backup_file_path}...")
try:
    with SecureTarFile(backup_file_path, 'r', password=backup_password) as tar_file:
        members = tar_file.getnames()
        print(f"Contents of backup: {members}")
        # Example of extracting a file
        extracted_path = Path("extracted_data.txt")
        tar_file.extract(dummy_file_path.name, path=".")
        print(f"Extracted '{dummy_file_path.name}' to '{extracted_path}'")
        extracted_path.unlink() # Clean up
except Exception as e:
    print(f"Error reading backup: {e}")
finally:
    dummy_file_path.unlink() # Clean up original dummy file
    backup_file_path.unlink() # Clean up backup file

view raw JSON →