sysrsync: Python wrapper for rsync

1.1.1 · active · verified Thu Apr 16

sysrsync is a simple and safe Python wrapper designed to execute the system's `rsync` command-line utility. It provides a programmatic interface to construct `rsync` commands, helping to prevent common shell injection vulnerabilities and simplify complex `rsync` operations. The current version is 1.1.1, and it maintains an active but infrequent release cadence, primarily for bug fixes and minor enhancements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to perform a local file synchronization using `sysrsync.run`. It uses the `source` and `destination` arguments, along with standard `rsync` options. The `sync=True` argument ensures the function waits for the `rsync` process to complete and returns a `subprocess.CompletedProcess` object. It also includes basic error handling for `sysrsync.exceptions.SysrsyncError`.

import sysrsync
import os

# Create dummy files for demonstration
if not os.path.exists("my_source_folder"): os.makedirs("my_source_folder")
with open("my_source_folder/file1.txt", "w") as f: f.write("Hello")
with open("my_source_folder/file2.txt", "w") as f: f.write("World")

# Example: Local to Local sync (replace with your actual paths)
# This requires rsync to be installed on your system.
# In a real scenario, 'destination_ssh_user' and 'destination_ssh_host' would be used for remote sync.
print("Attempting local sync...")
try:
    result = sysrsync.run(
        source="my_source_folder/", # Trailing slash means copy contents of folder
        destination="./my_destination_folder/",
        options=["-a", "--delete", "-v"],
        sync=True # Wait for completion
    )
    print(f"Rsync completed with exit code: {result.returncode}")
    print(f"STDOUT:\n{result.stdout.decode()}")
    if result.stderr: print(f"STDERR:\n{result.stderr.decode()}")
except sysrsync.exceptions.SysrsyncError as e:
    print(f"Rsync failed: {e}")
    print(f"STDOUT:\n{e.result.stdout.decode()}")
    if e.result.stderr: print(f"STDERR:\n{e.result.stderr.decode()}")

# Clean up dummy files
import shutil
if os.path.exists("my_source_folder"): shutil.rmtree("my_source_folder")
if os.path.exists("my_destination_folder"): shutil.rmtree("my_destination_folder")

view raw JSON →