From c9b69ea78917ed305a6020e1505d7a7de8b689fd Mon Sep 17 00:00:00 2001 From: Peter Wood Date: Mon, 26 May 2025 13:20:12 -0400 Subject: [PATCH] Add comprehensive Plex backup management scripts - Introduced `restore-plex.sh` for restoring Plex backups with logging and validation. - Created `test-plex-backup.sh` for automated testing of backup functionalities. - Developed `validate-plex-backups.sh` for validating backup integrity and monitoring. - Updated `update.sh` to reference the correct path for Plex service management. --- .gitignore | 10 +- README.md | 7 +- backup-log-monitor.sh | 2 +- crontab/README.md | 376 ++++++++++++++++++ .../crontab-backup-system.sh | 0 .../europa/current-crontab.backup | 2 +- .../io/current-crontab.backup | 2 +- .../racknerd/current-crontab.backup | 0 .../crontab-europa.txt | 2 +- crontab-io.txt => crontab/crontab-io.txt | 0 .../crontab-merging-issue-resolution.md | 0 .../crontab-racknerd.txt | 0 {docs => crontab}/enhanced-crontab-system.md | 0 .../fix-crontab-merging.sh | 0 .../manage-enhanced-crontab.sh | 2 +- .../multi-system-crontab-management.md | 2 +- dotfiles/my-aliases.zsh | 12 +- dotfiles/my-aliases.zsh.original | 8 +- plex/README.md | 294 ++++++++++++++ backup-plex.sh => plex/backup-plex.sh | 0 .../integration-test-plex.sh | 0 .../monitor-plex-backup.sh | 0 {docs => plex}/plex-backup.md | 4 +- {docs => plex}/plex-management.md | 0 .../plex-recent-additions.sh | 0 plex.sh => plex/plex.sh | 0 restore-plex.sh => plex/restore-plex.sh | 0 .../test-plex-backup.sh | 0 .../validate-plex-backups.sh | 0 update.sh | 4 +- 30 files changed, 702 insertions(+), 25 deletions(-) create mode 100644 crontab/README.md rename crontab-backup-system.sh => crontab/crontab-backup-system.sh (100%) rename {crontab-backups => crontab/crontab-backups}/europa/current-crontab.backup (86%) rename {crontab-backups => crontab/crontab-backups}/io/current-crontab.backup (72%) rename {crontab-backups => crontab/crontab-backups}/racknerd/current-crontab.backup (100%) rename crontab-europa.txt => crontab/crontab-europa.txt (93%) rename crontab-io.txt => crontab/crontab-io.txt (100%) rename {docs => crontab}/crontab-merging-issue-resolution.md (100%) rename crontab-racknerd.txt => crontab/crontab-racknerd.txt (100%) rename {docs => crontab}/enhanced-crontab-system.md (100%) rename fix-crontab-merging.sh => crontab/fix-crontab-merging.sh (100%) rename manage-enhanced-crontab.sh => crontab/manage-enhanced-crontab.sh (99%) rename {docs => crontab}/multi-system-crontab-management.md (98%) create mode 100644 plex/README.md rename backup-plex.sh => plex/backup-plex.sh (100%) rename integration-test-plex.sh => plex/integration-test-plex.sh (100%) rename monitor-plex-backup.sh => plex/monitor-plex-backup.sh (100%) rename {docs => plex}/plex-backup.md (98%) rename {docs => plex}/plex-management.md (100%) rename plex-recent-additions.sh => plex/plex-recent-additions.sh (100%) rename plex.sh => plex/plex.sh (100%) rename restore-plex.sh => plex/restore-plex.sh (100%) rename test-plex-backup.sh => plex/test-plex-backup.sh (100%) rename validate-plex-backups.sh => plex/validate-plex-backups.sh (100%) diff --git a/.gitignore b/.gitignore index 35dd196..53bd944 100644 --- a/.gitignore +++ b/.gitignore @@ -20,9 +20,13 @@ _book # Runtime generated files logs/ -crontab-backups/ -# But keep the current state backups for each system -!crontab-backups/*/current-crontab.backup +# Backup files - ignore most backups but keep current state files +crontab/crontab-backups/* +!crontab/crontab-backups/europa/ +!crontab/crontab-backups/io/ +!crontab/crontab-backups/racknerd/ +crontab/crontab-backups/*/archive/ +!crontab/crontab-backups/*/current-crontab.backup # Temporary files *.tmp diff --git a/README.md b/README.md index cb728ad..7e6d5c3 100644 --- a/README.md +++ b/README.md @@ -241,15 +241,18 @@ For comprehensive disaster recovery, sync backups to remote locations: ## Documentation +### Directory-Specific Documentation + +- **[Plex Scripts Documentation](./plex/README.md)**: Comprehensive documentation for all Plex backup, restoration, validation, and management scripts. +- **[Crontab Management Documentation](./crontab/README.md)**: Complete guide for crontab management, backup systems, and multi-system administration. + ### Backup System Documentation - [Enhanced Media Backup Documentation](./docs/enhanced-media-backup.md): Comprehensive guide for the enhanced `backup-media.sh` script with enterprise features. - [Media Backup Enhancement Summary](./docs/backup-media-enhancement-summary.md): Summary of enhancements and feature comparisons. -- [Plex Backup Script Documentation](./docs/plex-backup.md): Detailed documentation for the `backup-plex.sh` script. ### Script Documentation -- [Plex Management Script Documentation](./docs/plex-management.md): Detailed documentation for the `plex.sh` script. - [Folder Metrics Script Documentation](./docs/folder-metrics.md): Detailed documentation for the `folder-metrics.sh` script. - [Testing Framework Documentation](./docs/testing.md): Detailed documentation for the Docker-based testing system. diff --git a/backup-log-monitor.sh b/backup-log-monitor.sh index 17dfcc2..3bb59ad 100755 --- a/backup-log-monitor.sh +++ b/backup-log-monitor.sh @@ -207,7 +207,7 @@ check_backup_health() { "/home/acedanger/shell/backup-plex.sh" "/home/acedanger/shell/move-backups.sh" "/home/acedanger/shell/validate-plex-backups.sh" - "/home/acedanger/shell/crontab-backup-system.sh" + "/home/acedanger/shell/crontab/crontab-backup-system.sh" ) for script in "${backup_scripts[@]}"; do diff --git a/crontab/README.md b/crontab/README.md new file mode 100644 index 0000000..7d8bb52 --- /dev/null +++ b/crontab/README.md @@ -0,0 +1,376 @@ +# Crontab Management and Backup Scripts + +This directory contains all scripts and documentation related to crontab management, backup, monitoring, and multi-system administration. + +## Scripts Overview + +### Core Management Scripts + +#### `crontab-backup-system.sh` + +**Comprehensive crontab backup and recovery system** + +- **Multi-system support** with hostname-based organization +- **Automated backup creation** with timestamped archives +- **Syntax validation** and integrity checking +- **Cross-system comparison** and restoration +- **Legacy migration** from old backup structures +- **Automated cleanup** with configurable retention + +**Usage:** + +```bash +./crontab-backup-system.sh backup manual # Create manual backup +./crontab-backup-system.sh list # List backups for current system +./crontab-backup-system.sh list io # List backups for specific system +./crontab-backup-system.sh status all # View all systems status +./crontab-backup-system.sh restore backup-file # Restore from backup +./crontab-backup-system.sh compare current old.backup # Compare configurations +``` + +#### `manage-enhanced-crontab.sh` + +**Enhanced crontab installation and management** + +- **System-specific crontab installation** with validation +- **Pre/post-install backups** for safety +- **Script verification** and executable checking +- **Logrotate setup** for log management +- **Integration** with backup and monitoring systems + +**Usage:** + +```bash +./manage-enhanced-crontab.sh install # Install enhanced crontab system +./manage-enhanced-crontab.sh verify # Verify all scripts exist +./manage-enhanced-crontab.sh status # Check system health +./manage-enhanced-crontab.sh backup # Backup current crontab only +``` + +#### `fix-crontab-merging.sh` + +**Crontab merge conflict resolution** + +- **Conflict detection** and resolution +- **Safe merging** of crontab entries +- **Backup preservation** during merge operations +- **Interactive conflict resolution** + +### System-Specific Configuration Files + +#### `crontab-europa.txt` + +Production crontab configuration for the Europa system: + +```bash +# Move docker backups (01:00) +0 1 * * * /home/acedanger/shell/move-backups.sh 2>&1 | logger -t backup-move -p user.info + +# Plex backup (04:15) +15 4 * * * /home/acedanger/shell/plex/backup-plex.sh 2>&1 | logger -t plex-backup -p user.info + +# Validate plex backups (07:00) +0 7 * * * /home/acedanger/shell/validate-plex-backups.sh --fix 2>&1 | logger -t plex-validation -p user.info +``` + +#### `crontab-io.txt` + +Crontab configuration for the IO system with media-focused operations. + +#### `crontab-racknerd.txt` + +Crontab configuration for the RackNerd system with backup synchronization. + +#### `crontab.txt.bak` + +Backup of legacy crontab configuration for reference. + +## Directory Structure + +``` +crontab-backups/ +├── europa/ # Current system (example) +│ ├── current-crontab.backup +│ └── archive/ +│ ├── europa-crontab-initial-20250526_101354.backup +│ └── europa-crontab-pre-install-20250526_100622.backup +├── io/ # Remote system backups +│ ├── current-crontab.backup +│ └── archive/ +│ └── io-crontab-sample-20250526_101558.backup +└── racknerd/ # Another remote system + ├── current-crontab.backup + └── archive/ + └── racknerd-crontab-sample-20250526_101558.backup +``` + +## Enhanced Features + +### Multi-System Support + +- **Hostname-based organization**: Each system gets its own directory +- **Cross-system operations**: View, compare, and restore backups from any system +- **Centralized management**: Manage all systems from a single location +- **Legacy migration**: Automatic migration from old backup structures + +### System Logging Integration + +All crontab operations integrate with system logging using specific tags: + +- `crontab-backup`: Crontab backup operations +- `plex-backup`: Plex database backup operations +- `backup-move`: Docker backup file transfers +- `plex-validation`: Backup integrity checks +- `backup-cleanup`: Cleanup operations + +### Automated Operations + +- **Daily automated backups** at midnight +- **Syntax validation** before installing changes +- **Automatic cleanup** of old backups (30-day retention) +- **Health monitoring** and status reporting +- **Error handling** with comprehensive logging + +## Configuration + +### Environment Variables + +Key configuration parameters in the backup system: + +```bash +# Retention settings +BACKUP_RETENTION_DAYS=30 # Keep backups for 30 days +AUTO_CLEANUP=true # Enable automatic cleanup + +# Directory settings +BACKUP_ROOT="./crontab-backups" +LOG_DIR="./logs" + +# System identification +HOSTNAME=$(hostname) # Automatic system identification +``` + +### File Naming Convention + +Backup files follow the pattern: `{hostname}-crontab-{type}-{timestamp}.backup` + +Examples: + +- `europa-crontab-manual-20250526_101354.backup` +- `io-crontab-pre-upgrade-20250526_120000.backup` +- `racknerd-crontab-auto-20250526_000001.backup` + +## Automation and Scheduling + +### Automated Backup Setup + +```bash +# Setup automated daily backups +./crontab-backup-system.sh setup-auto +``` + +This adds a daily backup entry to the system crontab: + +```bash +0 0 * * * /path/to/crontab-backup-system.sh backup auto --auto-cleanup 2>&1 | logger -t crontab-backup -p user.info +``` + +### Cross-System Synchronization + +For distributed environments, set up backup synchronization: + +```bash +# Sync backups from remote systems +rsync -avz europa:/home/acedanger/shell/crontab-backups/europa/ ./crontab-backups/europa/ +rsync -avz io:/home/acedanger/shell/crontab-backups/io/ ./crontab-backups/io/ +``` + +## Monitoring and Health Checks + +### System Status Monitoring + +```bash +# View status for current system +./crontab-backup-system.sh status + +# View status for specific system +./crontab-backup-system.sh status io + +# View status for all systems +./crontab-backup-system.sh status all +``` + +### Log Monitoring + +```bash +# View all backup-related logs +journalctl -t crontab-backup -t plex-backup -t backup-move -f + +# View logs for specific operation +journalctl -t plex-backup --since "1 hour ago" + +# Check for errors in the last 24 hours +journalctl --since '24 hours ago' --priority=err -t plex-backup +``` + +### Health Check Commands + +```bash +# Check backup system health +./crontab-backup-system.sh status all + +# Verify all scripts exist and are executable +./manage-enhanced-crontab.sh verify + +# View recent backup activity +grep "SUCCESS" logs/crontab-management.log | tail -20 +``` + +## Troubleshooting + +### Common Issues + +#### Permission Errors + +Ensure proper file permissions on backup directories: + +```bash +chmod 755 crontab-backups/ +chmod 644 crontab-backups/*/*.backup +``` + +#### Missing Backups + +Check automated backup cron entries: + +```bash +sudo crontab -l | grep crontab-backup +``` + +#### Syntax Errors + +Validate crontab syntax before applying: + +```bash +./crontab-backup-system.sh validate new-crontab.txt +``` + +### Debug Mode + +Enable verbose logging for troubleshooting: + +```bash +./crontab-backup-system.sh status all 2>&1 | tee debug.log +``` + +### Recovery Procedures + +#### Emergency Crontab Restoration + +```bash +# List available backups +./crontab-backup-system.sh list + +# Compare current with backup +./crontab-backup-system.sh compare current backup-file.backup + +# Restore from specific backup +./crontab-backup-system.sh restore backup-file.backup +``` + +#### Disaster Recovery + +```bash +# Restore from specific system backup during emergency +./crontab-backup-system.sh restore io-crontab-pre-incident-20250526_101354.backup + +# Compare pre-incident and post-incident configurations +./crontab-backup-system.sh compare io-crontab-pre-incident-20250526_101354.backup current +``` + +## Advanced Use Cases + +### Configuration Management + +```bash +# Standardize crontab across multiple systems +./crontab-backup-system.sh compare europa-crontab-standard-20250526_101354.backup io-crontab-current-20250526_120000.backup + +# Validate configurations before deployment +./crontab-backup-system.sh validate new-crontab-config.txt +``` + +### Compliance and Auditing + +- **Change tracking**: Complete history of all crontab changes across systems +- **Audit trails**: System logs provide comprehensive audit information +- **Compliance reporting**: Generate reports showing backup frequency and success rates + +### Cross-System Comparison + +```bash +# Compare current crontab with backup from another system +./crontab-backup-system.sh compare current io-crontab-sample-20250526_101558.backup + +# Compare two backups from different systems +./crontab-backup-system.sh compare europa-crontab-manual-20250526_101354.backup racknerd-crontab-sample-20250526_101558.backup +``` + +## Security Considerations + +### File Permissions + +- **Backup directories**: Restrict access to authorized users only +- **Log files**: Ensure proper log rotation and access controls +- **Remote access**: Use secure methods (SSH, rsync) for cross-system operations + +### Backup Integrity + +- **Validation**: Regular syntax validation of backup files +- **Checksums**: Consider adding checksum verification for critical backups +- **Retention**: Implement appropriate backup retention policies + +### Access Control + +- Scripts require appropriate sudo permissions for crontab operations +- Backup locations should have restricted access +- Log files contain operational data only (no sensitive information) + +## Documentation + +### Detailed Documentation + +- **[enhanced-crontab-system.md](./enhanced-crontab-system.md)**: Comprehensive crontab system documentation +- **[multi-system-crontab-management.md](./multi-system-crontab-management.md)**: Multi-system management guide +- **[crontab-merging-issue-resolution.md](./crontab-merging-issue-resolution.md)**: Conflict resolution procedures + +### Integration Notes + +- All scripts follow repository coding standards +- Consistent logging and error handling +- Color-coded output for readability +- Comprehensive help systems +- System logging integration + +## Best Practices + +1. **Regular Testing**: Periodically test backup restoration procedures +2. **Documentation**: Keep records of system configurations and backup schedules +3. **Automation**: Use automated cleanup to prevent disk space issues +4. **Monitoring**: Implement comprehensive monitoring and alerting +5. **Security**: Regularly review and update access controls + +## Migration Notes + +When migrating from legacy crontab systems: + +1. **Backup current configuration**: Create manual backup before changes +2. **Test new system**: Use validation and dry-run features +3. **Gradual migration**: Migrate one system at a time +4. **Monitor performance**: Check logs for any issues post-migration + +The system automatically detects and migrates legacy backup structures while preserving all existing data. + +--- + +*This crontab management system provides robust, scalable backup management for distributed environments while maintaining simplicity and reliability.* diff --git a/crontab-backup-system.sh b/crontab/crontab-backup-system.sh similarity index 100% rename from crontab-backup-system.sh rename to crontab/crontab-backup-system.sh diff --git a/crontab-backups/europa/current-crontab.backup b/crontab/crontab-backups/europa/current-crontab.backup similarity index 86% rename from crontab-backups/europa/current-crontab.backup rename to crontab/crontab-backups/europa/current-crontab.backup index 2fd2558..31d80d6 100644 --- a/crontab-backups/europa/current-crontab.backup +++ b/crontab/crontab-backups/europa/current-crontab.backup @@ -1,5 +1,5 @@ 0 1 * * * /home/acedanger/shell/move-backups.sh 2>&1 | logger -t backup-move -p user.info -15 4 * * * { echo "Starting Plex backup"; /home/acedanger/shell/backup-plex.sh --non-interactive --auto-repair; echo "Plex backup completed with exit code: $?"; } 2>&1 | logger -t plex-backup -p user.info +15 4 * * * { echo "Starting Plex backup"; /home/acedanger/shell/plex/backup-plex.sh --non-interactive --auto-repair; echo "Plex backup completed with exit code: $?"; } 2>&1 | logger -t plex-backup -p user.info 0 7 * * * { echo "Starting Plex backup validation"; /home/acedanger/shell/validate-plex-backups.sh --fix; echo "Validation completed with exit code: $?"; } 2>&1 | logger -t plex-validation -p user.info 0 5 * * 1 { echo "Starting Immich database backup move"; if mv /mnt/share/media/immich/uploads/backups/immich-db-backup* /mnt/share/media/backups/immich 2>/dev/null; then echo "Immich backup move completed successfully"; else echo "No Immich backup files found or move failed"; fi; } 2>&1 | logger -t immich-backup -p user.info 0 8 * * 0 { echo "Starting weekly Plex backup report generation"; /home/acedanger/shell/validate-plex-backups.sh --report; echo "Weekly report generation completed with exit code: $?"; } 2>&1 | logger -t plex-report -p user.info diff --git a/crontab-backups/io/current-crontab.backup b/crontab/crontab-backups/io/current-crontab.backup similarity index 72% rename from crontab-backups/io/current-crontab.backup rename to crontab/crontab-backups/io/current-crontab.backup index 77e6f18..69de5e7 100644 --- a/crontab-backups/io/current-crontab.backup +++ b/crontab/crontab-backups/io/current-crontab.backup @@ -1,5 +1,5 @@ 0 2 * * * { echo "Starting Docker backup"; /home/acedanger/shell/backup-docker.sh; echo "Docker backup completed with exit code: $?"; } 2>&1 | logger -t docker-backup -p user.info -0 4 * * * /home/acedanger/shell/crontab-backup-system.sh backup auto --auto-cleanup 2>&1 | logger -t crontab-backup -p user.info +0 4 * * * /home/acedanger/shell/crontab/crontab-backup-system.sh backup auto --auto-cleanup 2>&1 | logger -t crontab-backup -p user.info # Daily backups at 2 AM 0 2 * * * /home/acedanger/shell/backup-media.sh >/dev/null 2>&1 # diff --git a/crontab-backups/racknerd/current-crontab.backup b/crontab/crontab-backups/racknerd/current-crontab.backup similarity index 100% rename from crontab-backups/racknerd/current-crontab.backup rename to crontab/crontab-backups/racknerd/current-crontab.backup diff --git a/crontab-europa.txt b/crontab/crontab-europa.txt similarity index 93% rename from crontab-europa.txt rename to crontab/crontab-europa.txt index 6c4baaa..a15a39a 100644 --- a/crontab-europa.txt +++ b/crontab/crontab-europa.txt @@ -9,7 +9,7 @@ # Daily Plex backup at 0415 with enhanced logging # Includes execution status and performance metrics -15 4 * * * { echo "Starting Plex backup"; /home/acedanger/shell/backup-plex.sh --non-interactive --auto-repair; echo "Plex backup completed with exit code: $?"; } 2>&1 | logger -t plex-backup -p user.info +15 4 * * * { echo "Starting Plex backup"; /home/acedanger/shell/plex/backup-plex.sh --non-interactive --auto-repair; echo "Plex backup completed with exit code: $?"; } 2>&1 | logger -t plex-backup -p user.info # Daily validation at 0700 with detailed logging # Logs validation results and any auto-fixes performed diff --git a/crontab-io.txt b/crontab/crontab-io.txt similarity index 100% rename from crontab-io.txt rename to crontab/crontab-io.txt diff --git a/docs/crontab-merging-issue-resolution.md b/crontab/crontab-merging-issue-resolution.md similarity index 100% rename from docs/crontab-merging-issue-resolution.md rename to crontab/crontab-merging-issue-resolution.md diff --git a/crontab-racknerd.txt b/crontab/crontab-racknerd.txt similarity index 100% rename from crontab-racknerd.txt rename to crontab/crontab-racknerd.txt diff --git a/docs/enhanced-crontab-system.md b/crontab/enhanced-crontab-system.md similarity index 100% rename from docs/enhanced-crontab-system.md rename to crontab/enhanced-crontab-system.md diff --git a/fix-crontab-merging.sh b/crontab/fix-crontab-merging.sh similarity index 100% rename from fix-crontab-merging.sh rename to crontab/fix-crontab-merging.sh diff --git a/manage-enhanced-crontab.sh b/crontab/manage-enhanced-crontab.sh similarity index 99% rename from manage-enhanced-crontab.sh rename to crontab/manage-enhanced-crontab.sh index 99be8e2..5b6968d 100755 --- a/manage-enhanced-crontab.sh +++ b/crontab/manage-enhanced-crontab.sh @@ -184,7 +184,7 @@ verify_scripts_exist() { local scripts=( "/home/acedanger/shell/move-backups.sh" - "/home/acedanger/shell/backup-plex.sh" + "/home/acedanger/shell/plex/backup-plex.sh" "/home/acedanger/shell/validate-plex-backups.sh" ) diff --git a/docs/multi-system-crontab-management.md b/crontab/multi-system-crontab-management.md similarity index 98% rename from docs/multi-system-crontab-management.md rename to crontab/multi-system-crontab-management.md index 1459c40..e118de8 100644 --- a/docs/multi-system-crontab-management.md +++ b/crontab/multi-system-crontab-management.md @@ -107,7 +107,7 @@ All backup operations now integrate with system logging: ```bash # Plex backup with comprehensive logging -15 4 * * * /home/acedanger/shell/backup-plex.sh 2>&1 | logger -t plex-backup -p user.info +15 4 * * * /home/acedanger/shell/plex/backup-plex.sh 2>&1 | logger -t plex-backup -p user.info # Backup move operation with error handling 0 1 * * * /home/acedanger/shell/move-backups.sh 2>&1 | logger -t backup-move -p user.info diff --git a/dotfiles/my-aliases.zsh b/dotfiles/my-aliases.zsh index ca367cd..b24b491 100644 --- a/dotfiles/my-aliases.zsh +++ b/dotfiles/my-aliases.zsh @@ -7,12 +7,12 @@ alias gcm="git commit -m" alias ll="ls -laFh --group-directories-first --color=auto" alias findzombie="ps -A -ostat,pid,ppid | grep -e '[zZ]'" # 🎬 Plex Media Server Management - Sexy Edition -alias plex="/home/acedanger/shell/plex.sh" -alias px="/home/acedanger/shell/plex.sh" # Quick shortcut -alias plex-start="/home/acedanger/shell/plex.sh start" # Start Plex -alias plex-stop="/home/acedanger/shell/plex.sh stop" # Stop Plex -alias plex-restart="/home/acedanger/shell/plex.sh restart" # Restart Plex -alias plex-status="/home/acedanger/shell/plex.sh status" # Status check +alias plex="/home/acedanger/shell/plex/plex.sh" +alias px="/home/acedanger/shell/plex/plex.sh" # Quick shortcut +alias plex-start="/home/acedanger/shell/plex/plex.sh start" # Start Plex +alias plex-stop="/home/acedanger/shell/plex/plex.sh stop" # Stop Plex +alias plex-restart="/home/acedanger/shell/plex/plex.sh restart" # Restart Plex +alias plex-status="/home/acedanger/shell/plex/plex.sh status" # Status check alias plex-web="xdg-open http://localhost:32400/web" # Open web UI in browser alias update="/home/acedanger/shell/update.sh" alias dcdn="docker compose down" diff --git a/dotfiles/my-aliases.zsh.original b/dotfiles/my-aliases.zsh.original index ca367cd..5c0df50 100644 --- a/dotfiles/my-aliases.zsh.original +++ b/dotfiles/my-aliases.zsh.original @@ -7,10 +7,10 @@ alias gcm="git commit -m" alias ll="ls -laFh --group-directories-first --color=auto" alias findzombie="ps -A -ostat,pid,ppid | grep -e '[zZ]'" # 🎬 Plex Media Server Management - Sexy Edition -alias plex="/home/acedanger/shell/plex.sh" -alias px="/home/acedanger/shell/plex.sh" # Quick shortcut -alias plex-start="/home/acedanger/shell/plex.sh start" # Start Plex -alias plex-stop="/home/acedanger/shell/plex.sh stop" # Stop Plex +alias plex="/home/acedanger/shell/plex/plex.sh" +alias px="/home/acedanger/shell/plex/plex.sh" # Quick shortcut +alias plex-start="/home/acedanger/shell/plex/plex.sh start" # Start Plex +alias plex-stop="/home/acedanger/shell/plex/plex.sh stop" # Stop Plex alias plex-restart="/home/acedanger/shell/plex.sh restart" # Restart Plex alias plex-status="/home/acedanger/shell/plex.sh status" # Status check alias plex-web="xdg-open http://localhost:32400/web" # Open web UI in browser diff --git a/plex/README.md b/plex/README.md new file mode 100644 index 0000000..a6d9939 --- /dev/null +++ b/plex/README.md @@ -0,0 +1,294 @@ +# Plex Backup and Management Scripts + +This directory contains all scripts and documentation related to Plex Media Server backup, restoration, validation, and management. + +## Scripts Overview + +### Core Backup Scripts + +#### `backup-plex.sh` + +**Enhanced Plex backup script with advanced features** + +- **Full backup operations** with integrity verification +- **Performance monitoring** with JSON-based logging +- **WAL file handling** for SQLite databases +- **Database integrity checks** with automated repair options +- **Parallel processing** for improved performance +- **Multi-channel notifications** (console, webhook, email) +- **Comprehensive logging** with color-coded output + +**Usage:** + +```bash +./backup-plex.sh # Standard backup +./backup-plex.sh --check-integrity # Integrity check only +./backup-plex.sh --non-interactive # Automated mode +./backup-plex.sh --auto-repair # Auto-repair database issues +``` + +#### `restore-plex.sh` + +**Safe restoration script with validation** + +- **Backup validation** before restoration +- **Dry-run mode** for testing +- **Current data backup** before restoration +- **Interactive backup selection** + +**Usage:** + +```bash +./restore-plex.sh # List available backups +./restore-plex.sh plex-backup-20250125_143022.tar.gz # Restore specific backup +./restore-plex.sh --dry-run backup-file.tar.gz # Test restoration +``` + +#### `validate-plex-backups.sh` + +**Backup validation and health monitoring** + +- **Archive integrity checking** +- **Backup freshness validation** +- **Comprehensive reporting** +- **Automated fix suggestions** + +**Usage:** + +```bash +./validate-plex-backups.sh # Validate all backups +./validate-plex-backups.sh --report # Generate detailed report +./validate-plex-backups.sh --fix # Auto-fix issues where possible +``` + +### Testing and Monitoring + +#### `test-plex-backup.sh` + +**Comprehensive testing framework** + +- **Unit tests** for core functionality +- **Integration tests** for full system testing +- **Performance benchmarks** + +**Usage:** + +```bash +./test-plex-backup.sh all # Run all tests +./test-plex-backup.sh unit # Unit tests only +./test-plex-backup.sh performance # Performance benchmarks +``` + +#### `integration-test-plex.sh` + +**Integration testing for Plex backup system** + +- **End-to-end testing** +- **System integration validation** +- **Environment compatibility checks** + +#### `monitor-plex-backup.sh` + +**Real-time backup monitoring** + +- **Live backup status** +- **Performance metrics** +- **Error detection and alerting** + +### Utility Scripts + +#### `plex.sh` + +**Plex Media Server service management** + +- **Service start/stop/restart** +- **Status monitoring** +- **Safe service management** + +#### `plex-recent-additions.sh` + +**Recent media additions reporting** + +- **New content detection** +- **Addition summaries** +- **Media library analytics** + +## Configuration + +### Environment Variables + +Key configuration parameters in `backup-plex.sh`: + +```bash +# Retention settings +MAX_BACKUP_AGE_DAYS=30 # Remove backups older than 30 days +MAX_BACKUPS_TO_KEEP=10 # Keep maximum of 10 backup archives + +# Directory settings +BACKUP_ROOT="/mnt/share/media/backups/plex" +LOG_ROOT="/mnt/share/media/backups/logs" + +# Feature toggles +PARALLEL_VERIFICATION=true # Enable parallel verification +PERFORMANCE_MONITORING=true # Track performance metrics +AUTO_REPAIR=false # Automatic database repair +``` + +### Backup Strategy + +The enhanced backup system implements: + +- **Archive-only structure**: Direct `.tar.gz` storage +- **Timestamp naming**: `plex-backup-YYYYMMDD_HHMMSS.tar.gz` +- **Automatic cleanup**: Age and count-based retention +- **Integrity validation**: Comprehensive archive verification + +## Directory Structure + +``` +/mnt/share/media/backups/plex/ +├── plex-backup-20250125_143022.tar.gz # Latest backup +├── plex-backup-20250124_143011.tar.gz # Previous backup +├── plex-backup-20250123_143008.tar.gz # Older backup +└── logs/ + ├── backup_log_20250125_143022.md + ├── plex-backup-performance.json + └── plex-backup.json +``` + +## Enhanced Features + +### Performance Monitoring + +- **JSON performance logs**: All operations timed and logged +- **Performance reports**: Automatic generation of metrics +- **Operation tracking**: Backup, verification, service management times + +### Database Management + +- **Integrity checking**: Comprehensive SQLite database validation +- **Automated repair**: Optional auto-repair of corruption +- **WAL file handling**: Proper SQLite Write-Ahead Logging management + +### Notification System + +- **Console output**: Color-coded status messages +- **Webhook notifications**: Custom webhook URL support +- **Email notifications**: SMTP-based email alerts +- **Default webhook**: Automatic notifications to configured endpoint + +### Safety Features + +- **Pre-flight checks**: Disk space and system validation +- **Service management**: Safe Plex service start/stop +- **Backup verification**: Checksum and integrity validation +- **Error handling**: Comprehensive error detection and recovery + +## Automation and Scheduling + +### Cron Integration + +Example crontab entries for automated operations: + +```bash +# Daily Plex backup at 04:15 +15 4 * * * /home/acedanger/shell/plex/backup-plex.sh --non-interactive --auto-repair 2>&1 | logger -t plex-backup -p user.info + +# Daily validation at 07:00 +0 7 * * * /home/acedanger/shell/plex/validate-plex-backups.sh --fix 2>&1 | logger -t plex-validation -p user.info +``` + +### Log Monitoring + +Monitor backup operations with: + +```bash +# Real-time monitoring +sudo journalctl -f -t plex-backup -t plex-validation + +# Historical analysis +sudo journalctl --since '24 hours ago' -t plex-backup + +# Performance analysis +jq '.[] | select(.operation == "backup") | .duration_seconds' logs/plex-backup-performance.json +``` + +## Troubleshooting + +### Common Issues + +1. **Database corruption**: Use `--auto-repair` flag or manual repair +2. **Insufficient disk space**: Check space requirements (2x backup size) +3. **Service management**: Ensure Plex service accessibility +4. **Archive validation**: Use validation script for integrity checks + +### Debug Mode + +Enable verbose logging: + +```bash +# Add environment variable for debug output +PLEX_DEBUG=true ./backup-plex.sh +``` + +### Log Analysis + +```bash +# Check backup success rate +grep "SUCCESS" logs/plex-backup-*.log | wc -l + +# Analyze errors +grep "ERROR" logs/plex-backup-*.log | tail -10 + +# Performance trends +jq '[.[] | select(.operation == "backup") | .duration_seconds] | add/length' logs/plex-backup-performance.json +``` + +## Security Considerations + +### File Permissions + +- Backup files created with appropriate permissions +- Sensitive files maintain original ownership +- Temporary files properly cleaned up + +### Access Control + +- Script requires appropriate sudo permissions +- Backup locations should have restricted access +- Log files contain operational data only + +### Network Security + +- Webhook notifications use HTTPS when possible +- No sensitive data included in notifications +- Email notifications respect system configuration + +## Documentation + +### Detailed Documentation + +- **[plex-backup.md](./plex-backup.md)**: Comprehensive backup script documentation +- **[plex-management.md](./plex-management.md)**: Plex management and administration guide + +### Integration Notes + +- All scripts follow repository coding standards +- Consistent logging and error handling +- Color-coded output for readability +- Comprehensive help systems + +## Migration Notes + +When migrating from legacy backup scripts: + +1. **Backup current configuration**: Save any custom modifications +2. **Test new scripts**: Run with `--check-integrity` first +3. **Update automation**: Modify cron jobs to use new options +4. **Monitor performance**: Check performance logs for optimization + +The enhanced scripts maintain backward compatibility while adding significant new capabilities. + +--- + +*For additional support and advanced configuration options, refer to the detailed documentation files in this directory.* diff --git a/backup-plex.sh b/plex/backup-plex.sh similarity index 100% rename from backup-plex.sh rename to plex/backup-plex.sh diff --git a/integration-test-plex.sh b/plex/integration-test-plex.sh similarity index 100% rename from integration-test-plex.sh rename to plex/integration-test-plex.sh diff --git a/monitor-plex-backup.sh b/plex/monitor-plex-backup.sh similarity index 100% rename from monitor-plex-backup.sh rename to plex/monitor-plex-backup.sh diff --git a/docs/plex-backup.md b/plex/plex-backup.md similarity index 98% rename from docs/plex-backup.md rename to plex/plex-backup.md index 26db1ba..2d0fd6c 100644 --- a/docs/plex-backup.md +++ b/plex/plex-backup.md @@ -125,7 +125,7 @@ This line defines the log file path, including the current date and time in the ```bash if systemctl is-active --quiet plexmediaserver.service; then - /home/acedanger/shell/plex.sh stop || { echo "Failed to stop plexmediaserver.service"; exit 1; } + /home/acedanger/shell/plex/plex.sh stop || { echo "Failed to stop plexmediaserver.service"; exit 1; } fi ``` @@ -184,7 +184,7 @@ This command sends a notification upon completion of the backup process, indicat ```bash if systemctl is-enabled --quiet plexmediaserver.service; then - /home/acedanger/shell/plex.sh start || { echo "Failed to start plexmediaserver.service"; exit 1; } + /home/acedanger/shell/plex/plex.sh start || { echo "Failed to start plexmediaserver.service"; exit 1; } fi ``` diff --git a/docs/plex-management.md b/plex/plex-management.md similarity index 100% rename from docs/plex-management.md rename to plex/plex-management.md diff --git a/plex-recent-additions.sh b/plex/plex-recent-additions.sh similarity index 100% rename from plex-recent-additions.sh rename to plex/plex-recent-additions.sh diff --git a/plex.sh b/plex/plex.sh similarity index 100% rename from plex.sh rename to plex/plex.sh diff --git a/restore-plex.sh b/plex/restore-plex.sh similarity index 100% rename from restore-plex.sh rename to plex/restore-plex.sh diff --git a/test-plex-backup.sh b/plex/test-plex-backup.sh similarity index 100% rename from test-plex-backup.sh rename to plex/test-plex-backup.sh diff --git a/validate-plex-backups.sh b/plex/validate-plex-backups.sh similarity index 100% rename from validate-plex-backups.sh rename to plex/validate-plex-backups.sh diff --git a/update.sh b/update.sh index 268b5e7..0c8ef2a 100755 --- a/update.sh +++ b/update.sh @@ -31,7 +31,7 @@ echo -e "${GREEN}Using package manager: $PKG_MANAGER${NC}" # checks if the plexmediaserver.service is defined on this machine. stop it if it is. if systemctl is-active --quiet plexmediaserver.service 2>/dev/null; then - sudo /home/acedanger/shell/plex.sh stop + sudo /home/acedanger/shell/plex/plex.sh stop fi omz_upgrade_script=~/.oh-my-zsh/tools/upgrade.sh @@ -63,5 +63,5 @@ esac # checks if the plexmediaserver.service is defined on this machine. start it if it is. if systemctl is-enabled --quiet plexmediaserver.service 2>/dev/null; then - sudo /home/acedanger/shell/plex.sh start + sudo /home/acedanger/shell/plex/plex.sh start fi