Skip to main content
The restore command restores your .env* files from backups created by the sync command. It features automatic backup discovery, validation, rollback capability, and comprehensive error handling.

Usage

env-twin restore [timestamp] [options]

Key Features

Auto-Discovery

Automatically finds and selects the most recent valid backup when no timestamp is provided

Backup Validation

Validates backup integrity and completeness before restoration

Rollback Protection

Creates pre-restore snapshots for automatic recovery if restoration fails

Cross-Platform

Works seamlessly on Windows, macOS, and Linux

Arguments

timestamp
string
Specific backup timestamp to restore (format: YYYYMMDD-HHMMSS)If omitted, the most recent valid backup will be automatically selected.Example:
env-twin restore 20241125-143022

Options

--yes
flag
Skip confirmation prompt and proceed with restoration immediatelyAliases: -yExample:
env-twin restore --yes
--list
flag
List all available backups without performing a restoreDisplays:
  • Valid backups with timestamps and file lists
  • Invalid backups with error messages
  • Usage examples for restoration
Example:
env-twin restore --list
--preserve-permissions
flag
Preserve original file permissions during restorationWhen enabled, files are restored with the same permissions they had when backed up.Example:
env-twin restore --preserve-permissions
--preserve-timestamps
flag
Preserve original file modification timestampsWhen enabled, restored files maintain their original modification times from the backup.Example:
env-twin restore --preserve-timestamps
--create-rollback
flag
Create a pre-restore snapshot for rollback capabilityAliases: --rollbackIf the restore operation fails, the snapshot can be used to automatically rollback to the pre-restore state.Example:
env-twin restore --create-rollback
--force
flag
Force restore without checking for file changesAliases: -fBypasses safety checks that normally warn about existing file modifications.
This may overwrite uncommitted changes. Use with caution!
Example:
env-twin restore --force
--dry-run
flag
Show what would be restored without making any actual changesAliases: --simulatePerfect for previewing the restore operation before committing to it.Example:
env-twin restore --dry-run
--verbose
flag
Enable verbose logging for detailed operation informationAliases: -VProvides detailed progress information, file-by-file status, and debugging information.Example:
env-twin restore --verbose
--help
flag
Display help information for the restore commandAliases: -hExample:
env-twin restore --help

Examples

The simplest way to restore - automatically selects the most recent valid backup:
env-twin restore
Output:
🔄 Starting enhanced restore process...

📁 Discovering available backups...
🎯 Auto-selected most recent backup: 2024-11-25 14:30:22 (2 hours ago)

🔍 Inspecting selected backup...
   ⚠️  .env exists (last modified: 2024-11-25T16:15:30Z)
   ⚠️  .env.local exists (last modified: 2024-11-25T16:15:30Z)
   ✅ .env.example will be created

📋 Restore Operation Summary:
   Backup: 2024-11-25 14:30:22 (2 hours ago)
   Files to restore: .env, .env.local, .env.example

❓ Do you want to proceed with the restore? (y/N)
Answer: y

🚀 Starting restore operation...

   33% - Restoring: .env
   66% - Restoring: .env.local
   100% - Restoring: .env.example

📊 Restore Results:
   ✅ Successfully restored: 3 files

🎉 Restore operation completed successfully!
Restore a specific backup by providing its timestamp:
env-twin restore 20241125-143022
Output:
🔄 Starting enhanced restore process...

📁 Discovering available backups...
🎯 Selected specific backup: 2024-11-25 14:30:22 (2 hours ago)

🔍 Inspecting selected backup...
   ⚠️  .env exists (last modified: 2024-11-25T16:15:30Z)
   ✅ .env.local will be created

📋 Restore Operation Summary:
   Backup: 2024-11-25 14:30:22 (2 hours ago)
   Files to restore: .env, .env.local

❓ Do you want to proceed with the restore? (y/N)
View all available backups without restoring:
env-twin restore --list
Output:
📋 Available backups:

✅ Valid backups:
   1. 2024-11-25 16:30:22 (30 minutes ago)
      Files: .env, .env.local, .env.example
      Created: 2024-11-25T16:30:22.000Z

   2. 2024-11-25 14:30:22 (2 hours ago)
      Files: .env, .env.local, .env.example
      Created: 2024-11-25T14:30:22.000Z

   3. 2024-11-24 09:15:00 (1 day ago)
      Files: .env, .env.local
      Created: 2024-11-24T09:15:00.000Z

💡 Usage examples:
   env-twin restore                    # Restore most recent (20241125-163022)
   env-twin restore 20241125-163022    # Restore specific backup
   env-twin restore --list             # List all backups
Skip the confirmation prompt using --yes:
env-twin restore --yes
or with a specific timestamp:
env-twin restore 20241125-143022 --yes
This is useful for:
  • Automated scripts
  • CI/CD pipelines
  • Quick emergency restores
Preview what would be restored without making changes:
env-twin restore --dry-run
Output:
🔄 Starting enhanced restore process...

📁 Discovering available backups...
🎯 Auto-selected most recent backup: 2024-11-25 14:30:22 (2 hours ago)

🔍 Inspecting selected backup...
   ⚠️  .env exists (last modified: 2024-11-25T16:15:30Z)
   ⚠️  .env.local exists (last modified: 2024-11-25T16:15:30Z)

📋 Restore Operation Summary:
   Backup: 2024-11-25 14:30:22 (2 hours ago)
   Files to restore: .env, .env.local
   Mode: DRY RUN (no actual changes will be made)

❓ Do you want to proceed with the restore? (y/N)
Answer: y

🚀 Starting restore operation...

   50% - [DRY RUN] Would restore: .env
   100% - [DRY RUN] Would restore: .env.local

📊 Restore Results:
   ✅ Would restore: 2 files

🎉 Dry run completed successfully!
Create a pre-restore snapshot for automatic recovery:
env-twin restore --create-rollback
If the restore fails, the snapshot will be automatically used to rollback:
📸 Creating pre-restore snapshot...
   ✅ Snapshot created: restore-1732546822000-a1b2c3d4e5f6g7h8

🚀 Starting restore operation...

   50% - Restoring: .env
   ❌ Failed to restore .env: Permission denied

🔄 Restore failed, initiating rollback...
✅ Rollback completed successfully

❌ Restore operation completed with errors
Combine multiple options for maximum control:
env-twin restore --create-rollback --preserve-permissions --preserve-timestamps --verbose
This will:
  1. Create a rollback snapshot before restoring
  2. Preserve original file permissions
  3. Preserve original file timestamps
  4. Show detailed logging throughout the process
Force restoration even if files have been modified:
env-twin restore --force
This will overwrite any existing files without checking if they’ve been modified since the backup. Use with caution!

Restore Process Flow

The restore command follows a comprehensive process:

Backup Validation

Before restoration, env-twin validates each backup:
  • Structure Check: Verifies backup directory structure
  • Timestamp Format: Validates timestamp format (YYYYMMDD-HHMMSS)
  • File Existence: Confirms all backup files exist
  • File Integrity: Checks files are readable and not corrupted
  • Metadata Validation: Ensures backup metadata is complete
Invalid backups are excluded from automatic selection but are shown in --list output with error details.

Rollback Capability

When using --create-rollback, env-twin creates a snapshot before restoration:
  1. Pre-Restore Snapshot: Captures current state of all files
  2. Metadata Storage: Stores file permissions, timestamps, and content
  3. Automatic Recovery: If restore fails, automatically reverts to snapshot
  4. Manual Rollback: Snapshot can be manually restored if needed
Snapshot Limitations:
  • Maximum file size: 1MB per file
  • Files larger than 1MB are skipped with a warning
  • Snapshots are temporary and cleaned up after successful restore

Best Practices

Before restoring in production, preview the changes:
env-twin restore --dry-run
# Review the output
env-twin restore --yes  # Only if dry-run looks good
Enable rollback protection for critical environments:
env-twin restore --create-rollback --verbose
Check available backups before restoration:
env-twin restore --list
env-twin restore 20241125-143022
Keep original permissions and timestamps:
env-twin restore --preserve-permissions --preserve-timestamps

sync

Sync command that creates the backups

clean-backups

Manage and clean old backups

Troubleshooting

Problem: Command reports “No backups found in .env-twin/ directory”Solution: Run env-twin sync first to create a backup:
env-twin sync
env-twin restore --list
Problem: “Backup with timestamp ‘X’ not found or invalid”Solution: List available backups and use a valid timestamp:
env-twin restore --list
env-twin restore [valid-timestamp]
Problem: Cannot write to .env filesSolution: Check file permissions and ownership:
ls -la .env*
chmod 644 .env*
Or use --force to override (not recommended):
env-twin restore --force
Problem: Restore fails partway throughSolution: If you used --create-rollback, the rollback happens automatically. Otherwise, restore the previous backup:
env-twin restore --list  # Find the previous backup
env-twin restore [previous-timestamp]

Build docs developers (and LLMs) love