Tag: bash

Seedbox autoupload to google drive with rclone

If you are an avid seedbox user, here is another option for automatic uploads to google drive.
There are many ways of achieving this, including a cronjob.
I like this option, as it only runs rclone when a torrent completes.

Using inotifywait to watch for file system events, we look out for primarily the ‘move’ event.
When your torrent completes, it moves the completed torrent into the completed folder.
When this event takes place, when then execute rcopy.sh

Setup rclone and scripts

mkdir ~/bin
cd ~/bin
wget https://downloads.rclone.org/rclone-v1.38-linux-amd64.zip
unzup rclone-v1.38-linux-amd64.zip
mv rclone*/* .

rscreen.sh

#!/bin/bash -x

ScSess=$(screen -ls)

if [[ $ScSess == *"No Sockets found"* ]]; then
  killall -9 rclone
  rm -rf ~/bin/rcopy.sh.lock
  screen -dmS rwait ~/bin/rwait.sh 
else
  exit
fi

rwait.sh

#!/bin/bash -x

COMPLETED_TORRENTS=~/private/deluge/completed/

while true; do
  inotifywait -r -e modify,attrib,move,close_write,create,delete,delete_self $COMPLETED_TORRENTS
  ~/bin/rcopy.sh
done

rcopy.sh

#!/bin/bash -x
#!/bin/bash -x

COMPLETED_TORRENTS=~/private/deluge/completed/

if [ -f "~/bin/rcopy.sh.lock" ]; then
  exit
else
  touch ~/bin/rcopy.sh.lock
  ~/bin/rclone --config=~/.config/rclone/rclone.conf --verbose --transfers=8 copy "$COMPLETED_TORRENTS" "google:/unsorted/"
  rm -f ~/bin/rcopy.sh.lock
fi

Permissions and PATH

Once you have setup the 3 scripts, make them executable

Permissions

chmod +x ~/bin/*

If ~/bin is not in your path, then add it and reload your shell

Edit Path

echo "PATH=~/bin:\$PATH" >> ~/.bashrc
source ~/.bashrc

Finally you run the main script in a cronjob

Finalize and run in the background

Screen session

*/1 * * * * /media/sdf1/feeditout/bin/rscreen.sh >/dev/null 2>&1

You can read more about inotifywait and rclone here
https://linux.die.net/man/1/inotifywait
https://rclone.org/


Synology Filebot Autonaming cron job

Having a synology NAS is great. However when dealing with 32tb, good file management is a must!
I have 2 primary folders, Films and Series. The set of below scripts iterate the files and use the TVDB and MovieDB to clean up the file names.
The second scripts downloads any missing subtitles for the media 🙂

File renaming

&1" ) );

foreach( $loglines as $logline )
{
  $line = trim( $logline );

  if( $line == "" ) continue;
  if( preg_match( "/^Skipped.*$/", $line, $dontcare ) ) continue;
  if( preg_match( "/^Auto-detect movie from context.*$/", $line, $dontcare ) ) continue;
  if( preg_match( "/.*Rename movies using.*$/", $line, $dontcare ) ) continue;

  $ploglines[] = $line;
}

$frt = implode( "\r\n" , $ploglines );

$body = "Dear user,\n\n $frt \n\nSincerely,\nSynology DiskStation\n\n";
mail($email, 'DSM - Filebot Rename Report - Films', "$body");


$log = "/volume1/homes/admin/cleanup/report-rename.txt";
unlink( $log );
touch( $log );

if ($handle = opendir('/volume1/Entertainment/Series/'))
{
  while (false !== ($entry = readdir($handle)))
  {
    if ($entry != "." && $entry != "..")
    { 
      $cmd = "filebot -r -rename \"/volume1/Entertainment/Series/$entry/\" --db TheTVDB 2>&1";
      $frt = shell_exec( $cmd );
      file_put_contents( $log, $frt . "\n\n", FILE_APPEND | LOCK_EX );
    }
  }
  closedir($handle);
}

$ploglines = array();
$loglines = file( $log );

foreach( $loglines as $logline )
{
  $line = trim( $logline );

  if( $line == "" ) continue;
  if( preg_match( "/'^Skipped.*$/", $line, $dontcare ) ) continue;
  if( preg_match( "/^Fetching episode data.*$/", $line, $dontcare ) ) continue;
  if( preg_match( "/^Processed.$*/", $line, $dontcare ) ) continue;
  if( preg_match( "/^Done.*$/", $line, $dontcare ) ) continue;
  if( preg_match( "/^Failure.*$/", $line, $dontcare ) ) continue;
  if( preg_match( "/^No media files.*$/", $line, $dontcare ) ) continue;

  $ploglines[] = $line;
}

$frt = implode( "\r\n" , $ploglines );

$body = "Dear user,\n\n $frt \n\nSincerely,\nSynology DiskStation\n\n";
mail($email, "DSM - Filebot Rename Report - Series $entry", "$body");

Subtitles scripts

&1" );
$body = "Dear user,\n\n $fst \n\nSincerely,\nSynology DiskStation\n\n";
mail($email, 'DSM - Filebot Subtitles Report - Films', "$body");

if ($handle = opendir('/volume1/Entertainment/Series/'))
{
  while (false !== ($entry = readdir($handle)))
  {
    if ($entry != "." && $entry != "..")
    { 
      $cmd = "filebot -r -script fn:suball \"/volume1/Entertainment/Series/$entry/\" --lang en -non-strict --db TheTVDB 2>&1";
      $frt = shell_exec( $cmd );
      file_put_contents( $log, $frt . "\n\n", FILE_APPEND | LOCK_EX );
    }
  }
  closedir($handle);
}

$frt = file_get_contents( $log );
$body = "Dear user,\n\n $frt \n\nSincerely,\nSynology DiskStation\n\n";
mail('dave@fio.ie', "DSM - Filebot Subtitles Report - Series $entry", "$body");

Cron Job

ash-4.3# cat /etc/crontab 
MAILTO=""
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/syno/sbin:/usr/syno/bin:/usr/local/sbin:/usr/local/bin
#minute hour    mday    month   wday    who     command
0       0       1       *       *       root    /usr/syno/bin/syno_disk_health_record
0       0       *       *       3       root    /usr/bin/php /var/services/homes/admin/cleanup/filebot-rename.php
0       0       *       *       5       root    /usr/bin/php /var/services/homes/admin/cleanup/filebot-subtitles.php
5       3       *       *       6       root    /usr/syno/bin/synomyds --report_info
0       3       *       *       1       root    /tmp/synoschedtask --run id=1
0       3       13      *       *       root    /tmp/synoschedtask --run id=2
11      2       *       *       4       root    /tmp/synoschedtask --run id=3

Linux Bash One Liners

I’m going to keep adding a number of one liners here. Mainly for my own personal usage.

Find duplicate filenames with different extensions

find . -type f -print | rev | cut -f 2- -d '.' | rev | sort | uniq -d

Mail War

Randomly spams someones with number of domains and random user.
This particular guy mark.silberman78@gmail.com thought i needed some extra mail.  I sent some back 😉

export de1=fio.ie
export de2=feeditout.com
export de3=feeditout.com

while true; 
  do export rand=$((1 + RANDOM % 3)); 
  export var=de$rand; 
  echo "fuck off with your spam" | mailx -r `tr -dc A-Za-z0-9 </dev/urandom | head -c 10`@$(eval echo \$$var) -s "fuck you, stop spamming my email address" mark.silberman78@gmail.com; 
  sleep 1; 
done