A Python script to synchronize .env files between local GitHub repositories and a central backup directory.
The tool supports bidirectional synchronization, conflict detection, and safe backups without deleting any files.
- Purpose
- Features
- Installation
- Usage
- Argument Structure
- Examples
- How It Works
- Sync Modes
- File Naming Strategy
- Conflict Handling
- File Detection
- Testing
- Project Structure
- Workflow Examples
- Sync Report
- Ignored Directories
- Safety Features
- Platform Support
- Troubleshooting
- Security Note
- License
Keep all .env configuration files from multiple GitHub repositories synchronized in a single backup location.
This enables:
- Centralized backups
- Easy restoration on new machines
- Safe bidirectional synchronization between repositories and backup
- Bidirectional synchronization (
two-way) - One-way synchronization (
repo-to-backup,backup-to-repo) - Content-based change detection using file hashes
- Conflict detection with non-destructive conflict files
- Automatic backup organization by repository
- JSON report generated after each run
- Smart directory filtering
- Comprehensive
.envfile detection
- Ensure Python 3.6 or higher is installed
- Clone or download the
sync_env.pyscript - No external dependencies are required (standard library only)
# Bidirectional synchronization (default)
python sync_env.py /path/to/repositories
python sync_env.py /path/to/repositories /path/to/backup
python sync_env.py /path/to/repositories /path/to/backup two-way
# One-way synchronization: repositories -> backup
python sync_env.py /path/to/repositories /path/to/backup repo-to-backup
# One-way synchronization: backup -> repositories
python sync_env.py /path/to/repositories /path/to/backup backup-to-repopython sync_env.py <repos_folder> [backup_folder] [mode]-
repos_folder (required) Path to the directory containing GitHub repositories.
-
backup_folder (optional) Path to the backup directory. Default:
~/.env_backups -
mode (optional) Synchronization mode:
-
two-way(default) -
repo-to-backup -
backup-to-repo
-
python sync_env.py C:\Users\username\projects
python sync_env.py C:\Users\username\projects D:\backups\env two-waypython sync_env.py /home/username/projects
python sync_env.py /home/username/projects /backup/env-files repo-to-backuprepositories/
├── frontend-app/
│ ├── .env
│ └── .env.production
├── backend-api/
│ ├── .env
│ └── config/
│ └── database.env
└── mobile-app/
└── .env~/.env_backups/
├── frontend-app/
│ ├── .env
│ ├── env_frontend-app_root.txt
│ └── frontend-app_.env.production
├── backend-api/
│ ├── .env
│ ├── env_backend-api_root.txt
│ └── backend-api_database.env
├── mobile-app/
│ ├── .env
│ └── env_mobile-app_root.txt
└── sync_report.jsonSynchronizes changes in both directions. If both versions differ, a conflict file is created.
Copies .env files from repositories to the backup directory only.
Restores .env files from the backup directory into repositories.
Repository File Backup File(s) Created Description
.env .env, env_<repo>_root.txt Original + safe descriptive copy
.env.local <repo>_.env.local Prefixed to avoid conflicts
config.env <repo>_config.env Prefixed to avoid conflicts
database.env <repo>_database.env Prefixed to avoid conflicts
- When the same file exists in both locations and contents differ:
.env
.env.conflict_20240204T174505
-
No file is overwritten
-
Both versions are preserved
-
Conflicts must be resolved manually
The script detects the following patterns:
-
.env -
.env.*(e.g. .env.local, .env.production) -
*.env(e.g. config.env, database.env) -
Files containing
.envand ending with.env
python run_tests.pypython run_tests.py --module test_unit
python run_tests.py --module test_integrationpython run_tests.py --test tests.test_unit.TestHashFunctions
python run_tests.py --test tests.test_unit.TestHashFunctions.test_calculate_file_hash_normaldot-env-handler/
├── sync_env.py
├── run_tests.py
├── README.md
├── tests/
│ ├── __init__.py
│ ├── helpers.py
│ ├── test_unit.py
│ └── test_integration.py
└── .gitignore- Initial Backup
python sync_env.py ~/projects ~/backups/env-files repo-to-backup- Regular Synchronization
python sync_env.py ~/projects ~/backups/env-files two-way- Restore on New Machine
python sync_env.py ~/new-projects ~/backups/env-files backup-to-repoAfter each run, a JSON report is generated:
{
"timestamp": "2024-02-04T17:45:05",
"repos_folder": "/path/to/repositories",
"backup_folder": "/path/to/backup",
"mode": "two-way",
"repos_processed": 3,
"files_copied_to_backup": 2,
"files_copied_to_repos": 1,
"conflicts": [],
"repositories": []
}The following directories are ignored automatically:
-
.git -
node_modules -
__pycache__ -
.venv, venv, env -
.idea, .vscode -
dist, build, target -
logs
-
No file deletion
-
Hash-based change detection
-
Conflict files instead of overwriting
-
Dual backup copies for root
.envfiles -
Graceful handling of permission errors
-
Windows
-
Linux
-
macOS
-
Verify repository paths
-
Check for nested
.envfiles -
Ensure read permissions
-
Verify write access to backup directory
-
Run the script with appropriate permissions
-
Review and resolve conflicts manually
-
Remove resolved conflict files
-
Consider switching to one-way sync when appropriate
.env files often contain sensitive data.
-
Store backups in secure locations
-
Do not commit backups to version control
-
Consider encrypting the backup directory
-
Use restrictive file permissions
Provided as-is for personal and educational use. Modify and adapt as needed.