Sitemap

Setup Google Drive on Linux using Rclone

5 min readJul 27, 2024

--

Press enter or click to view image in full size
Custom Rclone/Google Drive cover — ©TheLazyFox

As a recent migrant from Windows to Linux, one thing I missed is Google Drive desktop app with 2-way real-time syncing.

Although it could be intimidating, Rclone is a massive program that bridges this, and numerous other gaps. In the words of one Reddit user:

Rclone is the answer… Rclone is always the answer

Following meticulous research, testing, and surgical precision, I’ve compiled the following guide, so you don’t have to 🤓

First time sync

Now we’ll do our first sync manually in terminal to make sure everything works perfectly before configuring the background service.

If you receive errors running rclone commands, they’re usually descriptive and easy to fix. If not, you can always look it up in their extensive documentation & forums.

That said, I have spent a lot of time debugging such errors and came up with this guide, hoping to save your time.

Create local Google Drive directory:

Where you want your files to be saved locally. Of course, you can change it to your liking.

mkdir -p $HOME/gdrive

Initial test run

Run this command in terminal to see if everything works well.

Don’t worry, this is a -- dry-run with no permanent changes.

rclone bisync \
--resync \
--dry-run \
"gdrive:/" "$HOME/gdrive" \
--compare size,modtime,checksum \
--modify-window 1s \
--create-empty-src-dirs \
--drive-acknowledge-abuse \
--drive-skip-gdocs \
--drive-skip-shortcuts \
--drive-skip-dangling-shortcuts \
--metadata \
--progress \
--verbose \
--log-file "$HOME/.config/rclone/rclone.log"
  • Breakdown:
    resync: Forces a full sync to fix conflicts, takes more time.
    dry-run: Trial run with no permanent changes.
    "gdrive:/" "$HOME/gdrive": Specifies remote and local paths.
    compare size,modtime,checksum: Compare file size, modification time, checksum.
    modify-window 1s: Allows 1-second time difference for equivalent files.
    create-empty-src-dirs: Creates and deletes empty directories.
    drive-acknowledge-abuse: Allows transfer of “dangerous” files (e.g., .exe, .apk).
    drive-skip-gdocs: Skip Google Docs altogether.
    drive-skip-shortcuts: Skips shortcuts.
    drive-skip-dangling-shortcuts: Skips shortcuts to non-existent files.
    metadata: Preserves file metadata.
    progress: Displays progress bar.
    verbose: Increases output verbosity.
    log-file: Logs sync process to a file.

Initial live run (First time sync)

Same as previous “Initial test run”, but without dry-run.

Only run this after a successful dry-run

rclone bisync \
--resync \
"gdrive:/" "$HOME/gdrive" \
--compare size,modtime,checksum \
--modify-window 1s \
--create-empty-src-dirs \
--drive-acknowledge-abuse \
--drive-skip-gdocs \
--drive-skip-shortcuts \
--drive-skip-dangling-shortcuts \
--metadata \
--progress \
--verbose \
--log-file "$HOME/.config/rclone/rclone.log"

Files check

  • After successful first time sync, let’s check if files in source and destination match by comparing sizes and hashes (MD5 or SHA1).
  • If any, this will log mismatched files so we can manually resolve them.
  • It doesn’t alter the source or destination.
rclone check \
"gdrive:/" "$HOME/gdrive" \
--progress \
--verbose \
--drive-skip-gdocs \
--drive-skip-shortcuts \
--drive-skip-dangling-shortcuts \
--metadata \
--log-file "$HOME/.config/rclone/rclone.log"

Create test files

For safety against data loss, we need to create matching test files in remote and local paths (Required for check-access later on).

rclone touch gdrive:/RCLONE_TEST
rclone copyto gdrive:/RCLONE_TEST $HOME/gdrive/RCLONE_TEST

Final command (Manual test run)

Now we’ll run the final command one more time with explicit progress and detailed logging.

rclone bisync \
"gdrive:/" "$HOME/gdrive" \
--compare size,modtime,checksum \
--modify-window 1s \
--create-empty-src-dirs \
--drive-acknowledge-abuse \
--drive-skip-gdocs \
--drive-skip-shortcuts \
--drive-skip-dangling-shortcuts \
--metadata \
--progress \
--verbose \
--log-file "$HOME/.config/rclone/rclone.log" \
--track-renames \
--fix-case \
--resilient \
--recover \
--max-lock 2m \
--check-access
  • New options:
    track-renames: Tracks file renames.
    fix-case: Fixes case sensitivity issues.
    resilient: Continues on errors.
    recover: Recovers from failed transfers.
    max-lock 2m: Sets max lock wait time to 2 minutes.
    check-access: Checks file access before syncing.

Background service

Time to set things up to run continuously in the background.

Get Faris Khasawneh’s stories in your inbox

Join Medium for free to get updates from this writer.

To ensure a reliable 2-way syncing, we need bisync to trigger on:

  • Timer: every 10 minutes
  • File changes: recursive files/dirs changes
    * Make sure you have inotify-tools installed

Additionally, we’ll Pause on metered connection (using nmcli).

Script files

Create the following files in $HOME/.local/bin/

  • rclone_sync.sh
#!/bin/bash
# rclone_sync.sh

local_dir=$HOME/gdrive
remote_dir=gdrive:/

echo "$(date +'%Y/%m/%d %H:%M:%S') Sync started" >> "$HOME/.config/rclone/rclone.log"

rclone bisync \
"$remote_dir" "$local_dir" \
--compare size,modtime,checksum \
--modify-window 1s \
--create-empty-src-dirs \
--drive-acknowledge-abuse \
--drive-skip-gdocs \
--drive-skip-shortcuts \
--drive-skip-dangling-shortcuts \
--metadata \
--log-file "$HOME/.config/rclone/rclone.log" \
--track-renames \
--fix-case \
--resilient \
--recover \
--max-lock 2m \
--check-access

sleep 1

if [ $? -eq 0 ]; then
echo "$(date +'%Y/%m/%d %H:%M:%S') Sync done" >> "$HOME/.config/rclone/rclone.log"
else
echo "$(date +'%Y/%m/%d %H:%M:%S') Sync failed" >> "$HOME/.config/rclone/rclone.log"
fi
  • rclone_check_metered.sh
#!/bin/bash
# rclone_check_metered.sh

if nmcli -p -f GENERAL.CONNECTION,GENERAL.METERED dev show | grep -i "yes" > /dev/null; then
echo "$(date) Metered connection detected. Skipping rclone bisync." >> "$HOME/.config/rclone/rclone.log"
exit 0
else
exit 1
fi
  • rclone_timer.sh
#!/bin/bash
# rclone_timer.sh

if ! "$(dirname "$0")/rclone_check_metered.sh"
then
echo "$(date +'%Y/%m/%d %H:%M:%S') rclone_timer triggered" >> "$HOME/.config/rclone/rclone.log"
"$(dirname "$0")/rclone_sync.sh"
fi
  • rclone_inotify.sh
#!/bin/bash
# rclone_inotify.sh

inotifywait -q -m -r -e move,create,delete,modify $HOME/gdrive | while read file; do
if ! "$(dirname "$0")/rclone_check_metered.sh"
then
echo "$(date +'%Y/%m/%d %H:%M:%S') rclone_inotify triggered" >> "$HOME/.config/rclone/rclone.log"
"$(dirname "$0")/rclone_sync.sh"
fi
done

Make script files executable

chmod +x ~/.local/bin/rclone_*.sh

Schedule cron jobs

Configure scripts to auto run in background

Edit cron table:

crontab -e

Add the following lines:

*/10 * * * * ~/.local/bin/rclone_timer.sh
@reboot sleep 30 && ~/.local/bin/rclone_inotify.sh

Save and exit the editor.

You’re all set!

Reboot your system to ensure everything works smoothly.

Helpful commands for troubleshooting:

  • View rclone log file in real-time:
tail -F ~/.config/rclone/rclone.log
  • Check running rclone scripts:
ps -ef | grep -E 'rclone.*\.sh$'
  • Check cron service status:
systemctl status cron
  • Restart cron service:
sudo systemctl restart - now cron
  • Restart systemctl daemon:
sudo systemctl daemon-reload

Remarks:

  • Cron vs Systemd: Trust me, I tried, didn’t work (there’s a reason why people hate systemd). Cron is easy, simple, cross-distro, and just works!
  • Don’t worry about bisync is IN BETA. Don’t use in production! warning.
  • — drive-skip-gdocs: Syncing Google Docs with bisync doesn’t work well, and I don’t use them anyway.
  • Bisync vs Sync: Bisync provides two-way syncing, running sync twice from remote to local, and vice versa, for reliability and accuracy.
  • Bisync takes about 5–7 minutes for an unchanged 13GB folder with around 20k files. Duration depends on file size, number of files, and connection speed.
  • Bisync can safely run in concurrent cron jobs thanks to lock files it maintains.

--

--

Written by Faris Khasawneh

IT Expert | Technology & Infrastructure | Privacy & Systems at Scale. Exploring the intersection of power, people & economy. Objective, evidence-based.