Easy Backups to Google Drive on Linux

I’ve recently set up automated backups to Google Drive on my CentOS 7 Linux VPS to protect my blog posts from data loss. I don’t know why it took so long for me to care about backups of my own data. I chose to back up to Google Drive since I already have a Google One plan that has plenty of available storage space.

Screenshot from Google Drive showing 4 compressed files backed up in rclone/backup folder
Four compressed files backed up in a Google Drive folder

Research

I had to do some research to find the best tool for the job. I stumbled upon gdrive which is no longer maintained. There’s also Insync which requires a payment. I then found rclone which seems to be really popular on GitHub and actively maintained.

Implementation

Configuring rclone

Configuring rclone was the most difficult part as I didn’t want the machine to have access to my whole Google Drive data.

Create client_id and client_secret

To ensure you get the best transfer speed you should create your own client_id from Google Cloud Platform Console by following rclone Making your own client_id guide. Take a note of your client_id and client_secret.

Create root folder

Create a directory in your Google Drive and go to it. In the URL bar you will see a long string which is your root_folder_id. e.g. if your URL is drive.google.com/drive/u/0/folders/abc123, your root_folder_id is “abc123”.

Configure rclone

Execute “rclone config” command and fill in the details as follows. Output shortened for

e/n/d/r/c/s/q> n # n for New remote
name> gd # name your storage. I named mine "gd" here

Type of storage to configure.
Storage> 15 # Enter the number corresponding to Google Drive "drive"

Google Application Client Id
client_id> 124124214-214afasfas.apps.googleusercontent.com # REPLACE

OAuth Client Secret
client_secret> abc1234 # REPLACE with your own client_secret

Scope
scope> drive.file # use this to avoid giving access to whole drive

ID of the root folder
root_folder_id> abc123 # Use the folder you created above

Service Account Credentials JSON file path
service_account_file>    # Leave BLANK, press enter to skip

Edit advanced config? (y/n)
y/n> n # No need to modify advanced configuration

Use auto config?
y/n> n # Unless you're backing up your local machine, type "n"

Please go to the following link: https://path/to/auth
# Go to link above and click allow to obtain verification code below
Enter verification code> ccc\ddd123

Configure this as a Shared Drive (Team Drive)?
y) Yes
n) No (default)
y/n> n # this is "n" for most cases

# Your configuration will be printed below
type = drive
client_id = 124124214-214afasfas.apps.googleusercontent.com
client_secret = abc1234
scope = drive.file
root_folder_id = abc123
token = {some JSON}

y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y # Type "y"

Current remotes:
Name                 Type
====                 ====
gd                   drive
e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q> q # Type "q" to finish

Test your configuration

Run “rclone ls gd:” to ensure it works. The “:” at the end is important.

Backup script

Backup script will do the work for you. Change it according to your needs.

Screenshot of Google Drive showing previous versions of a file named "letsencrypt-config.tar.gz"
rclone also keeps older versions of the backups in Google Drive

Final thoughts

I was really happy to discover rclone project. Now I am sponsoring it on Patreon. Hope you can also benefit from this amazing tool. You should also support the project if you can afford to.

Related posts

Links

1 thought on “Easy Backups to Google Drive on Linux

Leave a Reply

Your email address will not be published. Required fields are marked *