Jul 272012
 

Google Drive is an excellent choice for storing files in the cloud. It comes with good support for Windows, OsX and Android. It does however lack support for Linux at the moment, though Google have promised Linux support in the future.

We are for now left with community driven alternatives, Grive is one such great alternative. It works very well, even though it lacks on important feature; automatic sync on folder changes.

There are some sites that have nice descriptions on how to overcome this with periodic syncs. This is good enough most of the times, but it would be better if syncing could be triggered automatically on folder changes, including sub-folders.

There is actually already an existing package that can detect folder changes; Inotify. I have written a small script that utilizes that to detect folder changes and trigger an execution of Grive. That script is detailed full below. It’s small, simple and effective. Just add it to your automatic startup programs.

Startup Applications
This is how you do that in Gome2 or Mate.

Pre reqs:
Grive
inotify-tools

Follow this link for a guide on how to set up periodic syncs with Grive. It also details how to install Grive if you need help with that. I will neglect to describe that.

The below script should be added to your Startup Applications, save the script as grive.sh and add it.

  7 Responses to “Grive sync script”

  1. the ‘&&’ in your command is translated to ‘&&’ you should fix that.
    Also you should mention that we have to adjust the variables

     
  2. Thank you for pointing out that!

     
  3. If you add a timeout variable:

    TIMEOUT=600

    and change the inotifywait command to:

    inotifywait -t $TIMEOUT -e modify -e move -e create -e delete -r $GDRIVE_PATH

    then it will do a check at least every ten minutes, (or whatever timeout you set in seconds) so you don’t need a separate cron job and you also avoid the risk of script and cron job both running grive simultaneously.

     
    • That’s valuable feedback. Thank you! I will update with the information you suggested.

       
      • Actually I’ve been tinkering with this for a bit now and ended up with this:

        GRIVE_COMMAND_WITH_PATH=grive # Path to your grive binary, change to match your system
        GDRIVE_PATH=~/GoogleDrive # Path to the folder that you want to be synced
        LOGFILE=~/applications/grive/grive.log
        STARTTIME=$(date +”%D-%T”)

        echo “Starting grive.sh at $STARTTIME” >> $LOGFILE

        if [ pgrep -c grsync.sh -gt 1 ]; then # is grsync.sh running twice
        echo “grsync.sh is already running, exiting” >> $LOGFILE # if so, then log this
        exit # and exit
        fi

        # monitor relevant inotify watches on $GDRIVE_PATH and act on any that occur
        inotifywait -m -e modify -e delete -e move -e create -r $GDRIVE_PATH | while read LINE; do
        if [[ $LINE != *.grive* ]]; then #ignore grive local status updates
        if [ pgrep -c grive -eq 0 ] ; then # make sure grive is not already running
        echo $(date +”%D-%T”)$’\n’$LINE$’\n’”Starting Grive” >> $LOGFILE
        cd $GDRIVE_PATH && $GRIVE_COMMAND_WITH_PATH >> $LOGFILE 2>&1 # run grive
        else
        echo “grive already running, not running again” >> $LOGFILE
        fi
        else
        echo “Ignored $LINE” >> $LOGFILE
        fi
        done

        This seems to be working and it avoids repeatedly creating the inotify watches. Also it doesn’t run grive when the .grive and .grive_status files are updated by grive. The only drawback is that it runs the full Grive check every time you update a file, so if you’re editing a document and saving as you go it’s constantly running Grive. However that is inherent to Grive rather than this script so can’t be helped for now.

        Then I added a cron job which updates a hidden .update file in the relevant directory every 15 minutes to force Grive to run and therefore check the remote files at least that often:

        */15 * * * * echo $(date +”%D-%T”) > /home/ivan/GoogleDrive/.update

        This all does what I need for now. Hope it’s helpful.

         
  4. Sorry, I’m going to stop hijacking this page until I’ve tested more thoroughly. There are two flaws with what I posted above. The cron job was failing until I put the command out into a bash script instead of directly in the crontab. Also the main script seems to get stuck in a loop if there’s a remote update of the Google Drive, because that causes the local files to get updated, which triggers an inotify event, which triggers grive. Not sure why it loops rather than just doing an extra sync, but it seems to.

    I think your original solution was probably better, perhaps with my crontab update trigger. I can’t delete my comment, though, unfortunately.

     
    • I will leave your comment here anyway. It’s inspiring to see what you are aiming for. Someone else might help you debug it. =) I will update if you get to a final solution. Thank you for the efforts!!