From 01f457d0bb1ba46bdeee552ae5fccc06612f01fa Mon Sep 17 00:00:00 2001 From: Al Williams Date: Mon, 1 Sep 2014 14:51:50 -0500 Subject: [PATCH 1/3] Adds a few useful scripts for auto upload and periodic download sync --- scripts/crontab.txt | 5 +++++ scripts/grive-incron | 34 ++++++++++++++++++++++++++++++++++ scripts/incron.txt | 3 +++ scripts/readme.txt | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 scripts/crontab.txt create mode 100755 scripts/grive-incron create mode 100644 scripts/incron.txt create mode 100644 scripts/readme.txt diff --git a/scripts/crontab.txt b/scripts/crontab.txt new file mode 100644 index 00000000..029d6597 --- /dev/null +++ b/scripts/crontab.txt @@ -0,0 +1,5 @@ +# This example runs grive on /home/USER/gdrive +# See man crontab for format +# This line runs at 5 after the hour and 35 after the hour + +5,35 * * * * /usr/bin/grive -p /home/USER/gdrive diff --git a/scripts/grive-incron b/scripts/grive-incron new file mode 100755 index 00000000..40ed76d6 --- /dev/null +++ b/scripts/grive-incron @@ -0,0 +1,34 @@ +#!/bin/bash + +# Allow grive to be called from incron +# if your path is /home/User/gdrivef and this file is in /usr/local/bin then use the following incrontab line: +# /home/User/gdrive IN_CREATE,IN_DELETE,IN_CLOSE_WRITE,IN_MOVE /usr/local/bin/grive-incron $# /home/User/gdrive + +# grive-incron a bash script to allow automatic push to gdrive from a directory +# It is made to be called by # incron +# +# Copyright (C) 2014 Al Williams (al.williams@awce.com) +# +# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. +# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/. + +if [ $# != 2 ] +then + >&2 echo Call from incron: \$# directory + exit 1 +fi +if [ "$1" == .grive -o "$1" == .grive_state ] +then + exit 0 # ignore grive housekeeping files +fi +# Don't run multiple copies at once +N=2 +while [ "$N" != 1 ] +do + N=$( pgrep -c -u "$USER" grive-? ) # assume if 1 it is me! + if [ "$N" != 1 ] + then sleep 1 + fi +done +exec grive -p "$2" diff --git a/scripts/incron.txt b/scripts/incron.txt new file mode 100644 index 00000000..99806945 --- /dev/null +++ b/scripts/incron.txt @@ -0,0 +1,3 @@ +# Fix path below (2 places) and location of grive-incron script +/home/USER/gdrive IN_CREATE,IN_DELETE,IN_CLOSE_WRITE,IN_MOVE /usr/bin/grive-incron $# /home/USER/gdrive + diff --git a/scripts/readme.txt b/scripts/readme.txt new file mode 100644 index 00000000..ea661d6a --- /dev/null +++ b/scripts/readme.txt @@ -0,0 +1,40 @@ +This directory contains a simple script that allows calling +grive from incron. This will cause any changes you make +to the local directory to automatically call grive to +update Google Drive. You can copy the script to a location on your +path: + +sudo cp grive-incron /usr/local/bin + +To make this work, you also need to insert the contents of incrontab.txt +into your incrontab (after changing the paths to suit your +installation). + +To do this run: +incrontab -e + +Then use the editor that appears to insert the line with your custom +paths. You can ignore lines that start with # -- they are just comments +although you can paste them into the incrontab if you like. + +To get the reverse, you can do a similar command in your crontab (see +crontab.txt). The example syncs at 5 and 35 minutes after each hour +but you can customize that by changing the crontab line. + +crontab -e + +will bring up an editor. Copy the line without the # in front from +crontab.txt after fixing it for your specific path names. You can +skip the # lines or include them, as before. + +You probably don't want to do a crontab sync every minute because +grive takes some time to run. The grive-incron script stalls if +another copy of it or grive is running for the current user to +prevent overrun, but the crontab entry has on such protection. If you +prefer, you could use the same script with a dummy file name in crontab. +Then your crontab line would look something like this: + +5,35 * * * * /usr/local/bin/grive-incron DUMMY /home/USER/gdrive + +The word DUMMY is just a placeholder. It does not have to refer to a real +file. From 1f5c6d4172a25638f497473984989bb3facdc294 Mon Sep 17 00:00:00 2001 From: Al Williams Date: Mon, 1 Sep 2014 15:47:06 -0500 Subject: [PATCH 2/3] Fixes deadlock problem but still possible for more than one grive to start --- scripts/grive-incron | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/scripts/grive-incron b/scripts/grive-incron index 40ed76d6..6c55cca3 100755 --- a/scripts/grive-incron +++ b/scripts/grive-incron @@ -24,11 +24,13 @@ then fi # Don't run multiple copies at once N=2 -while [ "$N" != 1 ] +DLY=1 +while [ "$N" != 0 ] do - N=$( pgrep -c -u "$USER" grive-? ) # assume if 1 it is me! - if [ "$N" != 1 ] - then sleep 1 + N=$( pgrep -c -u "$USER" ^grive$ ) # assume if 1 it is me! + if [ "$N" != 0 ] + then sleep $DLY + DLY=$((DLY+5)) fi done exec grive -p "$2" From 00e998694d7e2f6e14e716aab1944f8779422875 Mon Sep 17 00:00:00 2001 From: Al Williams Date: Mon, 1 Sep 2014 16:16:58 -0500 Subject: [PATCH 3/3] Better fix for interlock using PID file --- scripts/grive-incron | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/scripts/grive-incron b/scripts/grive-incron index 6c55cca3..cdebc201 100755 --- a/scripts/grive-incron +++ b/scripts/grive-incron @@ -13,6 +13,8 @@ # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. # You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/. +PIDFILE=/tmp/grive-incron.lock + if [ $# != 2 ] then >&2 echo Call from incron: \$# directory @@ -23,14 +25,28 @@ then exit 0 # ignore grive housekeeping files fi # Don't run multiple copies at once -N=2 + DLY=1 -while [ "$N" != 0 ] -do - N=$( pgrep -c -u "$USER" ^grive$ ) # assume if 1 it is me! - if [ "$N" != 0 ] - then sleep $DLY - DLY=$((DLY+5)) - fi + +while true + do + while [ -f "$PIDFILE" ] && kill -0 `cat "$PIDFILE"` 2>/dev/null + do + sleep $DLY + DLY=$((DLY+1)) + DLY=$((DLY % 10 )) # no more than 10 seconds + done + if ! kill -0 `cat "$PIDFILE" 2>/dev/null` 2>/dev/null + then + rm -f "$PIDFILE" + fi + if (set -o noclobber; echo $$>"$PIDFILE" ) >/dev/null + then + trap 'rm -f "$PIDFILE"' INT TERM EXIT + break + fi done -exec grive -p "$2" +grive -p "$2" +rm -f "$PIDFILE" +trap - INT TERM EXIT +exit 0