Rollback System
The env-twin rollback system provides automatic recovery capabilities when restoration operations fail. It creates snapshots of your current state before making changes, allowing you to safely revert if something goes wrong.Overview
The rollback system is built on theRollbackManager class (rollback-manager.ts:49), which handles:
- Snapshot creation - Capturing current file state before restoration
- Rollback triggers - Automatic reversion when errors occur
- Snapshot management - Maintaining a history of up to 10 snapshots
- Cross-platform support - Works on Windows, macOS, and Linux
How Snapshots Work
Snapshot Structure
Each rollback snapshot contains:File Information Captured
For each file in the snapshot (rollback-manager.ts:22-30):- File existence - Whether the file existed at snapshot time
- File content - Full content (if file is ≤1MB)
- File permissions - Unix-style permissions (on Linux/macOS)
- File stats - Size, modification time, and other metadata
Snapshots are stored in
.env-twin/rollbacks/ with secure permissions (0o700 for directories, 0o600 for files).Creating Snapshots
Automatic Pre-Restore Snapshots
When you runrestore with the --create-rollback flag, env-twin automatically creates a snapshot before making any changes:
includeContent: true- Stores full file content for reliable restorationincludePermissions: true- Preserves Unix file permissionsmaxSize: 1048576- Maximum file size of 1MB per filecompress: false- Content stored uncompressed for speed
Manual Snapshot Creation
The rollback system can be used programmatically:Rollback Process
When Rollback is Triggered
Automatic rollback occurs when (file-restoration.ts:138-147):- Restoration fails during file writing
- File validation fails
- Permission errors prevent file updates
- Any error occurs during the restore process
How Rollback Works
The rollback process (rollback-manager.ts:212-302):- Loads snapshot metadata from
.env-twin/rollbacks/{snapshotId}/metadata.json - Validates snapshot - Ensures all required files exist
- Restores each file:
- If file existed: Restore content and permissions from snapshot
- If file didn’t exist: Remove any new files created
- Reports results - List of successfully rolled back files
View Rollback Implementation
View Rollback Implementation
Snapshot Management
Listing Snapshots
View all available rollback snapshots:Automatic Cleanup
The system maintains a maximum of 10 snapshots by default (rollback-manager.ts:54). When this limit is exceeded, the oldest snapshots are automatically deleted (rollback-manager.ts:394-410):Manual Snapshot Deletion
Security Features
Path Traversal Prevention
All file paths are validated to prevent directory traversal attacks (rollback-manager.ts:64-68):Secure Permissions
All rollback files are created with restrictive permissions:- Directories:
0o700(rwx------) - Only owner can access - Files:
0o600(rw-------) - Only owner can read/write
Advanced Usage
Snapshot Validation
Check if a snapshot is still valid (files haven’t changed since creation):- File existence matches snapshot
- File size hasn’t changed
- Modification time within 1 second tolerance
Snapshot Size Information
Get storage information for a snapshot:Atomic File Operations
TheRollbackUtils class provides atomic file replacement with rollback capability:
Best Practices
Always use
--create-rollback in production environments to ensure you can recover from failed restorations.-
Enable rollback for critical operations:
-
Monitor snapshot storage:
- Each snapshot can use significant disk space
- Default limit of 10 snapshots prevents excessive storage use
- Large files (>1MB) are tracked but not fully stored
-
Validate before rollback:
-
Test rollback in development:
- Create test snapshots
- Verify rollback works correctly
- Understand timing and performance implications
Troubleshooting
Snapshot Creation Fails
Issue: “Failed to create rollback snapshot” Solutions:- Check disk space in
.env-twin/rollbacks/ - Verify write permissions on working directory
- Ensure files are smaller than 1MB (or increase
maxSize)
Rollback Not Available
Issue: Rollback option not working Solutions:Snapshot Validation Fails
Issue: Snapshot marked as invalid Cause: Files have changed since snapshot creation Solution: Create a new snapshot if you want to capture current state:Related Topics
- File Preservation - How permissions and timestamps are preserved
- Restore Command - Using the restore command with rollback
- CI/CD Integration - Rollback strategies for automated environments