dotfiles

Obligatory dotfiles repo
git clone git://git.danielmoch.com/dotfiles.git
Log | Files | Refs

commit 82ddc893f4b927dae8d2010566e103270d9969df
parent 61bcff79f3c334952f06ec65da5eedaa865fc9b2
Author: Daniel Moch <daniel@danielmoch.com>
Date:   Mon, 12 Feb 2018 06:20:17 -0500

Refactor photosync.sh into cloudsync.sh

What was being used for syncing photos only when I was using a cloud
host is now syncing "mobile" documents (i.e., any files in
~/Documents/Mobile).

Also make more efficient by only syncing photos in the folder
representing the current month. Additionally look at a lastrun file to
see if the previous month needs to be synced as well.

One limitation of this solution is that it expects the sync will never
be more than a month behind, which is to say that we only sync the month
shown in 'lastrun' if it differs from the current month and *NOT* all
intervening months.

Diffstat:
A.local/lib/cron/cloudsync.sh | 87+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
D.local/lib/cron/photosync.sh | 43-------------------------------------------
2 files changed, 87 insertions(+), 43 deletions(-)

diff --git a/.local/lib/cron/cloudsync.sh b/.local/lib/cron/cloudsync.sh @@ -0,0 +1,87 @@ +#!/bin/sh +# +# ~/.local/lib/cron/cloudsync.sh +# + +echo "Beginning cloud sync at `date`" + +year=`date '+%Y'` +month=`date '+%m'` +lastrun_dir="$HOME/.local/var/`basename $0`" +lastrun_file="$lastrun_dir/lastrun" +mobile_docs="$HOME/Documents/Mobile" + +if [ -d "$lastrun_dir" ] +then + if [ -f "$lastrun_file" ] + then + lastrun_year=`cat $lastrun_file | cut -d ' ' -f 1` + lastrun_month=`cat $lastrun_file | cut -d ' ' -f 2` + else + firstrun=1 + fi +else + firstrun=1 + mkdir -p "$lastrun_dir" +fi +echo "$year $month" > $lastrun_file + +[ ! -d "$mobile_docs" ] && mkdir -p "$mobile_docs" + +# Only do the following if nextcloud.danielmoch.com is reachable +if ping -c 1 nextcloud.danielmoch.com > /dev/null 2>&1 +then + echo "nextcloud.danielmoch.com reachable. Proceeding." +else + echo "nextcloud.danielmoch.com NOT reachable. Exiting." + exit -1 +fi + +if type -p xdg-user-dir > /dev/null 2>&1 +then + LOCAL_PHOTOS=`xdg-user-dir PICTURES` +else + LOCAL_PHOTOS="$HOME/Pictures" +fi + +# Mount the WebDAV folder and set so only we can read it +if mount /mnt/nextcloud +then + echo "NextCloud WebDAV successfully mounted. Setting permissions." + chmod 700 /mnt/nextcloud + + # Rsync from WebDAV to ~/Photos + echo "Syncing Photos from WebDAV to $LOCAL_PHOTOS" + rsync -aq /mnt/nextcloud/Photos/$year/$month/* $LOCAL_PHOTOS + if [ -z "$firstrun" -a $lastrun_month -ne $month ] + then + # TODO: What if we're more than a month behind? + rsync -aq /mnt/nextcloud/Photos/$lastrun_year/$lastrun_month/* $LOCAL_PHOTOS + fi + + # Rsync from ~/Photos to WebDAV + echo "Syncing Photos from $LOCAL_PHOTOS to WebDAV" + rsync -aq $LOCAL_PHOTOS/$year/$month/* /mnt/nextcloud/Photos + if [ -z "$firstrun" -a $lastrun_month -ne $month ] + then + # TODO: What if we're more than a month behind? + rsync -aq $LOCAL_PHOTOS/$lastrun_year/$lastrun_month/* /mnt/nextcloud/Photos + fi + + # Rsync from WebDAV to ~/Documents/Mobile + echo "Syncing mobile documents from WebDAV to $mobile_docs" + rsync -aq /mnt/nextcloud/Documents/* "$mobile_docs" + + # Rsync from ~/Documents/Mobile to WebDAV + echo "Syncing mobile documents from $mobile_docs to WebDAV" + rsync -aq "$mobile_docs"/* /mnt/nextcloud/Documents + + # Finish by unmounting the WebDAV folder + echo "Unmounting nextcloud.danielmoch.com" + umount /mnt/nextcloud +else + echo "NextCloud WebDAV mount FAILED. Exiting." + exit -2 +fi + +echo "Completed cloud sync at `date`" diff --git a/.local/lib/cron/photosync.sh b/.local/lib/cron/photosync.sh @@ -1,43 +0,0 @@ -#!/bin/sh - -echo "Beginning photo sync at `date`" - -# Only do the following if nextcloud.danielmoch.com is reachable -if ping -c 1 nextcloud.danielmoch.com > /dev/null 2>&1 -then - echo "nextcloud.danielmoch.com reachable. Proceeding." -else - echo "nextcloud.danielmoch.com NOT reachable. Exiting." - exit -1 -fi - -if type -p xdg-user-dir > /dev/null 2>&1 -then - LOCAL_FOLDER=`xdg-user-dir PICTURES` -else - LOCAL_FOLDER="$HOME/Pictures" -fi - -# Mount the WebDAV folder and set so only we can read it -if mount /mnt/nextcloud -then - echo "NextCloud WebDAV successfully mounted. Setting permissions." - chmod 700 /mnt/nextcloud - - # Rsync from WebDAV to ~/Photos - echo "Syncing Photos from WebDAV to $LOCAL_FOLDER" - rsync -aq /mnt/nextcloud/Photos/* $LOCAL_FOLDER - - # Rsync from ~/Photos to WebDAV - echo "Syncing Photos from $LOCAL_FOLDER to WebDAV" - rsync -aq $LOCAL_FOLDER/* /mnt/nextcloud/Photos - - # Finish by unmounting the WebDAV folder - echo "Unmounting nextcloud.danielmoch.com" - umount /mnt/nextcloud -else - echo "NextCloud WebDAV mount FAILED. Exiting." - exit -2 -fi - -echo "Completed photo sync at `date`"