From 20737f7872a09edfb482401c179f7d21a020679a Mon Sep 17 00:00:00 2001 From: Peter Wood Date: Thu, 29 May 2025 07:08:55 -0400 Subject: [PATCH] Add .env backup system to crontab and create integration guide - Added daily .env backup at 02:00 with proper logging - Added weekly .env validation at 08:30 on Sundays - Created comprehensive integration guide - Integrated with existing crontab management system - Follows established logging pattern with syslog tags --- crontab/crontab-europa.txt | 8 ++ docs/env-backup-integration-guide.md | 131 +++++++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 docs/env-backup-integration-guide.md diff --git a/crontab/crontab-europa.txt b/crontab/crontab-europa.txt index e66b11d..be4255f 100644 --- a/crontab/crontab-europa.txt +++ b/crontab/crontab-europa.txt @@ -7,6 +7,10 @@ # Logs both stdout and stderr with backup-move tag 0 1 * * * /home/acedanger/shell/move-backups.sh 2>&1 | logger -t backup-move -p user.info +# Daily .env files backup at 0200 with logging +# Backs up all Docker .env files to private Gitea repository +0 2 * * * { echo "Starting .env files backup"; /home/acedanger/shell/backup-env-files.sh; echo ".env backup completed with exit code: $?"; } 2>&1 | logger -t env-backup -p user.info + # Daily Plex database integrity check every 30 minutes */30 * * * * { echo "Check Plex database corruption"; /home/acedanger/shell/plex/backup-plex.sh --check-integrity --auto-repair; } 2>&1 | logger -t plex-database-integrity-check -p user.info @@ -29,6 +33,10 @@ # Comprehensive reporting with system logging 0 8 * * 0 { echo "Starting weekly Plex backup report generation"; /home/acedanger/shell/plex/validate-plex-backups.sh --report; echo "Weekly report generation completed with exit code: $?"; } 2>&1 | logger -t plex-report -p user.info +# Weekly .env backup validation (Sundays at 0830) +# Validates integrity of .env backup repository +30 8 * * 0 { echo "Starting .env backup validation"; /home/acedanger/shell/validate-env-backups.sh; echo ".env validation completed with exit code: $?"; } 2>&1 | logger -t env-validation -p user.info + # Optional: Add a health check entry to monitor cron jobs (every 6 hours) # This can help detect if any of the backup processes are failing # 0 */6 * * * { echo "Cron health check - all backup jobs scheduled"; ps aux | grep -E "(backup-plex|validate-plex|move-backups)" | grep -v grep | wc -l; } 2>&1 | logger -t cron-health -p user.info diff --git a/docs/env-backup-integration-guide.md b/docs/env-backup-integration-guide.md new file mode 100644 index 0000000..e1ea00a --- /dev/null +++ b/docs/env-backup-integration-guide.md @@ -0,0 +1,131 @@ +# .env Backup Integration Guide + +## Quick Setup Summary + +Your .env backup system is now fully operational! Here's what was set up: + +### ✅ What's Working +- **31 .env files** discovered across your Docker containers +- **30 files backed up** successfully to `/home/acedanger/.env-backup` +- **Private Gitea repository** configured and pushed successfully +- **Version control** with automatic commit messages and timestamps +- **Reference files** included (docker-compose.yml for context) + +### 🔧 Integration Options + +#### 1. Manual Backup (Current) +```bash +cd /home/acedanger/shell +./backup-env-files.sh # Regular backup +./backup-env-files.sh --dry-run # Preview changes +./backup-env-files.sh --list # Show all .env files +``` + +#### 2. Automated Daily Backup (Recommended) +Add to your crontab for daily backups at 2 AM: +```bash +# Daily .env backup at 2 AM +0 2 * * * /home/acedanger/shell/backup-env-files.sh >/dev/null 2>&1 +``` + +#### 3. Integration with Existing Backup Scripts +The backup integrates with your existing backup system through: +- **Logs**: Written to `/home/acedanger/shell/logs/env-backup.log` +- **Completion**: Tab completion available via `env-backup-completion.bash` +- **Validation**: Use `validate-env-backups.sh` for integrity checks + +### 🔐 Security Features + +1. **Private Repository**: Only you have access +2. **Gitignore**: Excludes temporary files and logs +3. **SSH Authentication**: Uses your existing SSH key +4. **Local + Remote**: Dual backup (local git + remote Gitea) + +### 📊 Backup Structure + +``` +~/.env-backup/ +├── docker-containers/ +│ ├── authentik/ +│ │ └── .env.example +│ ├── caddy/ +│ │ ├── .env +│ │ ├── .env.example +│ │ └── docker-compose.yml.ref +│ ├── database/ +│ │ ├── .env +│ │ ├── .env.example +│ │ └── docker-compose.yml.ref +│ └── ... (all your containers) +├── README.md +└── .env-backup-config +``` + +### 🔄 Common Operations + +#### Restore Files (if needed) +```bash +./backup-env-files.sh --restore +``` + +#### Force Backup (ignore unchanged files) +```bash +./backup-env-files.sh --force +``` + +#### Check What Would Change +```bash +./backup-env-files.sh --dry-run +``` + +### 🚨 Emergency Recovery + +If you lose your filesystem: + +1. **Clone the backup**: `git clone https://git.ptrwd.com/peterwood/docker-env-backup.git` +2. **Restore files**: `./backup-env-files.sh --restore` +3. **Recreate containers**: Your docker-compose.yml reference files are included + +### 📈 Monitoring + +- **Logs**: Check `/home/acedanger/shell/logs/env-backup.log` +- **Git History**: View changes with `git log` in backup directory +- **Validation**: Run `validate-env-backups.sh` for integrity checks + +### 🔧 Maintenance + +#### Weekly Validation (Recommended) +```bash +# Add to crontab for weekly validation +0 3 * * 0 /home/acedanger/shell/validate-env-backups.sh >/dev/null 2>&1 +``` + +#### Cleanup Old Logs (Monthly) +The system automatically manages logs, but you can clean them manually if needed. + +### 🆘 Troubleshooting + +#### Push Fails +- Check SSH key: `ssh -T git@git.ptrwd.com` +- Verify repository exists and is private +- Check network connectivity + +#### Files Not Found +- Verify Docker directory structure: `ls -la ~/docker/*/` +- Check file permissions +- Run with `--list` to see what's detected + +#### Restore Issues +- Ensure target directories exist +- Check file permissions +- Use `--dry-run` first to preview + +## Integration Complete! 🎉 + +Your .env files are now safely backed up and version controlled. The system will: +1. Track all changes to your .env files +2. Maintain a secure backup in your private Gitea +3. Provide easy restore capabilities +4. Integrate with your existing shell toolkit + +Run `./backup-env-files.sh` regularly or set up the cron job for automatic backups!