Syncing local directory to remote

This is my script I run manually after importing the contents of my digital camera (and a camcorder). It uses find to…well find files newer than the last time it was run. Also, only runnable as superuser.

It also ‘prunes’ the target of files that aren’t necessary, like the local photo organizer’s db (Shotwell, if you’re curious) using rm on a list from what was to be an exclude list for rsync, which turned out to slow things down dramatically. Though that was in place before using find to only send new files over.

#!/usr/bin/bash

# Mount point.
mp="/mnt/Example"
# Local storage.
lstor="/home/user/Pictures/Cameras/"
# Remote storage.
rstor="//example.local/photo/cameras"
# Credentials for above.
creds="/home/user/.smb/user@Example.local"
# Exclusions, but see note below.
excl="/home/user/.local/bin/rsync-cameras-exclude.txt"
# Meant to be used with rsync option --exclude-from but that causes massive slow down.
# Going to rm the list afterward instead. Although doesn't seem to be working...


[ ! -d $mp ] && mkdir $mp

# Additional rsync options from stdin to this script.
addopt=""
if [ $1 ]; then addopt=" $1"; fi

mount -t cifs -o credentials=$creds $rstor $mp && \
# So after adding a dozen years worth of pics and vids this takes over 
# 30 minutes every time it is run. Leaving as a reminder of a simpler time...
#/usr/bin/rsync -a$addopt --info=progress2 $lstor $mp

cd $lstor && \
find . -newer './last-rsync' | /usr/bin/rsync -a$addopt --info=progress2 --files-from=- $lstor $mp && \
date > ./last-rsync

if [ -f $excl ]; then
for i in `cat $excl`; do rm -rf '$mp$i'; done
fi

umount $mp

Leave a Reply

Your email address will not be published. Required fields are marked *