Copy newer files than last sync to Tablet


I download TV shows. Technically it’s a crime but they have already aired so I feel justified that I download them for catch up. The problem is that I never keep right up with a show and end up trying to work out which one I last watched. I also commute a lot, so I have a tablet (yes, it’s a BlackBerry Playbook, but it does the job) that I watch the shows on.

My issue comes from trying to get the right shows on to my tablet so the computer knows which ones to copy, which ones have already been on it before and which ones to ignore. The best way to do this, I have found, is to mark a file with a date from last sync and only copy those files to the tablet. I also need to detect if the tablet is on the network and mount it. The below script is pretty crude but it does the job. The only thing it doesn’t do is work out if I will have enough space on the tablet to take the files it’s copying. I may add to it further down the line (doubtful) but for now, here is the script:

 

#!/bin/bash

# Syncs TV shows to device based on time
#
synctimefile=~/.lastsynctime
deviceip=192.168.0.11
pingtest=$(ping $deviceip -c1 | grep "1 received" | wc -l)
smbuser=playbook
smbpass=qpqp
smbdomain=WORKGROUP
smbmount=/mnt/smb
smbtarget=media
remotemediadir=videos
srclist="/mnt/goodies/TV Series"

# Are we root or do we need a sudo for mount?
SUDO=''
if (( $EUID != 0 )); then
    SUDO='sudo'
fi


# Test if device is online
if [ $pingtest = 1 ]; then
	echo Device online
else
	echo Device offline
	exit 1
fi

# Get date and time from last sync
if [ ! -e $synctimefile ]; then
	touch $synctimefile
	synctime=$(stat -c %y $synctimefile)
else
	synctime=$(stat -c %y $synctimefile)
fi

echo Last time we did a sync was $synctime

# Let's check for the mount point and mount if it exists
if [ ! -d "$smbmount" ]; then
	echo "SMB mount point doesn't exist, please create it first"
	exit 1
elif [ -n "`mount | grep $smbmount`" ]; then
  echo "Already mounted"
else
	$SUDO mount -t cifs //$deviceip/$smbtarget $smbmount -o username=$smbuser,password=$smbpass,domain=$smbdomain,nocase,noperm,rw,file_mode=0777,dir_mode=0777
		if [ $? -eq 0 ]; then
			echo "Mounted!"
		else
			echo "Mount failed, bailing out..."
			exit 1
		fi
fi

# Okay, it's mounted.  Let's generate your files to copy using some find magic
find "$srclist" -newermt "$synctime" -type f -exec rsync -vut '{}' "$smbmount"/"$remotemediadir" ';'

# We copied some new files, so let's update the time we last did a sync
touch $synctimefile

# Tidy up and dismount
cd 
echo "Unmounting..."
$SUDO umount $smbmount

echo "Done"
exit 

Leave a comment