How Backups Work
Backups are created automatically in the following scenarios:- Before running the
synccommand (unless--no-backupis specified) - Before restoring from a previous backup (when
--create-rollbackis used)
Backups are never created when using the default command (copying .env to .env.example), as this operation doesn’t modify your existing .env files.
Backup Storage Location
All backups are stored in a dedicated directory:0o700 on Unix-based systems) to protect sensitive environment data.
Directory Structure
Backup Naming Convention
Backup files follow a specific naming pattern:- original-filename: The name of the backed-up file (e.g.,
.env,.env.local) - timestamp: A timestamp in
YYYYMMDD-HHMMSSformat
Timestamp Format
The timestamp format isYYYYMMDD-HHMMSS:
YYYY: 4-digit year (e.g., 2026)MM: 2-digit month (01-12)DD: 2-digit day (01-31)HH: 2-digit hour in 24-hour format (00-23)MM: 2-digit minute (00-59)SS: 2-digit second (00-59)
20260304-143022 represents March 4, 2026 at 2:30:22 PM
Listing Backups
You can view all available backups using therestore command with the --list flag:
- Valid backups with timestamps and file lists
- Invalid or corrupted backups with error details
- Usage examples for restoration
Example Output
Backup Validation
env-twin automatically validates backups before restoration to ensure integrity:Cleaning Old Backups
To prevent backups from consuming too much disk space, you can clean up old backups while keeping the most recent ones:Customizing Retention
Specify how many backups to keep:Skip Confirmation
Use the--yes flag to skip the confirmation prompt:
Example Cleanup Session
Backup Security
env-twin takes several security measures to protect your backup files:File Permissions
Backup files are created with restrictive permissions:- Backup directory:
0o700(owner read/write/execute only) - Backup files:
0o600(owner read/write only)
Git Exclusion
The.env-twin/ directory is automatically added to .gitignore to prevent committing sensitive data:
Path Traversal Protection
During restoration, env-twin validates all file paths to prevent path traversal attacks. Files outside the project directory are rejected.Symlink Protection
Before restoring files, env-twin checks for and removes symlinks to prevent arbitrary file overwrites.Troubleshooting Backup Issues
Backup directory not found
Backup directory not found
Problem: The
.env-twin/ directory doesn’t exist.Solution: The directory is created automatically when you first create a backup. If you deleted it, run env-twin sync to create new backups.Permission denied when creating backups
Permission denied when creating backups
Problem: Cannot write to the
.env-twin/ directory.Solution: Check directory permissions:Backup validation fails
Backup validation fails
Problem: Backups show as invalid when listing.Solution: This usually indicates file corruption or modification. Try:
- Check if backup files are readable:
ls -la .env-twin/ - Restore from an older backup
- If needed, manually inspect files in
.env-twin/
Running out of disk space
Running out of disk space
Problem: Too many backups consuming disk space.Solution: Clean old backups regularly:
Best Practices
Related Commands
- Restoring Backups - Learn how to restore from backups
- Configuration - Configure backup behavior with flags
- Troubleshooting - Common backup-related issues