Automated ZFS Snapshot Management Using a Python CLI Tool

Managing snapshots on ZFS storage appliances can be time-consuming when done manually through a web console or SSH. When you’re handling multiple filesystems across several storage arrays, the repetitive nature of creating, listing, and removing snapshots becomes a bottleneck in daily operations. This is where automation becomes essential.
I built a Python-based CLI tool that interacts directly with ZFS appliance REST APIs, allowing storage engineers to manage snapshots programmatically from any machine with network access. This tool eliminates manual console work and enables integration with backup scripts, monitoring systems, and automated workflows.

The Problem This Tool Solves

Storage administrators face several recurring challenges:

Manual snapshot management is inefficient. Logging into storage appliances, navigating web interfaces, and performing repetitive snapshot operations wastes valuable time, especially when managing multiple filesystems.

Backup verification requires coordination. Teams often need to verify whether specific snapshots exist before running migration or backup tools. Without automation, this becomes a manual checklist item prone to human error.

Audit trails need standardization. Export controls and compliance requirements often demand documented proof of snapshot inventories, but manually compiling this data is tedious.

Key Benefits

  • Control ZFS snapshots from any machine with network access
  • Automatically generates CSV and JSON reports of snapshot inventories
  • Enables automation for backup and verification tasks
  • Integrates seamlessly into larger maintenance scripts or cron jobs

Real-World Use Cases

🤖 Automated backup scheduling

Integrate with cron jobs to create daily or hourly snapshots before backup windows, ensuring consistent recovery points.

🔍 Pre-migration snapshot verification

Before running data migration tools like NetApp XCP, verify that required snapshots exist or automatically create them if missing.

📈 Storage capacity reporting

Generate snapshot inventory reports in CSV format for capacity planning meetings or to identify stale snapshots consuming unnecessary space.

⚙️ Development environment management

Developers can quickly create snapshots before risky code deployments, then roll back if needed without involving storage admins.

Key Components Explained

Snapshot Creation Logic

				
					def newsnap(storage, pool, project, filesys, snap_name):
    response_data = {}
    headers = get_headers()
    base_url = f'https://{storage}:215'
    
    payload = {
        "name": snap_name,
        "retention": "unlocked"
    }
    json_dump = json.dumps(payload)
    url = f'{base_url}/api/storage/v1/pools/{pool}/projects/{project}/filesystems/{filesys}/snapshots'
    
    resp = requests.post(url=url, data=json_dump, verify=False, headers=headers, timeout=30)
				
			

The function constructs a JSON payload with the snapshot name and retention policy, then sends a POST request to the appliance. If the API returns HTTP 201 (created) or 409 (already exists), the tool prints True and exports the current snapshot inventory. This design ensures idempotency—running the same create command multiple times won’t fail or corrupt data.

XCP Snapshot Discovery

One specialized feature is XCP snapshot detection. NetApp’s XCP migration tool creates snapshots with specific naming conventions. The find_xcp() function checks whether a particular XCP snapshot exists and creates it automatically if missing:

				
					def find_xcp(storage, pool, project, filesys, snap_name):
    headers = get_headers()
    base_url = f'https://{storage}:215'
    url = f'{base_url}/api/storage/v1/pools/{pool}/projects/{project}/filesystems/{filesys}/snapshots'
    resp = requests.get(url=url, verify=False, headers=headers, timeout=30)
    snapshots = resp.json().get("snapshots", [])
    
    for snapshot in snapshots:
        if snapshot.get('name') == snap_name:
            print(bool(1))
            break
    else:
        print("Creating Snapshot ....")
        newsnap(storage, pool, project, filesys, snap_name)
				
			

This function searches the snapshot list for an exact name match. If found, it returns True. If not found, it automatically triggers snapshot creation—a convenient workflow for pre-migration checks.

Get the Script: Access the GitHub Repository

  • Full source code with extensive comments
  • Detailed installation instructions
  • Advanced usage scenarios
  • Documentation on extending the script for your specific needs
Feel free to fork the repository, submit issues or pull requests, or reach out with your questions and feedback. I’m continuously improving this tool based on real-world use cases and community input.

Getting Started

1. Prerequisites

				
					# Required Python packages
pip install requests docopt
				
			

2. Installation

Clone or download the project directory structure:

				
					snapshotool/ 
    ├── tool.py 
    └── utils/ 
        ├── parser.py 
        └── utils.py
				
			

Before running commands, update the authentication credentials in parser.py:

				
					headers = {
    "X-Auth-User": 'root',
    "X-Auth-Key": 'your_password_here'
}
				
			

Usage Examples

Create a snapshot:

				
					python tool.py -s storage01.company.com -fs production_data -sp backup_20241117 --create
				
			

List all snapshots for a filesystem:

				
					python tool.py -s storage01.company.com -fs production_data --list
				
			

Remove a specific snapshot:

				
					python tool.py -s storage01.company.com -fs production_data -sp old_snapshot --remove
				
			

Check for any XCP-prefixed snapshots:

				
					python tool.py -s storage01.company.com -fs migration_data --xcpfind
				
			

Verify or create a specific XCP snapshot:

				
					python tool.py -s storage01.company.com -fs migration_data -sp xcp_baseline --xcp
				
			

Output Files

After each operation, the tool generates two files in your current directory:

  • datafile.csv – A comma-separated table of snapshot details (name, creation time, space used)
  • datafile.json – The complete JSON response from the storage API for programmatic parsing

These exports serve as audit trails and can be imported into spreadsheets or monitoring dashboards.

Extending the Tool

The modular design makes it straightforward to add new features. You could extend it to:

  • Accept configuration files for commonly used storage appliances
  • Add retention policy management beyond the default “unlocked” setting
  • Implement bulk operations across multiple filesystems
  • Schedule snapshot creation using Python’s schedule library
  • Send notifications to Slack or email after operations complete

Wrapping Up

This ZFS snapshot manager transforms manual storage administration into programmable infrastructure. By wrapping the ZFS REST API in a clean CLI, it enables storage engineers to automate backup workflows, integrate with migration tools, and generate compliance reports—all without ever opening a browser console.

The tool showcases practical Python development skills including REST API integration, command-line interface design, data serialization, and error handling. It solves a real operational pain point while remaining simple enough to modify and extend for specific organizational needs.

Whether you’re managing a handful of development filesystems or coordinating enterprise backup strategies across dozens of appliances, having snapshot management as code rather than clickops makes your infrastructure more reliable and your workday more productive.