Skip to content

Instantly share code, notes, and snippets.

@muety
Last active December 20, 2022 20:32
Show Gist options
  • Save muety/873980a2440b4c6692003fc23903fe5a to your computer and use it in GitHub Desktop.
Save muety/873980a2440b4c6692003fc23903fe5a to your computer and use it in GitHub Desktop.
Automated Google Drive sync for Linux using rclone
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
@kerus1024
Copy link

thanks

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