본문 바로가기
서버/리눅스 서버

트랜스미션 다운로드 완료 후 자동 구글 드라이브 업로드 스크립트

by ㅋㅋ잠자 2019. 1. 15.
반응형

안녕하세요. 


토렌트 다운로드가 완료되면 자동으로 업로드 하는 스크립트를 작성해 보았습니다.





1. plexdrive + rclone + unionfs


참고 게시글 : https://blog.djjproject.com/261


#!/bin/sh

cd /root


# plexdrive

VOLUME=/media/volume1

umount -l $VOLUME/plexdrive


nohup plexdrive mount -c /etc/plexdrive --cache-file=/etc/plexdrive/cache.bolt \

--uid=0 --gid=100 -o allow_other,read_only \

--chunk-check-threads=16 --chunk-load-ahead=16 --chunk-load-threads=16 --chunk-size=1M --max-chunks=512 \

$VOLUME/plexdrive/ &


# union-fs

sleep 30

mkdir $VOLUME/temp/downloads

chmod a+rwx $VOLUME/temp/downloads

unionfs-fuse -o allow_other,cow,uid=0,gid=100,umask=0 \

$VOLUME/temp/downloads=RW:$VOLUME/plexdrive/104.server/001.odroidh2/downloads=RO $VOLUME/downloads


1. plexdrive 마운트 

/media/volume1/plexdrive 에 plexdrive 를 이용한 구글 드라이브 마운트


2. union-fs 로 폴더 2개 짬뽕

/media/volume1/temp/downloads (RW) 

/media/volume1/plexdrive/104.server/001.odroidh2/downloads (RO)


----> /media/volume1/downloads 로 짬뽕


/media/volume1/downloads 에 plexdrive의 파일들이 보임

/media/volume1/downloads 에 파일을 직접 쓰게 되면 /media/volume1/temp/downloads 에 저장됨

/media/volume1/downloads 에 /media/volume1/temp/downloads 의 내용도 보임


이해하시기 어렵겠지만 굉장히 쉽게 작성하였습니다.





2. 트랜스미션 설정


{

    "alt-speed-down": 50,

    "alt-speed-enabled": false,

    "alt-speed-time-begin": 540,

    "alt-speed-time-day": 127,

    "alt-speed-time-enabled": false,

    "alt-speed-time-end": 1020,

    "alt-speed-up": 50,

    "bind-address-ipv4": "0.0.0.0",

    "bind-address-ipv6": "::",

    "blocklist-enabled": false,

    "blocklist-url": "http://www.example.com/blocklist",

    "cache-size-mb": 4,

    "dht-enabled": true,

    "download-dir": "/media/volume1/downloads",

    "download-limit": 100,

    "download-limit-enabled": 0,

    "download-queue-enabled": true,

    "download-queue-size": 5,

    "encryption": 1,

    "idle-seeding-limit": 30,

    "idle-seeding-limit-enabled": false,


# 미완료 토렌트 파일들이 구글 드라이브에 업로드 되는 것을 방지

    "incomplete-dir": "/media/volume1/temp/incomplete_torrent",

    "incomplete-dir-enabled": true,

    "lpd-enabled": false,

    "max-peers-global": 200,

    "message-level": 1,

    "peer-congestion-algorithm": "",

    "peer-id-ttl-hours": 6,

    "peer-limit-global": 200,

    "peer-limit-per-torrent": 50,

    "peer-port": 51413,

    "peer-port-random-high": 65535,

    "peer-port-random-low": 49152,

    "peer-port-random-on-start": false,

    "peer-socket-tos": "default",

    "pex-enabled": true,

    "port-forwarding-enabled": false,

    "preallocation": 1,

    "prefetch-enabled": true,

    "queue-stalled-enabled": true,

    "queue-stalled-minutes": 30,

    "ratio-limit": 0,

    "ratio-limit-enabled": true,

    "rename-partial-files": true,

    "rpc-authentication-required": true,

    "rpc-bind-address": "0.0.0.0",

    "rpc-enabled": true,

    "rpc-host-whitelist": "",

    "rpc-host-whitelist-enabled": true,

    "rpc-password": "{934817e47cae1a6af0ddc6a57ee35075ca745534h8zOExYK",

    "rpc-port": 9091,

    "rpc-url": "/transmission/",

    "rpc-username": "none",

    "rpc-whitelist": "127.0.0.1",

    "rpc-whitelist-enabled": false,

    "scrape-paused-torrents-enabled": true,

# 토렌트 파일이 완료되면 업로드 스크립트 실행

    "script-torrent-done-enabled": true,

    "script-torrent-done-filename": "/home/rclone/download.sh",

    "seed-queue-enabled": false,

    "seed-queue-size": 10,

    "speed-limit-down": 100,

    "speed-limit-down-enabled": false,

    "speed-limit-up": 100,

    "speed-limit-up-enabled": false,

    "start-added-torrents": true,

    "trash-original-torrent-files": false,

    "umask": 0,

    "upload-limit": 100,

    "upload-limit-enabled": 0,

    "upload-slots-per-torrent": 14,

    "utp-enabled": true

}


자 그럼 다운로드가 완료된 다음 스크립트를 작성해 보겠습니다.


#!/bin/bash

# RCLONE UPLOAD CRON TAB SCRIPT 

# chmod a+x /home/plex/scripts/rclone-upload.sh

# Type crontab -e and add line below (without #) and with correct path to the script

# * * * * * /home/plex/scripts/rclone-upload.sh >/dev/null 2>&1

# if you use custom config path add line bellow in line 20 after --log-file=$LOGFILE 

# --config=/path/rclone.conf (config file location)


# 이 쉘 스크립트가 실행중일 경우 중복 실행을 방지하는 if 문입니다.

if pidof -o %PPID -x "$0"; then

   exit 1

fi


sleep 5


LOGFILE="/var/log/rclone-upload.log"

FROM="/media/volume1/temp/downloads"

TO="gdrive:/104.server/001.odroidh2/downloads"


# CHECK FOR FILES IN FROM FOLDER THAT ARE OLDER THAN 15 MINUTES

if find $FROM* -type f | read

  then

  start=$(date +'%s')

  echo "$(date "+%d.%m.%Y %T") RCLONE UPLOAD STARTED" | tee -a $LOGFILE

  rclone move "$FROM" "$TO" --transfers=20 --checkers=20 --delete-after --log-file=$LOGFILE

  echo "$(date "+%d.%m.%Y %T") RCLONE UPLOAD FINISHED IN $(($(date +'%s') - $start)) SECONDS" | tee -a $LOGFILE

fi

exit


상기 스크립트는 원본과 달리 시간 옵션이 제외 되었습니다. 이유는 이미 완료된 파일들만 temp 폴더에서 이동되기 때문입니다.


한편, 중복 실행을 방지하게 되면 토렌트 파일이 여러개 다운중일 경우 몇몇 파일은 업로드가 안될 수 있습니다.


이런 문제로 인하여, 중복 실행 방지 구문을 없애 보았지만, 동일 파일이 여러번 업로드 되는 문제가 생겼습니다.



그렇기 때문에 10분마다 crontab 을 돌려서 해결하기로 하였습니다. 물론 파일을 리스팅하면서 하드디스크가 깨어나는 문제는 어떻게 해결할 방도가 없네요.


# Edit this file to introduce tasks to be run by cron.

#

# Each task to run has to be defined through a single line

# indicating with different fields when the task will be run

# and what command to run for the task

#

# To define the time you can provide concrete values for

# minute (m), hour (h), day of month (dom), month (mon),

# and day of week (dow) or use '*' in these fields (for 'any').#

# Notice that tasks will be started based on the cron's system

# daemon's notion of time and timezones.

#

# Output of the crontab jobs (including errors) is sent through

# email to the user the crontab file belongs to (unless redirected).

#

# For example, you can run a backup of all your user accounts

# at 5 a.m every week with:

# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

#

# For more information see the manual pages of crontab(5) and cron(8)

#

# m h  dom mon dow   command

0 * * * * /home/rclone/upload.sh > /dev/null

*/30 * * * * /home/port.sh > /dev/null

59 23 * * * su root -c "service odroidlcd stop" > /dev/null

0 6 * * * su root -c "service odroidlcd start" > /dev/null

*/10 * * * * /home/rclone/download.sh > /dev/null


여기까지 마치도록 하겠습니다.


좋은 아이디어 있으시면 덧글 주시길 바랍니다.


반응형

댓글