Skip to content

Instantly share code, notes, and snippets.

@mrsrvman
Created April 1, 2020 12:37
Show Gist options
  • Save mrsrvman/bb6a669c117c1de3dfc29267399b684d to your computer and use it in GitHub Desktop.
Save mrsrvman/bb6a669c117c1de3dfc29267399b684d to your computer and use it in GitHub Desktop.
Script that will trigger a local to remote sync when any changes below your local Google Drive folder occur - but at max. every 10 minutes - and a remote to local sync every x (e.g. 30 minutes) via a cron job.
0. Install rclone and configure it for Google Drive
1. Create files listed below
2. Configure rclone_watch_local.sh to be run on startup (e.g. using a systemd service unit)
3. Add a cron job that runs rclone_remote2local.sh every x (e.g. 30) minutes
----------------------
rclone_local2remote.sh
----------------------
!/bin/bash
GDRIVE_DIR=/media/myuser/HDD/Google\ Drive
echo '[rclone] Syncing local -> remote.'
if [ -d "$GDRIVE_DIR" ]; then
if ! ps ax | grep -v grep | grep "rclone sync" > /dev/null; then
rclone sync "$GDRIVE_DIR" drive:/ && echo 'Finished local -> remote sync.'
else
echo '[rclone] A sync is already running.'
fi
fi
----------------------
rclone_remote2local.sh
----------------------
!/bin/bash
GDRIVE_DIR=/media/myuser/HDD/Google\ Drive
echo '[rclone] Syncing remote -> local.'
if [ -d "$GDRIVE_DIR" ]; then
if ! ps ax | grep -v grep | grep "rclone sync" > /dev/null; then
rclone sync drive:/ "$GDRIVE_DIR" && echo '[rclone] Finished remote -> local sync.'
else
echo '[rclone] A sync is already running.'
fi
fi
---------------------
rclone_watch_local.sh
---------------------
!/bin/bash
GDRIVE_DIR=/media/myuser/HDD/Google\ Drive
echo '[rclone] Watching locally.'
if [ -d "$GDRIVE_DIR" ]; then
while true; do
# sync at max every 10 minutes
inotifywait -r "$GDRIVE_DIR" && bash /home/myuser/scripts/rclone_local2remote.sh && sleep 10m
done
fi
@3ter
Copy link

3ter commented May 15, 2020

Wouldn't your recommended setup (systemd unit + cron job every 30 minutes) potentially impose conflicts?

  • You're changing something on your remote (on another system)
  • You boot up the system which doesn't have the latest changes yet
    • The cron job still takes 25 minutes for its first run due to the schedule
  • You change something, it gets pushed to the remote by the watching systemd unit -> potential conflict / data loss

Do you have a recommendation to make sure the current state is fetched after a restart?

I think this would work fine
# /home/3ter/.config/systemd/user/rclone-fetch-gdrive.service
[Unit]
Description=Download the latest changes from Googledrive to the local folder (restarted every 30 minutes) 

[Service]
Restart=always
RestartSec=30m
ExecStart=/usr/bin/sh /home/3ter/Documents/scripts/rclone_remote2local.sh

[Install]
WantedBy=default.target

Additionally I think inotify should only watch for the close_write event, because it otherwise gets triggered by rclone_remote2local.sh.

inotify --recursive --event close_write "$GDRIVE_DIR"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment