mirror of
https://github.com/acedanger/shell.git
synced 2025-12-06 04:30:13 -08:00
feat: Revamp Plex backup system to streamline archive structure and enhance validation processes
This commit is contained in:
@@ -302,7 +302,38 @@ The script includes a comprehensive testing framework (`test-plex-backup.sh`):
|
||||
|
||||
## Backup Strategy
|
||||
|
||||
The enhanced script implements a robust backup strategy:
|
||||
The enhanced script implements a robust backup strategy with a streamlined tar.gz-only structure:
|
||||
|
||||
### Archive-Only Directory Structure
|
||||
|
||||
The new backup system eliminates intermediate dated directories and stores only compressed archives:
|
||||
|
||||
```bash
|
||||
/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
|
||||
```
|
||||
|
||||
### Archive Naming Convention
|
||||
|
||||
Backup files follow the pattern: `plex-backup-YYYYMMDD_HHMMSS.tar.gz`
|
||||
|
||||
- **YYYYMMDD**: Date of backup (e.g., 20250125)
|
||||
- **HHMMSS**: Time of backup (e.g., 143022)
|
||||
- **tar.gz**: Compressed archive format
|
||||
|
||||
### Key Improvements
|
||||
|
||||
1. **Direct Archive Creation**: No intermediate directories required
|
||||
2. **Efficient Storage**: Only compressed files stored permanently
|
||||
3. **Easy Identification**: Timestamp-based naming for sorting
|
||||
4. **Legacy Cleanup**: Automatic removal of old dated directories
|
||||
5. **Archive Validation**: Integrity checking of compressed files
|
||||
|
||||
### 3-2-1 Backup Rule
|
||||
|
||||
|
||||
@@ -20,43 +20,55 @@ The enhanced script performs the following advanced tasks:
|
||||
## Enhanced Features
|
||||
|
||||
### Performance Tracking
|
||||
|
||||
- **JSON Performance Logs**: All operations are timed and logged to `logs/plex-backup-performance.json`
|
||||
- **Performance Reports**: Automatic generation of average performance metrics
|
||||
- **Operation Monitoring**: Tracks backup, verification, service management, and overall script execution times
|
||||
|
||||
### Notification System
|
||||
|
||||
The script supports multiple notification channels:
|
||||
|
||||
#### Console Notifications
|
||||
|
||||
- Color-coded status messages (Success: Green, Error: Red, Warning: Yellow, Info: Blue)
|
||||
- Timestamped log entries with clear formatting
|
||||
|
||||
#### Webhook Notifications
|
||||
|
||||
```bash
|
||||
./backup-plex.sh --webhook=https://your-webhook-url.com/endpoint
|
||||
```
|
||||
|
||||
Sends JSON payloads with backup status, hostname, and timestamps.
|
||||
|
||||
#### Email Notifications
|
||||
|
||||
```bash
|
||||
./backup-plex.sh --email=admin@example.com
|
||||
```
|
||||
|
||||
Requires `sendmail` to be configured on the system.
|
||||
|
||||
### WAL File Management
|
||||
|
||||
The script now properly handles SQLite Write-Ahead Logging files:
|
||||
|
||||
- **Automatic Detection**: Identifies and backs up `.db-wal` and `.db-shm` files when present
|
||||
- **WAL Checkpointing**: Performs `PRAGMA wal_checkpoint(FULL)` before integrity checks
|
||||
- **Safe Backup**: Ensures WAL files are properly backed up alongside main database files
|
||||
|
||||
### Database Integrity & Repair
|
||||
|
||||
Enhanced database management features:
|
||||
|
||||
- **Pre-backup Integrity Checks**: Verifies database health before backup operations
|
||||
- **Automated Repair**: Optional automatic repair of corrupted databases using advanced techniques
|
||||
- **Interactive Repair Mode**: Prompts for repair decisions when issues are detected
|
||||
- **Post-repair Verification**: Re-checks integrity after repair operations
|
||||
|
||||
### Parallel Processing
|
||||
|
||||
- **Concurrent Verification**: Parallel backup verification for improved performance
|
||||
- **Fallback Safety**: Automatically falls back to sequential processing if parallel mode fails
|
||||
- **Configurable**: Can be disabled with `--no-parallel` for maximum safety
|
||||
@@ -120,65 +132,62 @@ fi
|
||||
|
||||
This block checks if the Plex Media Server service is running. If it is, the script stops the service using a custom script (`plex.sh`).
|
||||
|
||||
### 5. Create Backup Directory
|
||||
### 5. Backup Plex Database Files and Preferences
|
||||
|
||||
The enhanced backup system now creates compressed archives directly, eliminating intermediate directories:
|
||||
|
||||
```bash
|
||||
mkdir -p /mnt/share/media/backups/plex/$(date +%Y%m%d)/ || { echo "Failed to create backup directory"; exit 1; }
|
||||
# Files are copied to temporary staging area for verification
|
||||
cp "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db" "$BACKUP_PATH/"
|
||||
log_file_details "com.plexapp.plugins.library.db" "$BACKUP_PATH/"
|
||||
|
||||
cp "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.blobs.db" "$BACKUP_PATH/"
|
||||
log_file_details "com.plexapp.plugins.library.blobs.db" "$BACKUP_PATH/"
|
||||
|
||||
cp "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Preferences.xml" "$BACKUP_PATH/"
|
||||
log_file_details "Preferences.xml" "$BACKUP_PATH/"
|
||||
```
|
||||
|
||||
This command creates a backup directory with the current date. If the directory creation fails, the script exits with an error message.
|
||||
These commands copy the Plex database files and preferences directly to the backup root directory. Each file copy operation includes integrity verification and checksum validation.
|
||||
|
||||
### 6. Copy Plex Database Files and Preferences
|
||||
### 6. Create Compressed Archive
|
||||
|
||||
```bash
|
||||
cp "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.db" /mnt/share/media/backups/plex/$(date +%Y%m%d)/ || { echo "Failed to copy com.plexapp.plugins.library.db"; exit 1; }
|
||||
log_file_details "com.plexapp.plugins.library.db" "/mnt/share/media/backups/plex/$(date +%Y%m%d)/"
|
||||
|
||||
cp "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Plug-in Support/Databases/com.plexapp.plugins.library.blobs.db" /mnt/share/media/backups/plex/$(date +%Y%m%d)/ || { echo "Failed to copy com.plexapp.plugins.library.blobs.db"; exit 1; }
|
||||
log_file_details "com.plexapp.plugins.library.blobs.db" "/mnt/share/media/backups/plex/$(date +%Y%m%d)/"
|
||||
|
||||
cp "/var/lib/plexmediaserver/Library/Application Support/Plex Media Server/Preferences.xml" /mnt/share/media/backups/plex/$(date +%Y%m%d)/ || { echo "Failed to copy Preferences.xml"; exit 1; }
|
||||
log_file_details "Preferences.xml" "/mnt/share/media/backups/plex/$(date +%Y%m%d)/"
|
||||
# Create archive directly with timestamp naming convention
|
||||
final_archive="${BACKUP_ROOT}/plex-backup-$(date '+%Y%m%d_%H%M%S').tar.gz"
|
||||
tar -czf "$final_archive" -C "$temp_staging_dir" .
|
||||
```
|
||||
|
||||
These commands copy the Plex database files and preferences to the backup directory. Each file copy operation is followed by a call to the `log_file_details` function to log the details of the copied files.
|
||||
The system now creates compressed archives directly using a timestamp-based naming convention (`plex-backup-YYYYMMDD_HHMMSS.tar.gz`), eliminating the need for intermediate dated directories.
|
||||
|
||||
### 7. Compress the Backup Directory
|
||||
### 7. Archive Validation and Cleanup
|
||||
|
||||
```bash
|
||||
tar -czf /mnt/share/media/backups/plex/$(date +%Y%m%d).tar.gz -C /mnt/share/media/backups/plex/plex $(date +%Y%m%d) || { echo "Failed to compress backup folder"; exit 1; }
|
||||
```
|
||||
|
||||
This command compresses the backup directory into a gzip archive. If the compression fails, the script exits with an error message.
|
||||
|
||||
### 8. Delete Original Backup Directory
|
||||
|
||||
```bash
|
||||
if [ $? -eq 0 ]; then
|
||||
if [ -s /mnt/share/media/backups/plex/$(date +%Y%m%d).tar.gz ]; then
|
||||
rm -rf /mnt/share/media/backups/plex/$(date +%Y%m%d)/ || { echo "Failed to delete original backup folder"; exit 1; }
|
||||
else
|
||||
echo "Compressed file is empty, not deleting the backup folder" >> "$LOG_FILE"
|
||||
fi
|
||||
# Validate archive integrity
|
||||
if tar -tzf "$final_archive" >/dev/null 2>&1; then
|
||||
log_success "Archive created and validated: $(basename "$final_archive")"
|
||||
# Clean up temporary staging files
|
||||
rm -rf "$temp_staging_dir"
|
||||
else
|
||||
echo "Compression failed, not deleting the backup folder" >> "$LOG_FILE"
|
||||
log_error "Archive validation failed"
|
||||
rm -f "$final_archive"
|
||||
fi
|
||||
```
|
||||
|
||||
This block checks if the compression was successful. If it was, and the compressed file is not empty, it deletes the original backup directory. If the compression failed or the compressed file is empty, it logs an appropriate message.
|
||||
The system validates the created archive and removes temporary staging files, ensuring only valid compressed backups are retained in the backup root directory.
|
||||
|
||||
### 9. Send Notification
|
||||
### 8. Send Notification
|
||||
|
||||
```bash
|
||||
curl \
|
||||
-H tags:popcorn,backup,plex,${HOSTNAME} \
|
||||
-d "The Plex databases have been saved to the /media/backups/plex folder" \
|
||||
-d "The Plex databases have been saved to the /media/backups/plex folder as plex-backup-YYYYMMDD_HHMMSS.tar.gz" \
|
||||
https://notify.peterwood.rocks/lab || { echo "Failed to send notification"; exit 1; }
|
||||
```
|
||||
|
||||
This command sends a notification upon completion of the backup process. If the notification fails, the script exits with an error message.
|
||||
This command sends a notification upon completion of the backup process, indicating the compressed archive has been created. If the notification fails, the script exits with an error message.
|
||||
|
||||
### 10. Restart Plex Media Server Service
|
||||
### 9. Restart Plex Media Server Service
|
||||
|
||||
```bash
|
||||
if systemctl is-enabled --quiet plexmediaserver.service; then
|
||||
@@ -188,11 +197,36 @@ fi
|
||||
|
||||
This block checks if the Plex Media Server service is enabled. If it is, the script restarts the service using a custom script (`plex.sh`).
|
||||
|
||||
### 10. Legacy Cleanup
|
||||
|
||||
```bash
|
||||
# Clean up any remaining dated directories from old backup structure
|
||||
find "${BACKUP_ROOT}" -maxdepth 1 -type d -name "????????" -exec rm -rf {} \; 2>/dev/null || true
|
||||
```
|
||||
|
||||
The enhanced system includes cleanup of legacy dated directories from previous backup structure versions, ensuring a clean tar.gz-only backup directory.
|
||||
|
||||
## Important Information
|
||||
|
||||
- Ensure that the [`plex.sh`](https://github.com/acedanger/shell/blob/main/plex.sh) script is available and executable. This script is used to stop and start the Plex Media Server service.
|
||||
- The script uses `systemctl` to manage the Plex Media Server service. Ensure that `systemctl` is available on your system.
|
||||
- The backup directory and log directory paths are hardcoded. Modify these paths as needed to fit your environment.
|
||||
- The script logs important actions and errors to a log file with a timestamped filename. Check the log file for details if any issues arise.
|
||||
- **New Directory Structure**: The enhanced backup system stores only compressed `.tar.gz` files directly in the backup root directory, eliminating intermediate dated directories.
|
||||
- **Archive Naming**: Backup files follow the naming convention `plex-backup-YYYYMMDD_HHMMSS.tar.gz` for easy identification and sorting.
|
||||
- **Legacy Compatibility**: The system automatically cleans up old dated directories from previous backup versions during operation.
|
||||
- The backup directory path is configurable through the `BACKUP_ROOT` variable. Modify this path as needed to fit your environment.
|
||||
- The script logs important actions and errors to timestamped log files. Check the log files for details if any issues arise.
|
||||
- **Backup Validation**: All archives undergo integrity checking to ensure backup reliability.
|
||||
|
||||
By following this documentation, you should be able to understand and use the `backup-plex.sh` script effectively.
|
||||
## Final Directory Structure
|
||||
|
||||
```
|
||||
/mnt/share/media/backups/plex/
|
||||
├── plex-backup-20250125_143022.tar.gz
|
||||
├── plex-backup-20250124_143011.tar.gz
|
||||
├── plex-backup-20250123_143008.tar.gz
|
||||
└── logs/
|
||||
├── backup_log_20250125_143022.md
|
||||
└── plex-backup-performance.json
|
||||
```
|
||||
|
||||
By following this documentation, you should be able to understand and use the enhanced `backup-plex.sh` script effectively with its new streamlined tar.gz-only structure.
|
||||
|
||||
Reference in New Issue
Block a user