From e71ca59d1ca340361ad934b695204359199abaf2 Mon Sep 17 00:00:00 2001 From: Peter Wood Date: Thu, 13 Mar 2025 18:31:47 -0400 Subject: [PATCH] feat: add documentation for backup-media, folder-metrics, and plex management scripts --- README.md | 17 ++++- docs/backup-media.md | 92 ++++++++++++++++++++++++++ docs/folder-metrics.md | 72 +++++++++++++++++++++ docs/plex-backup.md | 139 ++++++++++++++++++++++++++++++++++++++++ docs/plex-management.md | 80 +++++++++++++++++++++++ 5 files changed, 398 insertions(+), 2 deletions(-) create mode 100644 docs/backup-media.md create mode 100644 docs/folder-metrics.md create mode 100644 docs/plex-backup.md create mode 100644 docs/plex-management.md diff --git a/README.md b/README.md index d898e17..8b07966 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,19 @@ -# shell +# Shell Scripts Documentation -Shell commands +This repository contains various shell scripts for managing media-related tasks. + +## Available Scripts + +- [Backup Media Script](docs/backup-media.md): Documentation for the `backup-media.sh` script. +- `plex.sh`: Script to manage the Plex Media Server (start, stop, restart, status). +- `backup-plex.sh`: Script to back up Plex Media Server databases and related files. +- `folder-metrics.sh`: Script to calculate disk usage and file count for a directory and its subdirectories. + +## Documentation + +- [Plex Backup Script Documentation](./docs/plex-backup.md): Detailed documentation for the `backup-plex.sh` script. +- [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. # plex.sh diff --git a/docs/backup-media.md b/docs/backup-media.md new file mode 100644 index 0000000..3674150 --- /dev/null +++ b/docs/backup-media.md @@ -0,0 +1,92 @@ +# Backup Media Script Documentation + +This document provides an overview of the `backup-media.sh` script, which is used to back up media-related databases and configurations from various Docker containers. + +## Script Overview + +The script performs the following tasks: +1. Creates a log directory if it doesn't exist. +2. Generates a log file with the current date and time. +3. Defines a function to log file details. +4. Copies backup files from specified Docker containers to a backup directory. +5. Logs the details of each copied file. +6. Sends a notification upon completion. + +## Detailed Steps + +### 1. Create Log Directory + +```bash +mkdir -p /mnt/share/media/backups/logs +``` + +This command ensures that the log directory exists. + +### 2. Generate Log File + +```bash +LOG_FILE="/mnt/share/media/backups/logs/backup_log_$(date +%Y%m%d_%H%M%S).md" +``` + +A log file is created with a timestamp in its name. + +### 3. 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 source, destination, and size of each backed-up file. + +### 4. Copy Backup Files and Log Details + +The script copies backup files from various Docker containers and logs the details using the `log_file_details` function. Here are the examples for all services: + +```bash +docker cp sonarr:/config/Backups/scheduled /mnt/share/media/backups/sonarr/ +log_file_details "sonarr:/config/Backups/scheduled" "/mnt/share/media/backups/sonarr/" + +docker cp radarr:/config/Backups/scheduled /mnt/share/media/backups/radarr/ +log_file_details "radarr:/config/Backups/scheduled" "/mnt/share/media/backups/radarr/" + +docker cp prowlarr:/config/Backups/scheduled /mnt/share/media/backups/prowlarr/ +log_file_details "prowlarr:/config/Backups/scheduled" "/mnt/share/media/backups/prowlarr/" + +docker cp audiobookshelf:/metadata/backups /mnt/share/media/backups/audiobookshelf/ +log_file_details "audiobookshelf:/metadata/backups" "/mnt/share/media/backups/audiobookshelf/" + +docker cp tautulli:/config/backups /mnt/share/media/backups/tautulli/ +log_file_details "tautulli:/config/backups" "/mnt/share/media/backups/tautulli/" + +docker cp sabnzbd:/config/sabnzbd.ini /mnt/share/media/backups/sabnzbd/sabnzbd_$(date +%Y%m%d).ini +log_file_details "sabnzbd:/config/sabnzbd.ini" "/mnt/share/media/backups/sabnzbd/sabnzbd_$(date +%Y%m%d).ini" + +mkdir -p /mnt/share/media/backups/jellyseerr/backup_$(date +%Y%m%d) +docker cp jellyseerr:/config/db/ /mnt/share/media/backups/jellyseerr/backup_$(date +%Y%m%d)/ +log_file_details "jellyseerr:/config/db/" "/mnt/share/media/backups/jellyseerr/backup_$(date +%Y%m%d)/" + +docker cp jellyseerr:/config/settings.json /mnt/share/media/backups/jellyseerr/backup_$(date +%Y%m%d)/ +log_file_details "jellyseerr:/config/settings.json" "/mnt/share/media/backups/jellyseerr/backup_$(date +%Y%m%d)/" +``` + +### 5. Send Notification + +```bash +curl \ + -H tags:popcorn,backup,sonarr,radarr,prowlarr,sabnzbd,audiobookshelf,tautulli,jellyseerr,${HOSTNAME} \ + -d "A backup of media-related databases has been saved to the /media/backups folder" \ + https://notify.peterwood.rocks/lab +``` + +A notification is sent upon completion of the backup process. + +## Conclusion + +The `backup-media.sh` script automates the process of backing up media-related databases and configurations from Docker containers, logging the details, and sending a notification upon completion. diff --git a/docs/folder-metrics.md b/docs/folder-metrics.md new file mode 100644 index 0000000..094428d --- /dev/null +++ b/docs/folder-metrics.md @@ -0,0 +1,72 @@ +# Folder Metrics Script Documentation + +This document provides an overview and step-by-step explanation of the `folder-metrics.sh` script. This script calculates disk usage and file count for a directory and its subdirectories. + +## Script Overview + +The script performs the following main tasks: +1. Checks if a directory is provided as an argument. +2. Calculates the disk usage of the directory. +3. Iterates over each subdirectory to calculate disk usage and file count. +4. Outputs the results in a tabular format. + +## Detailed Steps + +### 1. Check if a Directory is Provided + +```bash +if [ -z "$1" ]; then + echo "Usage: $0 " + exit 1 +fi +``` + +This block checks if a directory is provided as an argument. If not, it prints a usage message and exits. + +### 2. Calculate Disk Usage of the Directory + +```bash +echo "Disk usage for directory: $1" +echo "--------------------------" +du -sh "$1" +``` + +This block calculates and prints the disk usage of the provided directory. + +### 3. Iterate Over Each Subdirectory + +```bash +echo +echo "Usage for each subdirectory" +echo "--------------------------" + +for dir in "$1"/*; do + if [ -d "$dir" ]; then + space_used=$(du -sh "$dir" | cut -f1) + file_count=$(find "$dir" -type f | wc -l) + printf "%-30s %-10s %10s files\n" "$(basename "$dir")" "$space_used" "$file_count" + fi +done +``` + +This block iterates over each subdirectory, calculates the disk usage and file count, and prints the results in a tabular format. + +## Usage + +To use the script, run it with the directory path as an argument: + +```shell +./folder-metrics.sh +``` + +- ``: The path to the directory for which you want to calculate disk usage and file count. + +## Important Information + +- Ensure that the script is executable. You can make it executable with the following command: + ```shell + chmod +x folder-metrics.sh + ``` +- The script requires a directory path as an argument. Ensure that you provide a valid directory path when running the script. + +By following this documentation, you should be able to understand and use the `folder-metrics.sh` script effectively. diff --git a/docs/plex-backup.md b/docs/plex-backup.md new file mode 100644 index 0000000..f5ba2ee --- /dev/null +++ b/docs/plex-backup.md @@ -0,0 +1,139 @@ +# 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. diff --git a/docs/plex-management.md b/docs/plex-management.md new file mode 100644 index 0000000..35d75cc --- /dev/null +++ b/docs/plex-management.md @@ -0,0 +1,80 @@ +# Plex Management Script Documentation + +This document provides an overview and step-by-step explanation of the `plex.sh` script. This script is used to manage the Plex Media Server service on a systemd-based Linux distribution. + +## Script Overview + +The script performs the following main tasks: +1. Starts the Plex Media Server. +2. Stops the Plex Media Server. +3. Restarts the Plex Media Server. +4. Displays the current status of the Plex Media Server. + +## Detailed Steps + +### 1. Start Plex Media Server + +```bash +start_plex() { + sudo systemctl start plexmediaserver + echo "Plex Media Server started." +} +``` + +This function starts the Plex Media Server using `systemctl`. + +### 2. Stop Plex Media Server + +```bash +stop_plex() { + sudo systemctl stop plexmediaserver + echo "Plex Media Server stopped." +} +``` + +This function stops the Plex Media Server using `systemctl`. + +### 3. Restart Plex Media Server + +```bash +restart_plex() { + sudo systemctl restart plexmediaserver + echo "Plex Media Server restarted." +} +``` + +This function restarts the Plex Media Server using `systemctl`. + +### 4. Display Plex Media Server Status + +```bash +status_plex() { + sudo systemctl status plexmediaserver +} +``` + +This function displays the current status of the Plex Media Server using `systemctl`. + +## Usage + +To use the script, run it with one of the following parameters: + +```shell +./plex.sh {start|stop|restart|status} +``` + +- `start`: Starts the Plex Media Server. +- `stop`: Stops the Plex Media Server. +- `restart`: Restarts the Plex Media Server. +- `status`: Displays the current status of the Plex Media Server. + +## Important Information + +- Ensure that the script is executable. You can make it executable with the following command: + ```shell + chmod +x plex.sh + ``` +- The script uses `systemctl` to manage the Plex Media Server service. Ensure that `systemctl` is available on your system. +- The script requires `sudo` privileges to manage the Plex Media Server service. Ensure that you have the necessary permissions to run the script with `sudo`. + +By following this documentation, you should be able to understand and use the `plex.sh` script effectively.