Compare commits

...

4 Commits

9 changed files with 265 additions and 1 deletions

View File

@ -2,6 +2,8 @@
Some useful system scripts for things like fan control and other convenience.
**USE FILESYSTEM SCRIPTS AT YOUR OWN RISK.**
You can regenerate `env-template.txt` based on the current .env file with:
```bash
./setup.sh --gentmpl

41
backup/all-subdirs.sh Executable file
View File

@ -0,0 +1,41 @@
#!/bin/bash
if [ "$#" -eq 0 ]; then
echo "Error: Please provide the source directory as an argument."
exit 1
fi
basedir="${1%/}"
echo "Found these directories:"
for dir in $basedir/*; do
if [ -d "$dir" ]; then
echo " - $dir"
fi
done
read -n1 -p "Back them up? (y/n): " answer
echo
if [ "$answer" != "y" ]; then
echo "Exiting."
exit 1
fi
for dir in $basedir/*; do
if [ -d "$dir" ]; then
echo -e "\e[1;36mProcessing folder: $dir\e[0m"
./backup2cloud.sh $dir
if [ $? -ne 0 ]; then
echo -en "\e[1;31mLast run failed. Do you want to continue? (y/n): \e[0m"
read -n1 answer
echo
if [ "$answer" != "y" ]; then
echo "Exiting."
exit 1
fi
fi
fi
done
echo -e "\e[1;32mCompleted\e[0m"

93
backup/backup2cloud.sh Executable file
View File

@ -0,0 +1,93 @@
#!/bin/bash
# ensure prerequisites are installed: rclone p7zip-full
# run `rclone config` to set up your target, put the name you choose as REMOTE_NAME
# edit .env and add an ENC_PASSWORD used for encryption, can also put variable overrides there
# the directory name will be used as archive name by default
# usage: script.sh [path] [archive name override]
# Default values
SOURCE_DIR="$1"
CURRENT_DATE_TIME=$(date +"%Y%m%d_%H%M%S_%s")
REMOTE_NAME="onedrive"
REMOTE_DIR="Backups/"
# Load environment variables from .env file
source .env
# Check if TEMP_DIR is set
if [ -z "$TEMP_DIR" ]; then
echo "Error: TEMP_DIR environment variable is not set."
exit 1
else
echo "Temporary directory: $TEMP_DIR"
sleep 1
fi
DESTINATION_DIR=$(mktemp -d --tmpdir=$TEMP_DIR)
# Check if ENC_PASSWORD is set
if [ -z "$ENC_PASSWORD" ]; then
echo "Error: ENC_PASSWORD environment variable is not set."
exit 1
fi
# Check if source directory is provided as an argument
if [ "$#" -eq 0 ]; then
echo "Error: Please provide the source directory as an argument."
exit 1
fi
# Check if source directory exists
if [ ! -d "$SOURCE_DIR" ]; then
echo "Error: Source directory '$SOURCE_DIR' does not exist."
exit 1
fi
if [ "$SOURCE_DIR" == "." ]; then
SOURCE_DIR=$(pwd)
fi
SOURCE_DIR_NAME=$(basename "$SOURCE_DIR")
ARCHIVE_NAME="${SOURCE_DIR_NAME}"
# Use the provided archive name if the second parameter is present
if [ "$#" -eq 2 ]; then
ARCHIVE_NAME="$2"
fi
ARCHIVE_NAME="$(hostname)_${ARCHIVE_NAME}_${CURRENT_DATE_TIME}"
ARCHIVE_FILE="$DESTINATION_DIR/${ARCHIVE_NAME}.7z"
# Function to clean up temporary files on exit
cleanup() {
echo "Cleaning up at: $DESTINATION_DIR"
rm -rf "$DESTINATION_DIR"
}
echo "Temporary directory: $DESTINATION_DIR"
trap cleanup EXIT
# Archive, compress, and encrypt the directory
7z a -t7z -m0=lzma2 -mx=9 -mhe=on -p"$ENC_PASSWORD" "$ARCHIVE_FILE" "$SOURCE_DIR"
if [ $? -eq 0 ]; then
echo "Archiving successful. Beginning upload."
else
echo "Error ($?): Archiving failed. Check path permissions."
exit 1
fi
# Upload to OneDrive using rclone
rclone copy "$ARCHIVE_FILE" "$REMOTE_NAME:$REMOTE_DIR" -P
# Check if the upload was successful
if [ $? -eq 0 ]; then
echo "Upload successful. Cleaning up temporary files."
cleanup
else
echo "Error: Upload failed. Check your internet connection or OneDrive configuration."
exit 1
fi

3
downloads/scan-downloads.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
mkdir -p $HOME/.clamscan/
clamscan -r --remove --log=$HOME/.clamscan/$(date +"%Y%m%d-%H%M%S").log /mnt/data/downloads

95
downloads/sort-downloads.py Executable file
View File

@ -0,0 +1,95 @@
#!/usr/bin/env python
# - generated, gpt3.5
import os
import shutil
import sys
import datetime
from pathlib import Path
# Configurable variables
MOVIES_EXTENSION = {".mkv", ".mp4", ".avi"}
MUSIC_EXTENSION = {".mp3", ".ogg", ".flac", ".wma", ".wav"}
EPISODIC_THRESHOLD = 1 # Maximum number of video files to consider as episodic (inclusive)
MOVIES_PATH = "Movies"
MUSIC_PATH = "Music"
SHOWS_PATH = "Shows"
def organize_folders(directory, root_path):
log_file = create_log_file(directory, root_path)
write_log(log_file, f"Organizing folders in {directory}")
actions = preview_actions(directory, root_path)
if confirm_actions(actions):
execute_actions(actions, log_file)
else:
log_file.close()
os.remove(log_file.name)
print("Log file deleted.")
log_file_path = os.path.abspath(log_file.name)
print(f"Log file saved at: {log_file_path}")
def preview_actions(directory, root_path):
actions = []
for folder_name in os.listdir(directory):
folder_path = os.path.join(directory, folder_name)
if os.path.isdir(folder_path):
file_types = set()
for file in os.listdir(folder_path):
file_extension = Path(file).suffix.lower()
file_types.add(file_extension)
if MOVIES_EXTENSION.intersection(file_types) and len(file_types) <= EPISODIC_THRESHOLD:
actions.append((folder_path, os.path.join(root_path, MOVIES_PATH, folder_name)))
elif len(file_types.intersection(MUSIC_EXTENSION)) == len(file_types):
actions.append((folder_path, os.path.join(root_path, MUSIC_PATH, folder_name)))
elif len(file_types) > EPISODIC_THRESHOLD:
actions.append((folder_path, os.path.join(root_path, SHOWS_PATH, folder_name)))
return actions
def confirm_actions(actions):
print("Preview of actions:")
for source, destination in actions:
print(f"Move: {source} -> {destination}")
user_input = input("Do you want to proceed with the above actions? (y/n): ").lower()
return user_input == "y"
def execute_actions(actions, log_file):
for source, destination in actions:
message = f"Moving {os.path.basename(source)} to {destination}"
write_log(log_file, message)
print(message)
shutil.move(source, destination)
def move_folder(folder_path, destination):
if not os.path.exists(destination):
os.makedirs(destination)
shutil.move(folder_path, os.path.join(destination, os.path.basename(folder_path)))
def create_log_file(directory, root_path):
current_time = datetime.datetime.now().strftime("%y%m%d_%H%M%S")
log_file_name = f"{current_time}.log"
log_file_path = os.path.join(root_path, directory, log_file_name)
return open(log_file_path, "w")
def write_log(log_file, message):
timestamp = datetime.datetime.now().strftime("[%Y-%m-%d %H:%M:%S]")
log_message = f"{timestamp} {message}\n"
log_file.write(log_message)
print(log_message, end="")
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: ./organize_folders.py <directory_path> <root_path>")
sys.exit(1)
directory = sys.argv[1]
root_path = sys.argv[2]
organize_folders(directory, root_path)

15
snippets/human_readable_size.sh Executable file
View File

@ -0,0 +1,15 @@
human_readable_size() {
local bytes="$1"
local sizes=("B" "KB" "MB" "GB" "TB" "PB" "EB" "ZB" "YB")
local i=0
while ((bytes > 1024)); do
((bytes /= 1024))
((i++))
done
printf "%d%s" "$bytes" "${sizes[i]}"
}
human_readable_size $1
echo ""

9
snippets/sizetest.sh Executable file
View File

@ -0,0 +1,9 @@
folder_size=$(du -s /mnt/data/storage/ | awk '{print $1}')
available_space=$(df /tmp | awk 'NR==2 {print $4}')
echo $folder_size
echo $available_space
if [[ "$folder_size" -gt "$available_space" ]]; then
echo "Error: Folder size exceeds available space in $(realpath $DESTINATION_DIR/)."
else
echo "Folder size is within available space in $(realpath $DESTINATION_DIR/)."
fi

View File

@ -73,11 +73,17 @@ def download_file(file_url, download_dir, ytdlp_format):
rgx_booru = r'https?://[a-z.]+/(index\.php.*id=([0-9]+)|posts/([0-9]+))'
rgx_booru_v1 = r'(https?://.*/original/([A-Za-z0-9/_]*\.{0})|https?://img[a-z0-9.]+\.[a-z]+\.com/(.*?)))'.format(extensions)
rgx_booru_v2 = r'(https?://.*/original/([A-Za-z0-9/_]*\.{0})|https?://img[a-z0-9.]+\.[a-z]+\.com/im)'.format(extensions)
rgx_booru_v2_vid = r'https?://video-cdn[0-9]{1}.*booru.*\.com/images/.*\.mp4'
artist = None
if re.match(rgx_booru, file_url):
artist = find_booru_artist(file_url)
file_url = re.search(rgx_booru_v2, requests.get(file_url).text).group(1).strip()
booru_url = file_url
file_url = re.search(rgx_booru_v2, requests.get(booru_url).text)
file_url = file_url.group(1).strip() if file_url else None
if not file_url: #try video
file_url = re.search(rgx_booru_v2_vid, requests.get(booru_url).text)
file_url = file_url.group(0).strip() if file_url else None
if not file_url:
return 1