From e71ca59d1ca340361ad934b695204359199abaf2 Mon Sep 17 00:00:00 2001 From: Peter Wood Date: Thu, 13 Mar 2025 18:31:47 -0400 Subject: [PATCH 1/5] 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. From 888d3173dbb92fc30bbf9f3dba28484d1c6420fb Mon Sep 17 00:00:00 2001 From: Peter Wood Date: Wed, 19 Mar 2025 14:29:53 -0400 Subject: [PATCH 2/5] added powershell profile --- Microsoft.PowerShell_profile.ps1 | 166 +++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) create mode 100644 Microsoft.PowerShell_profile.ps1 diff --git a/Microsoft.PowerShell_profile.ps1 b/Microsoft.PowerShell_profile.ps1 new file mode 100644 index 0000000..08b2584 --- /dev/null +++ b/Microsoft.PowerShell_profile.ps1 @@ -0,0 +1,166 @@ +if (-not (Get-Module -ListAvailable -Name Terminal-Icons)) { + Import-Module -Name Terminal-Icons +} +if (-not (Get-Module -ListAvailable -Name PSReadLine)) { + Import-Module -Name PSReadLine +} +if (-not (Get-Module -ListAvailable -Name tiPS)) { + Install-Module -Name tiPS -Scope CurrentUser +} +if (-not (Get-Module -ListAvailable -Name PSScriptAnalyzer)) { + Install-Module -Name PSScriptAnalyzer -Force -Scope CurrentUser +} + +# kali.omp.json +oh-my-posh --init --shell pwsh --config "$env:OneDrive\Documents\PowerShell\prompt\themes\easy-term.omp.json" | Invoke-Expression + +Set-PSReadLineOption -PredictionSource History +Set-PSReadLineOption -PredictionViewStyle ListView +Set-PSReadLineOption -EditMode Windows +Set-PSReadLineKeyHandler -Key Tab -Function Complete + +Register-ArgumentCompleter -Native -CommandName winget -ScriptBlock { + param($wordToComplete, $commandAst, $cursorPosition) + [Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding = [System.Text.Utf8Encoding]::new() + $Local:word = $wordToComplete.Replace('"', '""') + $Local:ast = $commandAst.ToString().Replace('"', '""') + winget complete --word="$Local:word" --commandline "$Local:ast" --position $cursorPosition | ForEach-Object { + [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) + } +} +function Get-Ip-Address { + (Invoke-WebRequest -Uri ifconfig.me/ip).Content +} + +Set-Alias getIp Get-Ip-Address + +function Invoke-WslReboot() { + param ( + [string]$Distro = 'Ubuntu' + ) + Write-Host "Rebooting $Distro" + wsl --shutdown +} + +Set-Alias wslreboot Invoke-WslReboot + +function Update-Budget() { + Write-Host "Updating budget database" + py D:\dev\export-budget-csv\export.py -s "$env:OneDrive\Documents\Financial\Wood Family Financials.xlsx" + Write-Host "Budget database updated" +} + +Set-Alias updbudget Update-Budget + +function Update-Winget() { + winget upgrade +} + +Set-Alias wgu Update-Winget +#f45873b3-b655-43a6-b217-97c00aa0db58 PowerToys CommandNotFound module + +Import-Module -Name Microsoft.WinGet.CommandNotFound +#f45873b3-b655-43a6-b217-97c00aa0db58 + +if (Get-Command zoxide -ErrorAction SilentlyContinue) { + Invoke-Expression (& { (zoxide init powershell | Out-String) }) +} +else { + Write-Host "zoxide command not found. Attempting to install via winget..." + try { + winget install -e --id ajeetdsouza.zoxide + Write-Host "zoxide installed successfully. Initializing..." + Invoke-Expression (& { (zoxide init powershell | Out-String) }) + } + catch { + Write-Error "Failed to install zoxide. Error: $_" + } +} + +Set-TiPSConfiguration -AutomaticallyWritePowerShellTip EverySession + +function ff($name) { + Get-ChildItem -Recurse -Filter "*${name}*" -ErrorAction SilentlyContinue | ForEach-Object { Write-Output "${$_.directory}\$(%_)" } +} + +function touch($file) { + "" | Out-File -File $file -Encoding ascii +} + +function reload-profile { + & $PROFILE +} + + +function Update-PowerShell { + if (-not $global:canConnectToGitHub) { + Write-Host "Skipping PowerShell update check due to GitHub.com not responding within 1 second." -ForegroundColor Yellow + return + } + + try { + Write-Host "Checking for PowerShell updates..." -ForegroundColor Cyan + $updateNeeded = $false + $currentVersion = $PSVersionTable.PSVersion.ToString() + $gitHubApiUrl = "https://api.github.com/repos/PowerShell/PowerShell/releases/latest" + $latestReleaseInfo = Invoke-RestMethod -Uri $gitHubApiUrl + $latestVersion = $latestReleaseInfo.tag_name.Trim('v') + if ($currentVersion -lt $latestVersion) { + $updateNeeded = $true + } + + if ($updateNeeded) { + Write-Host "Updating PowerShell..." -ForegroundColor Yellow + winget upgrade "Microsoft.PowerShell" --accept-source-agreements --accept-package-agreements + Write-Host "PowerShell has been updated. Please restart your shell to reflect changes" -ForegroundColor Magenta + } + else { + Write-Host "Your PowerShell is up to date." -ForegroundColor Green + } + } + catch { + Write-Error "Failed to update PowerShell. Error: $_" + } +} +Update-PowerShell + +function grep($regex, $dir) { + if ( $dir ) { + Get-ChildItem $dir | select-string $regex + return + } + $input | select-string $regex +} + +function df { + get-volume +} + +function head { + param($Path, $n = 10) + Get-Content $Path -Head $n +} + +function tail { + param($Path, $n = 10) + Get-Content $Path -Tail $n +} + +function docs { Set-Location -Path $HOME\Documents } + +# Networking Utilities +function flushdns { Clear-DnsClientCache } + +# Clipboard Utilities +function cpy { Set-Clipboard $args[0] } + +function pst { Get-Clipboard } + +# Enhanced PowerShell Experience +Set-PSReadLineOption -Colors @{ + Command = 'Yellow' + Parameter = 'Green' + String = 'DarkCyan' +} + +# http://bin.christitus.com/unakijolon \ No newline at end of file From a2ddf90a8ccd6b21916da13b05d0c13183762119 Mon Sep 17 00:00:00 2001 From: Peter Wood Date: Wed, 19 Mar 2025 14:38:50 -0400 Subject: [PATCH 3/5] fix: ensure Terminal-Icons and PSReadLine modules are installed before importing in PowerShell profile --- Microsoft.PowerShell_profile.ps1 | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/Microsoft.PowerShell_profile.ps1 b/Microsoft.PowerShell_profile.ps1 index 08b2584..3c87d55 100644 --- a/Microsoft.PowerShell_profile.ps1 +++ b/Microsoft.PowerShell_profile.ps1 @@ -1,15 +1,23 @@ -if (-not (Get-Module -ListAvailable -Name Terminal-Icons)) { - Import-Module -Name Terminal-Icons -} -if (-not (Get-Module -ListAvailable -Name PSReadLine)) { - Import-Module -Name PSReadLine -} if (-not (Get-Module -ListAvailable -Name tiPS)) { Install-Module -Name tiPS -Scope CurrentUser } if (-not (Get-Module -ListAvailable -Name PSScriptAnalyzer)) { Install-Module -Name PSScriptAnalyzer -Force -Scope CurrentUser } +if (-not (Get-Module -ListAvailable -Name Terminal-Icons)) { + Install-Module -Name Terminal-Icons -Scope CurrentUser +} +if (-not (Get-Module -ListAvailable -Name PSReadLine)) { + Install-Module -Name PSReadLine -Force -Scope CurrentUser +} + + +if (-not (Get-Module -ListAvailable -Name Terminal-Icons)) { + Import-Module -Name Terminal-Icons +} +if (-not (Get-Module -ListAvailable -Name PSReadLine)) { + Import-Module -Name PSReadLine +} # kali.omp.json oh-my-posh --init --shell pwsh --config "$env:OneDrive\Documents\PowerShell\prompt\themes\easy-term.omp.json" | Invoke-Expression From 3f6b73a3a0fa37c98a81eb3a3a3693d097060ede Mon Sep 17 00:00:00 2001 From: Peter Wood Date: Wed, 19 Mar 2025 14:47:10 -0400 Subject: [PATCH 4/5] moved to acedanger/dotfiles --- Microsoft.PowerShell_profile.ps1 | 174 ------------------------------- tailscale-acl.json | 59 ----------- 2 files changed, 233 deletions(-) delete mode 100644 Microsoft.PowerShell_profile.ps1 delete mode 100644 tailscale-acl.json diff --git a/Microsoft.PowerShell_profile.ps1 b/Microsoft.PowerShell_profile.ps1 deleted file mode 100644 index 3c87d55..0000000 --- a/Microsoft.PowerShell_profile.ps1 +++ /dev/null @@ -1,174 +0,0 @@ -if (-not (Get-Module -ListAvailable -Name tiPS)) { - Install-Module -Name tiPS -Scope CurrentUser -} -if (-not (Get-Module -ListAvailable -Name PSScriptAnalyzer)) { - Install-Module -Name PSScriptAnalyzer -Force -Scope CurrentUser -} -if (-not (Get-Module -ListAvailable -Name Terminal-Icons)) { - Install-Module -Name Terminal-Icons -Scope CurrentUser -} -if (-not (Get-Module -ListAvailable -Name PSReadLine)) { - Install-Module -Name PSReadLine -Force -Scope CurrentUser -} - - -if (-not (Get-Module -ListAvailable -Name Terminal-Icons)) { - Import-Module -Name Terminal-Icons -} -if (-not (Get-Module -ListAvailable -Name PSReadLine)) { - Import-Module -Name PSReadLine -} - -# kali.omp.json -oh-my-posh --init --shell pwsh --config "$env:OneDrive\Documents\PowerShell\prompt\themes\easy-term.omp.json" | Invoke-Expression - -Set-PSReadLineOption -PredictionSource History -Set-PSReadLineOption -PredictionViewStyle ListView -Set-PSReadLineOption -EditMode Windows -Set-PSReadLineKeyHandler -Key Tab -Function Complete - -Register-ArgumentCompleter -Native -CommandName winget -ScriptBlock { - param($wordToComplete, $commandAst, $cursorPosition) - [Console]::InputEncoding = [Console]::OutputEncoding = $OutputEncoding = [System.Text.Utf8Encoding]::new() - $Local:word = $wordToComplete.Replace('"', '""') - $Local:ast = $commandAst.ToString().Replace('"', '""') - winget complete --word="$Local:word" --commandline "$Local:ast" --position $cursorPosition | ForEach-Object { - [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) - } -} -function Get-Ip-Address { - (Invoke-WebRequest -Uri ifconfig.me/ip).Content -} - -Set-Alias getIp Get-Ip-Address - -function Invoke-WslReboot() { - param ( - [string]$Distro = 'Ubuntu' - ) - Write-Host "Rebooting $Distro" - wsl --shutdown -} - -Set-Alias wslreboot Invoke-WslReboot - -function Update-Budget() { - Write-Host "Updating budget database" - py D:\dev\export-budget-csv\export.py -s "$env:OneDrive\Documents\Financial\Wood Family Financials.xlsx" - Write-Host "Budget database updated" -} - -Set-Alias updbudget Update-Budget - -function Update-Winget() { - winget upgrade -} - -Set-Alias wgu Update-Winget -#f45873b3-b655-43a6-b217-97c00aa0db58 PowerToys CommandNotFound module - -Import-Module -Name Microsoft.WinGet.CommandNotFound -#f45873b3-b655-43a6-b217-97c00aa0db58 - -if (Get-Command zoxide -ErrorAction SilentlyContinue) { - Invoke-Expression (& { (zoxide init powershell | Out-String) }) -} -else { - Write-Host "zoxide command not found. Attempting to install via winget..." - try { - winget install -e --id ajeetdsouza.zoxide - Write-Host "zoxide installed successfully. Initializing..." - Invoke-Expression (& { (zoxide init powershell | Out-String) }) - } - catch { - Write-Error "Failed to install zoxide. Error: $_" - } -} - -Set-TiPSConfiguration -AutomaticallyWritePowerShellTip EverySession - -function ff($name) { - Get-ChildItem -Recurse -Filter "*${name}*" -ErrorAction SilentlyContinue | ForEach-Object { Write-Output "${$_.directory}\$(%_)" } -} - -function touch($file) { - "" | Out-File -File $file -Encoding ascii -} - -function reload-profile { - & $PROFILE -} - - -function Update-PowerShell { - if (-not $global:canConnectToGitHub) { - Write-Host "Skipping PowerShell update check due to GitHub.com not responding within 1 second." -ForegroundColor Yellow - return - } - - try { - Write-Host "Checking for PowerShell updates..." -ForegroundColor Cyan - $updateNeeded = $false - $currentVersion = $PSVersionTable.PSVersion.ToString() - $gitHubApiUrl = "https://api.github.com/repos/PowerShell/PowerShell/releases/latest" - $latestReleaseInfo = Invoke-RestMethod -Uri $gitHubApiUrl - $latestVersion = $latestReleaseInfo.tag_name.Trim('v') - if ($currentVersion -lt $latestVersion) { - $updateNeeded = $true - } - - if ($updateNeeded) { - Write-Host "Updating PowerShell..." -ForegroundColor Yellow - winget upgrade "Microsoft.PowerShell" --accept-source-agreements --accept-package-agreements - Write-Host "PowerShell has been updated. Please restart your shell to reflect changes" -ForegroundColor Magenta - } - else { - Write-Host "Your PowerShell is up to date." -ForegroundColor Green - } - } - catch { - Write-Error "Failed to update PowerShell. Error: $_" - } -} -Update-PowerShell - -function grep($regex, $dir) { - if ( $dir ) { - Get-ChildItem $dir | select-string $regex - return - } - $input | select-string $regex -} - -function df { - get-volume -} - -function head { - param($Path, $n = 10) - Get-Content $Path -Head $n -} - -function tail { - param($Path, $n = 10) - Get-Content $Path -Tail $n -} - -function docs { Set-Location -Path $HOME\Documents } - -# Networking Utilities -function flushdns { Clear-DnsClientCache } - -# Clipboard Utilities -function cpy { Set-Clipboard $args[0] } - -function pst { Get-Clipboard } - -# Enhanced PowerShell Experience -Set-PSReadLineOption -Colors @{ - Command = 'Yellow' - Parameter = 'Green' - String = 'DarkCyan' -} - -# http://bin.christitus.com/unakijolon \ No newline at end of file diff --git a/tailscale-acl.json b/tailscale-acl.json deleted file mode 100644 index e63566b..0000000 --- a/tailscale-acl.json +++ /dev/null @@ -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. -} From 89e0336754d496da551d07dc03c6bf6501755bbc Mon Sep 17 00:00:00 2001 From: Peter Wood Date: Sat, 29 Mar 2025 13:03:06 -0400 Subject: [PATCH 5/5] feat: add scripts for event log size monitoring and old file removal --- powershell/event-log-size.ps1 | 19 +++++++++++++++++++ .../remove-files.ps1 | 0 2 files changed, 19 insertions(+) create mode 100644 powershell/event-log-size.ps1 rename remove-files.ps1 => powershell/remove-files.ps1 (100%) diff --git a/powershell/event-log-size.ps1 b/powershell/event-log-size.ps1 new file mode 100644 index 0000000..eea9a0f --- /dev/null +++ b/powershell/event-log-size.ps1 @@ -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)) +} \ No newline at end of file diff --git a/remove-files.ps1 b/powershell/remove-files.ps1 similarity index 100% rename from remove-files.ps1 rename to powershell/remove-files.ps1