Files
shell/docs/plex-backup.md

140 lines
6.2 KiB
Markdown

# Plex Backup Script Documentation
This document provides an overview and step-by-step explanation of the `backup-plex.sh` script. This script is designed to back up Plex Media Server databases and related files, compress the backup, and clean up the original files if the compression is successful.
## Script Overview
The script performs the following main tasks:
1. Creates a log directory if it doesn't exist.
2. Defines a log file with the current date and time.
3. Defines a function to log file details.
4. Stops the Plex Media Server service if it is running.
5. Creates a backup directory with the current date.
6. Copies important Plex database files and preferences to the backup directory.
7. Logs the details of the copied files.
8. Compresses the backup directory into a gzip archive.
9. Deletes the original backup directory if the compression is successful.
10. Sends a notification upon completion.
11. Restarts the Plex Media Server service if it was stopped.
## Detailed Steps
### 1. Create Log Directory
```bash
mkdir -p /mnt/share/media/backups/logs || { echo "Failed to create log directory"; exit 1; }
```
This command ensures that the log directory exists. If it doesn't, it creates the directory. If the directory creation fails, the script exits with an error message.
### 2. Define Log File
```bash
LOG_FILE="/mnt/share/media/backups/logs/backup_log_$(date +%Y%m%d_%H%M%S).md"
```
This line defines the log file path, including the current date and time in the filename to ensure uniqueness.
### 3. Define Log File Details Function
```bash
log_file_details() {
local src=$1
local dest=$2
local size=$(du -sh "$dest" | cut -f1)
echo "Source: $src" >> "$LOG_FILE"
echo "Destination: $dest" >> "$LOG_FILE"
echo "Size: $size" >> "$LOG_FILE"
}
```
This function logs the details of the copied files, including the source, destination, and size.
### 4. Stop Plex Media Server Service
```bash
if systemctl is-active --quiet plexmediaserver.service; then
/home/acedanger/shell/plex.sh stop || { echo "Failed to stop plexmediaserver.service"; exit 1; }
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
```bash
mkdir -p /mnt/share/media/backups/plex/$(date +%Y%m%d)/ || { echo "Failed to create backup directory"; exit 1; }
```
This command creates a backup directory with the current date. If the directory creation fails, the script exits with an error message.
### 6. Copy Plex Database Files and Preferences
```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)/"
```
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.
### 7. Compress the Backup Directory
```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
else
echo "Compression failed, not deleting the backup folder" >> "$LOG_FILE"
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.
### 9. Send Notification
```bash
curl \
-H tags:popcorn,backup,plex,${HOSTNAME} \
-d "The Plex databases have been saved to the /media/backups/plex folder" \
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.
### 10. Restart Plex Media Server Service
```bash
if systemctl is-enabled --quiet plexmediaserver.service; then
/home/acedanger/shell/plex.sh start || { echo "Failed to start plexmediaserver.service"; exit 1; }
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`).
## 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.
By following this documentation, you should be able to understand and use the `backup-plex.sh` script effectively.