Skip to content

Instantly share code, notes, and snippets.

@werkkrew
Created March 25, 2016 14:48
Show Gist options
  • Save werkkrew/b4b19a95239c66b03e20 to your computer and use it in GitHub Desktop.
Save werkkrew/b4b19a95239c66b03e20 to your computer and use it in GitHub Desktop.
Sync
#!/bin/bash
#### Seedbox Sync
#
# Sync various directories between home and seedbox, do some other things also.
#
# To use rsync, this script requires you have ssh key based logins with no passphrase
# set up between the user this script runs as and your seedbox.
logdir="/var/log/htpc"
xferlog="xferlog.log"
log="$logdir/$xferlog"
# folders where music/videos would be in, used in a grep pattern so separate folder names with a pipe |
# only use music/video folder names here you want beets/filebot to process, so if you don't want filebot to
# process your porn, dont list it here.
music_tags="music"
video_tags="movie|tv|anime"
host="foo.bar.gov"
user="username"
# leave empty if you use a key
pass=""
# Location of remote files ready for pickup, no trailing slash
remote_media="~/private/downloads/completed"
# Destination for remote media to be stored for further processing
local_media="/storage/downloads/seedbox"
# choices are rysnc or lftp
sync_method="lftp"
# Are there any blackhole torrent directories we want to sync?
# e.g. - tools like headphones that can't talk to rtorrent can drop torrent files somewhere, we can sync them up to a watch dir
sync_blackholes=false
local_blackhole="/foo/bar"
remote_watch="/remote/watch"
# Process video with filebot?
use_filebot=true
filebot_script="/usr/local/bin/filebot-process.sh"
# Process music with beets?
use_beets=true
beets_script="/usr/local/bin/beets-process.sh"
base_name="$(basename "$0")"
lock_file="/tmp/$base_name.lock"
trap "rm -f $lock_file" SIGINT SIGTERM
if [[ -e "$lock_file" ]]; then
echo "$base_name is running already."
exit
fi
touch "$lock_file"
if [[ -f $log ]]; then
mv $log $log.last
fi
if [[ "$sync_method" == "rsync" ]]; then
rsync -av --ignore-existing --remove-source-files --prune-empty-dirs \
--log-file=$log \
--log-file-format="%f - %n" \
${host}:${remote_media}/ \
${local_media}/
elif [[ "$sync_method" == "lftp" ]]; then
lftp -p 22 -u ${user},${pass} sftp://"$host" << LFTP
set xfer:log true
set xfer:log-file "$log"
set sftp:auto-confirm yes
set mirror:use-pget-n 10
mirror -n -c -P10 --Remove-source-files "$remote_media" "$local_media"
quit
LFTP
else
echo "No valid sync method chosen"
fi
music=1
video=1
# See what got transferred
if [[ -f $log ]]; then
# log file exists, so something got transferred
music=$( cat $log | grep -qE "${music_tags}" )
video=$( cat $log | grep -qE "${video_tags}" )
fi
if [[ "$sync_blackholes" == true ]]; then
rsync -av --ignore-existing --remove-source-files \
${local_blackhole}/ \
${host}:${remote_watch}/
fi
if [[ "$use_filebot" == true && $video -eq 0 ]]; then
eval $filebot_script
fi
if [[ "$use_beets" == true && $music -eq 0 ]]; then
eval $beets_script
fi
rm -f "$lock_file"
trap - SIGINT SIGTERM
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment