I made this little script for my own needs and works like a charm, so I decided to share it.. I hope it helps someone.
So, you got a linux/bsd box at home which you use as a media and storage server, but keeping it synchronized with the files on your seedbox is a nightmare. Wouldn't it be nice if your home box downloaded the latest files on the seedbox with a sinple fire-and-forget command, or even better: did it by itself during the day while you are at work, so when you get home all the goodies are avaiable? Well, read on then.
- A machine with any flavor of Linux or BSD (does anyone still use Solaris?). Hell, it should work even on Macs, provided it has lftp built for it installed. (duh)
Edit: I went googling for lftp on mac and found a great guide on how to install it on Linux and Mac right here in here in TI! Kudos to the hobbit! I cant test if the rest of the process will work on Mac as I typed without modification, for I lack a Mac here to test it.
- lftp: this is a small yet hugely powerful shell ftp client. It should come default with most installations, but if your machine lack it, there's plenty of tutorials around for how to compile it from source or install binary packages (hint: if are in fedora run yum install lftp as root)
- Just whip out your favorite shell text editor (should be vi, unless you are an emacs heathen) and type out the following script in any path of your choosing (for simplicity sake, I will assume from now on the path you are creating the script is /home/user, but it can be wherever you like):
Enter insert mode (press ' i' ), then paste the following code:
Obviously enough, the value of the variables at the start of the script should be replaced with your values. I trust they are self explanatory enough.
trap "rm -f /tmp/synctorrent.lock" SIGINT SIGTERM
if [ -e /tmp/synctorrent.lock ]
echo "Synctorrent is running already."
lftp -u $login,$pass $host << EOF
set ftp:ssl-allow no
set mirror:use-pget-n 5
mirror -c -P5 --log=synctorrents.log $remote_dir $local_dir
rm -f /tmp/synctorrent.lock
The important parameters i use for lftp are:
'set mirror:use-pget-n 5' this makes lftp try to split up fies in 5 pieces for parallel downloading. Likewise,
'-P5' means it will download at most 5 files in parallel (for a total 25 connections). Those 2 combined work wonders. In my case, I always end up downloading the files at the limit of my connection, but feel free to play with them and find what works best for you.
'-c ' just tells it to try and resume an interrupted download if it' s the case.
Ok, save it and close the editor (Esc, then :wq) and give it permission to run with:
There you go! You can test it running it from where you stand with ./synctorrents.sh and he will nicely log in your seedbox' s ftp, scan the directory you configured, compare with the local directory you configured and start downloading the new or changed files!
chmod +x synctorrents.sh
Now, the real nice thing about it is that you can have this bad boy in the crontab without worrying it will run over itself so go for it:
This will open vi to your crontab file. Add the line:
Save and close (Esc, then :wq) and you are done!
0 * * * * /home/user/synctorrents.sh >> /home/user/sync_cron.log 2>&1
This will make the script run at the 0 minute of every hour (it wont run over itself if the previous instance hasn't finished running yet) and let a logfile in case anything goes wrong. Of course you can adjust any parameter of the crontab entry to run it whenever and how often you like, just google for it ;)
You can make sure the line was really saved by checking the crontab tasks with
Enjoy and let me know if it was useful! ;)
By the way, if your seedbox is Linux and has lftp installed, with just a minor modification you could install a ftp server at your home (including windows) and have the seedbox pushing the files to you instead of you pulling them down. Let me know if a tutorial like that would be usefull and I will conjure up something ;)