.env.backup.production
Use environment variables to define the backup location.
| Feature | .env.example | .env.backup.production | | :--- | :--- | :--- | | | No (uses DB_PASSWORD=changeme ) | Yes (contains actual database password) | | Can be committed to git | Yes (safe) | Never (unsafe unless encrypted) | | Restores a live system | No (requires manual entry of secrets) | Yes (one command restore) | | Backup rotation needed | No | Yes |
By implementing immutable, rotated, off-server backups of your environment configuration, you transform a potential 4-hour firefight into a 30-second recovery. You give your team the confidence to deploy on Friday afternoons. You build a culture of resilience over heroics. .env.backup.production
But a team with a strict backup protocol does the following:
Do not check .env.backup.production into a public repository. If you must store it in Git, use git-crypt or SOPS (Secrets OPerationS) to encrypt it. Manual backups fail. You will forget. Automation is the only reliable path. Use environment variables to define the backup location
#!/bin/bash # /usr/local/bin/backup-env.sh TIMESTAMP=$(date +%Y%m%d_%H%M%S) BACKUP_DIR="/var/backups/env" SOURCE_ENV="/var/www/app/.env.production" cp "$SOURCE_ENV" "$BACKUP_DIR/.env.backup.production.$TIMESTAMP" Maintain the generic '.env.backup.production' symlink ln -sf "$BACKUP_DIR/.env.backup.production.$TIMESTAMP" "/var/www/app/.env.backup.production" Retention: keep only last 30 backups find "$BACKUP_DIR" -name ".env.backup.production.*" -mtime +30 -delete
Before creating a backup, run a validation script that tests all critical connections (database, redis, external APIs). Only create the backup if validation passes. Pitfall 2: Hardcoding the backup path Developers often hardcode /var/www/app/.env.backup.production . When you migrate to containers or different servers, the path breaks. You build a culture of resilience over heroics
# Validate syntax (for Node.js apps) node -e "require('dotenv').config(); console.log('DB_HOST:', process.env.DB_HOST)" curl http://localhost/health Reload the process manager pm2 reload app # or: systemctl reload app Common Pitfalls and How to Avoid Them Even experienced engineers mishandle .env.backup.production . Here are three frequent mistakes. Pitfall 1: Backing up invalid state If your production environment is already misconfigured (e.g., an expired API key), your backup will be equally broken.