Skip to content

Instantly share code, notes, and snippets.

@Blackdixxa
Last active June 13, 2018 08:26
Show Gist options
  • Select an option

  • Save Blackdixxa/95f8d5746f6224fa275fd2a36f441912 to your computer and use it in GitHub Desktop.

Select an option

Save Blackdixxa/95f8d5746f6224fa275fd2a36f441912 to your computer and use it in GitHub Desktop.
torrentsync
#!/bin/sh
# remote mono-directional sync v2.4
# note: the first time this script runs, nothing append
# because the script initializes the last exec to now.
# configuration parameters
# remote folder to sync to (the source)
REMOTE_FOLDER=''
# remote host and username to id against
REMOTE_URI=''
# remote ssh port if different from the standard
REMOTE_PORT=2222
# local folder (the destination)
TEMP_URI='/cygdrive/z/radarr/incomplete/'
# local folder (the destination to be consumed by radarr/sonarr)
LOCAL_URI='/cygdrive/z/radarr/complete/'
# file containing the last sync date
LAST_EXECUTION='/home/Blackdixxa/last-remote-user-sync'
# the ssh private key to ident to the remote host
REMOTE_KEY='/home/Blackdixxa/.ssh/id_rsa'
# private variables -- DO NOT MODIFY
#qnap compatibility: TMP_FILES_TO_SYNC=$(mktemp /tmp/rsync.XXXXXX)
TMP_FILES_TO_SYNC=$(mktemp -p /tmp rsync.XXXXXX)
LAST_EXECUTION_FOLDER=$(dirname $LAST_EXECUTION)
RSYNC_CMD="rsync -4 -hzzrvRc --no-p --chmod=ugo=rwX --partial --progress"
#RSYNC_CMD="rsync -4 -hrvR -z --partial --progress -e 'ssh -p $REMOTE_PORT'"
# fuck you rsync
echo "#!/bin/sh
exec ssh -Tp $REMOTE_PORT \$@" > /usr/local/bin/ssh-rsync
chmod +x /usr/local/bin/ssh-rsync
pushd `dirname $0`
# beginning of the script
if [ ! -d "$LAST_EXECUTION_FOLDER" ]; then
mkdir -p "$LAST_EXECUTION_FOLDER"
date '+%Y-%m-%d %H:%M:%S' > $LAST_EXECUTION
fi
# initializing ssh-agent to connect to the remote
eval $(ssh-agent)
ssh-add "$REMOTE_KEY"
# cleanup
trap '[ -e /proc/$SSH_AGENT_PID ] && kill $SSH_AGENT_PID; [ -f "$TMP_FILES_TO_SYNC" ] && rm "$TMP_FILES_TO_SYNC"' 0 2 3 15
LAST_EXEC=$(cat $LAST_EXECUTION)
ssh -p $REMOTE_PORT "$REMOTE_URI" "cd ${REMOTE_FOLDER};find . -daystart -maxdepth 1 -mindepth 1 -newermt '$LAST_EXEC' -print" > $TMP_FILES_TO_SYNC
NEW_LAST_EXEC=$(date '+%Y-%m-%d %H:%M:%S')
$RSYNC_CMD --files-from=$TMP_FILES_TO_SYNC "${REMOTE_URI}:${REMOTE_FOLDER}" "$TEMP_URI"
ret=$?
while [ $ret != 0 ]; do
$RSYNC_CMD --files-from=$TMP_FILES_TO_SYNC "${REMOTE_URI}:${REMOTE_FOLDER}" "$TEMP_URI"
ret=$?
done
cd "$TEMP_URI"; attrib -h /s /d
mv "$TEMP_URI"/* "$LOCAL_URI"
# store last execution
[ "$ret" -eq '0' ] && echo $NEW_LAST_EXEC > $LAST_EXECUTION
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment