Skip to content

Instantly share code, notes, and snippets.

@rlogiacco
Last active November 11, 2016 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlogiacco/c18bcfb8a92cbe65d923094a30e54b17 to your computer and use it in GitHub Desktop.
Save rlogiacco/c18bcfb8a92cbe65d923094a30e54b17 to your computer and use it in GitHub Desktop.
Keep movies folder synchronized
#!/bin/bash
exec 5>&1
SHARE="/mnt/TORRENT/completed"
LOCAL="/media/STORAGE/@temp"
FILES="/media/STORAGE/@new"
TIMESTAMP="$(date '+%d %h %y %H:%M:%S')"
LOCK="rsync-completed.lock"
# acquire lock
if ! mkdir /tmp/$LOCK; then
echo "[$TIMESTAMP] Failed to aquire lock"
exit 1
fi
trap 'rm -rf /tmp/$LOCK' EXIT # remove the lockdir on exit
# mount and check
#mount $SHARE
if [ ! -e $SHARE ]; then
echo "[$TIMESTAMP] Shared folder unavailable"
exit 1
elif [ ! -e $LOCAL ]; then
echo "[$TIMESTAMP] Local folder unavailable"
exit 1
else
START=$(date +%s)
# remote copy
echo "[$TIMESTAMP] File sync in progress..."
log=$(/usr/bin/rsync -ai --delete-during --ignore-existing $SHARE/ $LOCAL/ | tee -a /dev/fd/5)
# hardlinks
echo "[$TIMESTAMP] Creating hardlinks..."
echo "$log" | grep '>f+++++++++' | cut -c 13- | while read file; do
target=$(sed -e 's/ /_/g' <<< $(basename "$file"))
echo "hardlink $file --> $target"
ln "$LOCAL/$file" "$FILES/$target"
done
FINISH=$(date +%s)
echo "[$TIMESTAMP] total time: $(( ($FINISH-$START) / 60 )) minutes, $(( ($FINISH-$START) % 60 )) seconds" | tee $1/"Backup from $(date '+%Y-%m-%d, %T, %A')"
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment