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..cdebc201 --- /dev/null +++ b/scripts/grive-incron @@ -0,0 +1,52 @@ +#!/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/. + +PIDFILE=/tmp/grive-incron.lock + +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 + +DLY=1 + +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 +grive -p "$2" +rm -f "$PIDFILE" +trap - INT TERM EXIT +exit 0 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.