If you’re looking for a reliable Linux to Google Drive sync tool, you can use rclone and a bash script to make this work. Find out how.
Recently, disaster struck for me. Let me set the stage: There I was, minding my own business, when I went to work on a file housed within the folder I have synced with my Google Drive cloud account. Much to my surprise, the file was missing. So, I open my file manager and, shock of all shocks, the entire sync folder was gone.
As in not there.
“No problem,” I say. After all, the folder is still in the cloud.
No. It’s gone–as in not there.
Sweat began dripping down my forehead.
You have to understand, that folder contained 20 years of work and over 40 books I’ve published, including my current work in progress which was about 95% complete.
I sent my mind racing back to figure out what had happened. Well, the previous day I discovered that the tool I was using for syncing, overGrive, had stopped working. No matter what I did, I couldn’t get it to sync. So, I uninstalled it and re-installed Insync, which had been my sync tool for years.
After installing that tool, I discovered something was wrong with my license, so I uninstalled that. Somehow, in the process of uninstalling both of those tools, my local and cloud folders were deleted.
Fortunately, after much searching, I found the folder in Google Drive trash and was able to restore it.
The first thing I did after that was mount Google Drive, with the help of GNOME online accounts, and copy the folder in question to my local drive.
However, I still needed to keep that folder in sync between Drive and my local machine.
For that, I turned to my old pal, rclone. For those who aren’t aware of this tool, you can get up to speed with it via my article: How to sync from Linux to Google Drive with rclone.
The thing about the original piece is that you have to work with it manually. This is 2020 and we want our cloud syncing to happen automagically. So, I dug in and cobbled together a system for that purpose.
Let me show you how.
SEE: Power checklist: Local email server-to-cloud migration (TechRepublic Premium)
What you’ll need
To make this work, you’ll need rclone installed and have it linked to your Google Drive account (instructions for this are in the original post). You’ll also need a local directory to house the sync.
How to create a bash script for rclone
What we’re going to do is create a bash script that will be run from within a cron job. Before we create the script, know that it’s easy to get the actual command for rclone from the rclone-browser tool (as shown in the original article).
To create the new script, issue the command:
In this new file, paste the following, making sure to substitute the command you copied from the rclone-browser tool:
#!/bin/bash /usr/bin/rclone copy --update --verbose --transfers 30 --checkers 8 --contimeout 60s --timeout 300s --retries 3 --low-level-retries 10 --stats 1s --stats-file-name-length 0 "Google Drive:WORK" "/media/jack/TOSHIBA/RCLONE/WORK"
It’s important to note that Google Drive defines the cloud service used and WORK defines the directory to be synced. Also, make sure you change the local directory path that will house the sync.
Save and close that file.
Give the script executable permission with the command:
chmod u+x rclonedrive
Next, you need to move that file to a directory in your $PATH. Do that with the command:
sudo mv rclonedrive /usr/local/bin/
Finally, you need to change the ownership of the file with the command:
sudo chown $USER.$USER /usr/local/bin/rclonedrive
Once you’ve done that, you can test the script to make sure it works by issuing the command:
Once the command completes and you’re certain it’s working, it’s time to create a cronjob.
How to create a cronjob
We’re going to create a cronjob that will run every night at midnight. To do this, issue the command:
At the bottom of that file, paste the following:
0 0 * * * /usr/local/bin/rclonedrive > /dev/null
We have to direct the output of the rclonedrive script to /dev/null, otherwise the cronjob won’t work.
Save and close the file.
That’s it. Your Google Drive will now be copied to your local drive every night at midnight. Of course, that doesn’t mean it’s actually in sync.
To finish off the process, I created a second script that will copy from the local directory to Google Drive. I call that script rclone2drive. The only difference between rclonedrive and rclone2drive is that it switches the placement of the remote and local sources, so the rclone2drive bash script looks like:
#!/bin/bash /usr/bin/rclone copy --update --verbose --transfers 30 --checkers 8 --contimeout 60s --timeout 300s --retries 3 --low-level-retries 10 --stats 1s --stats-file-name-length 0 "/media/jack/MINA/WORK" "Google Drive:WORK"
Notice I’m working with a different folder locally. That means all of my local files are uploaded to the WORK directory in Google Drive and then those file are pulled down (by the first script) to the backup directory. It’s a bit circular, but it works.
With this one-two punch, you can have a reliable command-line sync system for Google Drive on Linux. For those of you who’ve had issues with other Google Drive sync tools, this should solve all of your problems.
At least it did for me.
Source of Article