torrent/install.sh
2025-02-28 10:07:04 +01:00

162 lines
4.2 KiB
Bash

#!/bin/bash
set -e
# Git repository configuration
GIT_REPO="http://192.168.0.236:3000/masterdraco/torrent"
INSTALL_DIR="/tmp/torrent-install"
# Check root privileges
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi
# Install dependencies
echo "Checking dependencies..."
declare -A PKGS=(
[transmission-cli]="transmission-remote"
[unrar]="unrar"
[unzip]="unzip"
[p7zip-full]="7z"
[parallel]="parallel"
[bc]="bc"
[git]="git"
[logrotate]="logrotate"
)
for pkg in "${!PKGS[@]}"; do
if ! command -v "${PKGS[$pkg]}" &> /dev/null; then
echo "Installing $pkg..."
apt-get update
apt-get install -y "$pkg"
fi
done
# Get files from Repo
echo "Getting latest files from repository..."
if [ -d "$INSTALL_DIR" ]; then
cd "$INSTALL_DIR"
git fetch
git reset --hard origin/main
else
mkdir -p "$INSTALL_DIR"
git clone "$GIT_REPO" "$INSTALL_DIR"
cd "$INSTALL_DIR"
fi
# Create directory structure
echo "Creating directory structure..."
mkdir -p /etc/torrent
mkdir -p /usr/local/bin
mkdir -p /usr/local/lib/torrent-mover
mkdir -p /var/lib/torrent
mkdir -p /var/log/torrent
mkdir -p /etc/systemd/system
# Create dedicated user for security
TORRENT_USER="torrent-mover"
TORRENT_GROUP="torrent-mover"
# Check if user exists and create if not
if ! id "$TORRENT_USER" &>/dev/null; then
echo "Creating dedicated $TORRENT_USER user for security..."
useradd -r -s /bin/false "$TORRENT_USER"
fi
# Install files
echo "Installing files..."
install -Dm644 etc/torrent/mover.conf /etc/torrent/mover.conf.new
install -Dm755 usr/local/bin/torrent-mover /usr/local/bin/torrent-mover
install -Dm755 usr/local/bin/torrent-config /usr/local/bin/torrent-config
# Install library modules
for module in usr/local/lib/torrent-mover/*.sh; do
if [ -f "$module" ]; then
install -Dm755 "$module" "/usr/local/lib/torrent-mover/$(basename "$module")"
fi
done
# Create backup directory for configuration files
mkdir -p /etc/torrent/backups
chown $TORRENT_USER:$TORRENT_GROUP /etc/torrent/backups
# If this is a first-time install, copy the default config
if [ ! -f "/etc/torrent/mover.conf" ]; then
mv /etc/torrent/mover.conf.new /etc/torrent/mover.conf
else
echo "Existing configuration found at /etc/torrent/mover.conf"
echo "New configuration is at /etc/torrent/mover.conf.new"
fi
# Create log rotation configuration
cat > /etc/logrotate.d/torrent-mover << EOF
/var/log/torrent_mover.log /var/log/torrent_processed.log {
weekly
rotate 4
compress
delaycompress
missingok
notifempty
create 0640 $TORRENT_USER $TORRENT_GROUP
}
EOF
# Create systemd service
cat > /etc/systemd/system/torrent-mover.service << EOF
[Unit]
Description=Torrent Mover Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/torrent-mover
Restart=on-failure
RestartSec=60
User=$TORRENT_USER
Group=$TORRENT_GROUP
[Install]
WantedBy=multi-user.target
EOF
# Create systemd timer for periodic execution
cat > /etc/systemd/system/torrent-mover.timer << EOF
[Unit]
Description=Run Torrent Mover every 15 minutes
[Timer]
OnBootSec=5min
OnUnitActiveSec=15min
AccuracySec=1min
[Install]
WantedBy=timers.target
EOF
# Set permissions
echo "Setting permissions..."
chmod 600 /etc/torrent/mover.conf*
chown root:root /etc/torrent/mover.conf*
chmod 644 /etc/systemd/system/torrent-mover.service
chmod 644 /etc/systemd/system/torrent-mover.timer
# Set permissions for data directories
chown $TORRENT_USER:$TORRENT_GROUP /var/lib/torrent
chmod 755 /var/lib/torrent
touch /var/log/torrent_mover.log /var/log/torrent_processed.log
chown $TORRENT_USER:$TORRENT_GROUP /var/log/torrent_mover.log /var/log/torrent_processed.log
chmod 640 /var/log/torrent_mover.log /var/log/torrent_processed.log
# Ensure torrent-mover user can access required directories
echo "Setting up group memberships..."
if getent group debian-transmission >/dev/null; then
usermod -a -G debian-transmission $TORRENT_USER
echo "Added $TORRENT_USER to debian-transmission group"
fi
# Reload systemd and enable timer
systemctl daemon-reload
echo "To enable automatic execution every 15 minutes, run:"
echo " systemctl enable --now torrent-mover.timer"
echo
echo "Installation complete!"