diff --git a/create_torrent.sh b/create_torrent.sh new file mode 100644 index 0000000..7b086c4 --- /dev/null +++ b/create_torrent.sh @@ -0,0 +1,200 @@ +#!/bin/bash + +# Configuration +SOURCE_DIR="/mnt/zpool0_nfs/cios_www" +DEST_DIR="/home/paulmataruso/TORRENT_BACKUP_GOLD" +TORRENT_WATCH_DIR="/home/paulmataruso/TORRENT_WATCH" +TRACKER_URL="http://tracker.dhitechnical.com:6969/announce" + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# Function to print colored output +print_status() { + echo -e "${GREEN}[INFO]${NC} $1" +} + +print_warning() { + echo -e "${YELLOW}[WARNING]${NC} $1" +} + +print_error() { + echo -e "${RED}[ERROR]${NC} $1" +} + +print_header() { + echo -e "${BLUE}================================${NC}" + echo -e "${BLUE}$1${NC}" + echo -e "${BLUE}================================${NC}" +} + +# Check if transmission-create is installed +check_dependencies() { + if ! command -v transmission-create &> /dev/null; then + print_error "transmission-create is not installed. Please install transmission-cli package." + print_status "On Ubuntu/Debian: sudo apt-get install transmission-cli" + print_status "On CentOS/RHEL: sudo yum install transmission-cli" + print_status "On Arch: sudo pacman -S transmission-cli" + exit 1 + fi +} + +# Validate destination directories exist +check_dest_dirs() { + if [ ! -d "$DEST_DIR" ]; then + print_error "Destination directory does not exist: $DEST_DIR" + exit 1 + fi + + if [ ! -d "$TORRENT_WATCH_DIR" ]; then + print_error "Torrent watch directory does not exist: $TORRENT_WATCH_DIR" + exit 1 + fi +} + +# Calculate optimal piece size based on folder size +calculate_piece_size() { + local folder_path="$1" + local size_bytes=$(du -sb "$folder_path" | cut -f1) + + # Convert to MB for easier comparison + local size_mb=$((size_bytes / 1024 / 1024)) + + # Piece size mapping based on folder size + # < 100 MB: 16 KB pieces + # 100 MB - 1 GB: 32 KB pieces + # 1 GB - 10 GB: 64 KB pieces + # 10 GB - 100 GB: 128 KB pieces + # > 100 GB: 256 KB pieces + + if [ $size_mb -lt 100 ]; then + echo "16" + elif [ $size_mb -lt 1024 ]; then + echo "32" + elif [ $size_mb -lt 10240 ]; then + echo "64" + elif [ $size_mb -lt 102400 ]; then + echo "128" + else + echo "256" + fi +} + +# Format size for display +format_size() { + local size_bytes="$1" + if [ $size_bytes -gt 1073741824 ]; then + echo "$(($size_bytes / 1073741824)) GB" + elif [ $size_bytes -gt 1048576 ]; then + echo "$(($size_bytes / 1048576)) MB" + elif [ $size_bytes -gt 1024 ]; then + echo "$(($size_bytes / 1024)) KB" + else + echo "$size_bytes bytes" + fi +} + +# Create torrent file for a single folder +create_torrent() { + local folder_path="$1" + local folder_name=$(basename "$folder_path") + local torrent_file="$DEST_DIR/${folder_name}.torrent" + + # Calculate folder size and optimal piece size + local size_bytes=$(du -sb "$folder_path" | cut -f1) + local size_formatted=$(format_size $size_bytes) + local piece_size=$(calculate_piece_size "$folder_path") + + print_status "Creating torrent for: $folder_name (Size: $size_formatted, Piece size: ${piece_size}KB)" + + # Create torrent file + transmission-create \ + --tracker "$TRACKER_URL" \ + --outfile "$torrent_file" \ + --piece-size "$piece_size" \ + --comment "Created by torrent creation script - Piece size: ${piece_size}KB" \ + "$folder_path" + + if [ $? -eq 0 ]; then + print_status "✓ Torrent created: $torrent_file" + + # Copy torrent file to watch directory + local watch_torrent_file="$TORRENT_WATCH_DIR/${folder_name}.torrent" + cp "$torrent_file" "$watch_torrent_file" + chown 1000:1000 "$watch_torrent_file" + print_status "✓ Torrent copied to watch directory: $watch_torrent_file" + + # Extract magnet link + local magnet_link=$(transmission-show --magnet "$torrent_file") + if [ $? -eq 0 ]; then + local magnet_file="$DEST_DIR/${folder_name}.magnet" + echo "$magnet_link" > "$magnet_file" + print_status "✓ Magnet link saved: $magnet_file" + else + print_warning "Could not extract magnet link for: $folder_name" + fi + else + print_error "Failed to create torrent for: $folder_name" + fi +} + +# Main function +main() { + print_header "Torrent Creation Script" + + # Check dependencies + check_dependencies + + # Validate source directory + if [ ! -d "$SOURCE_DIR" ]; then + print_error "Source directory does not exist: $SOURCE_DIR" + exit 1 + fi + + print_status "Source directory: $SOURCE_DIR" + print_status "Destination directory: $DEST_DIR" + print_status "Torrent watch directory: $TORRENT_WATCH_DIR" + print_status "Tracker URL: $TRACKER_URL" + + # Validate destination directories + check_dest_dirs + + # Get list of folders in source directory + local folders=($(find "$SOURCE_DIR" -maxdepth 1 -type d -not -name "$(basename "$SOURCE_DIR")")) + + if [ ${#folders[@]} -eq 0 ]; then + print_warning "No folders found in source directory" + exit 0 + fi + + print_status "Found ${#folders[@]} folders to process" + + # Process each folder + local count=0 + for folder in "${folders[@]}"; do + count=$((count + 1)) + print_status "Processing folder $count of ${#folders[@]}: $(basename "$folder")" + create_torrent "$folder" + echo + done + + print_header "Summary" + print_status "Processed ${#folders[@]} folders" + print_status "Torrent files saved to: $DEST_DIR" + print_status "Torrent files copied to: $TORRENT_WATCH_DIR" + print_status "Magnet files saved to: $DEST_DIR" + + # List created files + echo + print_status "Created files:" + ls -la "$DEST_DIR"/*.torrent 2>/dev/null | wc -l | xargs echo " Torrent files in backup:" + ls -la "$TORRENT_WATCH_DIR"/*.torrent 2>/dev/null | wc -l | xargs echo " Torrent files in watch:" + ls -la "$DEST_DIR"/*.magnet 2>/dev/null | wc -l | xargs echo " Magnet files:" +} + +# Run main function +main "$@"