Merge branch 'main' of github.com:acedanger/shell

This commit is contained in:
Peter Wood
2025-04-11 13:25:32 +00:00
8 changed files with 417 additions and 61 deletions

View File

@@ -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

92
docs/backup-media.md Normal file
View File

@@ -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.

72
docs/folder-metrics.md Normal file
View File

@@ -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 <directory>"
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 <directory>
```
- `<directory>`: 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.

139
docs/plex-backup.md Normal file
View File

@@ -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.

80
docs/plex-management.md Normal file
View File

@@ -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.

View File

@@ -0,0 +1,19 @@
$logs = Get-EventLog -list
foreach ($log in $logs) {
$logName = $log.LogDisplayName
$logFilePath = Join-Path -Path $env:SystemRoot -ChildPath "System32\winevt\Logs\$($log.LogFileName).evtx"
if (Test-Path $logFilePath) {
$fileSize = (Get-Item $logFilePath).Length / 1MB
} else {
$fileSize = 0
}
# Skip if file size is less than 1 MB
if ($fileSize -lt 1) { continue }
$maxSize = $log.MaximumKilobytes / 1024 # Convert KB to MB
$percentage = if ($maxSize -ne 0) { ($fileSize / $maxSize) * 100 } else { 0 }
Write-Host ("Log Name: {0}`nFile Size: {1:N2} MB`nMaximum Size: {2:N2} MB`nPercentage Full: {3}%`n---" -f $logName, $fileSize, $maxSize, [math]::Round($percentage, 2))
}

View File

@@ -1,59 +0,0 @@
// Example/default ACLs for unrestricted connections.
{
// Define access control lists for users, groups, autogroups, tags,
// Tailscale IP addresses, and subnet ranges.
"acls": [
{
"action": "accept",
"src": ["tag:client", "tag:server", "acedanger49@gmail.com"],
"dst": ["tag:golink:*", "tag:server:*"],
},
// Allow all connections.
// Comment this section out if you want to define specific restrictions.
{"action": "accept", "src": ["*"], "dst": ["*:*"]},
],
// Define users and devices that can use Tailscale SSH.
"ssh": [
{
// any user can use Tailscale SSH to connect to their own devices
// in check mode as a root or non-root user
"action": "accept",
"src": ["tag:client", "tag:server", "acedanger49@gmail.com"],
"dst": ["tag:server"],
"users": ["autogroup:nonroot", "root"],
},
{
// any user can use Tailscale SSH to connect to their own devices
// in check mode as a root or non-root user
"action": "check",
"src": ["autogroup:member"],
"dst": ["autogroup:self"],
"users": ["autogroup:nonroot", "root"],
},
],
"nodeAttrs": [
{
// Funnel policy, which lets tailnet members control Funnel
// for their own devices.
// Learn more at https://tailscale.com/kb/1223/tailscale-funnel/
"target": ["autogroup:member"],
"attr": ["funnel"],
},
{"target": ["*"], "app": {"tailscale.com/app-connectors": []}},
],
// Define the tags which can be applied to devices and by which users.
"tagOwners": {
"tag:golink": ["acedanger49@gmail.com"],
"tag:server": ["acedanger49@gmail.com"],
"tag:client": ["acedanger49@gmail.com"],
"tag:docker": ["acedanger49@gmail.com"],
},
"autoapprovers": {
"exitNode": ["autogroup:admin"],
},
// Test access rules every time they're saved.
}