Skip to content

Instantly share code, notes, and snippets.

@jfeilbach
Last active November 11, 2020 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfeilbach/b39715c83a3e0ab38152b108c6f08599 to your computer and use it in GitHub Desktop.
Save jfeilbach/b39715c83a3e0ab38152b108c6f08599 to your computer and use it in GitHub Desktop.
search for string e.g. tracker.example.com and move torrent via samba to new client
#!/bin/bash
NC='\033[0m' # No color
Green='\033[0;32m' # Green
White='\033[0;37m' # White
Red='\033[0;31m' # Red
Yellow='\033[0;33m' # Yellow
id=${1}
remote_host=uhura.local:9091
local_host=localhost:9091
cmd=/usr/bin/transmission-remote
counter='1'
dldir=/dl_1/
dest=/home/jason/xfer/
mnt1=/mnt/uhura_dl_1/
mnt2=/mnt/uhura_dl_2/
mnt3=/mnt/uhura_dl_1/sonarr/
mnt4=/mnt/uhura_dl_1/radarr/
count=$(${cmd} -l | grep 'Stopped' | wc -l)
check=$(${cmd} -l | grep 'Stopped' | awk '{ ORS=" " } ; { print $1 }')
hash=$(${cmd} ${remote_host} -t ${id} -i |grep 'Hash:' | sed 's/.*\:\ //')
name=$(${cmd} ${remote_host} -t ${id} -i |grep 'Name:' | sed 's/.*\:\ //')
copy_torrent_file () {
file=${hash}.torrent
cp -v ${mnt1}torrents/${file} ${dest}
}
add_torrent () {
file=${hash}.torrent
# echo -e "Adding ${White}${file}${NC}\n"
$cmd ${local_host} --add ${dest}/${file} --verify --no-trash-torrent --start-paused
echo ""
}
search_mnt () {
for m in $mnt1 $mnt2 $mnt3 $mnt4 ; do
search=${m}${name}
ls -d "${search}" > /dev/null 2>&1
if [ $? -eq 0 ]; then
export mnt=${m}
echo -e $(date '+%b %d %H:%M:%S') "$HOSTNAME Found ${name} in ${mnt}" >> /home/jason/migration.log
return 0
fi
done
if [ "$mnt" = "" ]; then
echo -e "${Red} Warning \$mnt variable is empty. Cound not find files in any mount points. Exiting.${NC}\n"
read -p $'\e[33mPress Enter to continue...\e[0m'
fi
}
move () {
src=${mnt}${name}
# echo -e "Will transfer from ${White}${src}${NC}\n"
rsync -avh --progress "${src}" ${dldir}
# rsync -avh --stats --progress "${src}" ${dldir}
}
stop () {
# echo -e "${Yellow}Stopping ${NC}${White}${name}${NC}..."
${cmd} ${remote_host} -t ${id} --stop
echo -e $(date '+%b %d %H:%M:%S') "$HOSTNAME Stopped ${name} on ${remote_host}" >> /home/jason/migration.log
}
log_migrate () {
echo -e $(date '+%b %d %H:%M:%S') "$HOSTNAME Started rsync of ${name} from ${remote_host}" >> /home/jason/migration.log
}
log_transfer () {
echo -e $(date '+%b %d %H:%M:%S') "$HOSTNAME Finished rsync of ${name}" >> /home/jason/migration.log
echo -e $(date '+%b %d %H:%M:%S') "$HOSTNAME Hash for ${name} is ${hash}" >> /home/jason/migration.log
}
log_add () {
echo -e $(date '+%b %d %H:%M:%S') "$HOSTNAME Added ${name} to Transmission" >> /home/jason/migration.log
}
log_start () {
echo -e $(date '+%b %d %H:%M:%S') "$HOSTNAME Started ${name} in Transmission" >> /home/jason/migration.log
}
echo -e "${Yellow}Hash:${NC} ${White}${hash}${NC}"
echo -e "${Yellow}Name:${NC} ${White}${name}${NC}"
# Start the migration
search_mnt "${name}"
log_migrate
move "${name}"
copy_torrent_file
log_transfer
add_torrent
stop
# Check for any torrents ready to be started
if [[ "${count}" = 0 ]]; then
# /bin/date
echo -e "\n${Yellow}Found ${NC}${White}${count}${NC}${Yellow} torrents ready to be started. Exiting.${NC}\n"
exit 1
else
echo -e "\n${Green}Found ${NC}${White}${count}${NC}${Green} torrents ready to be started...${NC}\n"
fi
#echo -e "\n${Yellow}Found ${NC}${White}${count}${NC}${Yellow} torrents ready to be started...${NC}\n"
for x in ${check} ; do
status=$(${cmd} -t ${x} -i | grep 'Percent Done: ' | awk '{ print $3 }')
name=$(${cmd} -t ${x} -i | grep 'Name: '| head -n 1 | sed 's/.*\:\ //' )
if [[ "${status}" != "100%" ]] ; then
echo -e "\n${Red}Warning torrent ${NC}${White}${x}${NC}${Red} not 100% downloaded. Will not start.${NC}\n"
# echo -e "${Red}Please verify ${White}${name}${NC}\n"
break
else
echo -e "${Green}Torrent ${White}${x}${NC}${Green} is 100% downloaded. Ready to start.${NC}"
fi
# echo -e "Starting torrent ${White}${name}${NC}"
${cmd} -t ${x} --start
# echo -e "Started ${White}${counter}${NC} of ${White}${count}${NC}\n"
log_start
((counter ++))
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment