Last active
November 11, 2016 22:24
-
-
Save rlogiacco/c18bcfb8a92cbe65d923094a30e54b17 to your computer and use it in GitHub Desktop.
Keep movies folder synchronized
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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