How to Free Backup your Linux server in Google Drive automatically 2023

For this integration we are using rClone, an open-source tool easy to install and easy to configure.

Here is the website of the project: https://rclone.org/

How to install and configure rClone in Linux

We can install it directly with this command:

sudo -v ; curl https://rclone.org/install.sh | sudo bash

Also, the configuration is straightforward, but first, we are going to create the JSON credentials in Google.

If you want to do everything from the shell/terminal, as is our case (because in some web servers, we do not have screenplay to save resources) you have to configure the JSON credentials in Google (read below). If you can open a web browser in your server, you can configure the credentials by following these instructions on the official website of the software.

How to configure JSON credentials in Google

Go to Google Cloud and create a New Project (or use one you have). To create a new one you just have to click one button, Google tell you how here.

Once in your project, go to APIs and Services > Enable APIs and Services or enter this link, and click on the button Enable APIs and Services:

Enable APIs and Services in Google Cloud

On the new window search for “Google Drive” and select the API call “Google Drive API” and select it.

Search Google Drive API in Google Cloud

In the next step, click Enable (or Manage if you have it active already).

Now, go to Credentials and click Create Credentials > Service Account:

Choose a name for the credential. The rest of the data is optional. When you finish it, on the page if Google Drive API, go down until you find Service Accounts and click on the new one you created.

Here, go to Keys > Add key > Create a new key:

Choose JSON and Create. The website will download the JSON key to your computer. Upload this file in a directory in your server and copy the absolute route of it to add it to the configuration later. Example: /var/www/vhosts/myhost.com/thejsonfile.json

Here, we also go to “Details“, and we will see the page “Service account details“. Copy the Unique ID, we will need it for the next step.

Go to https://admin.google.com

Enter in Security > Access and Data Control > APIs controls > Domain-wide delegation > Manage domain-wide delegation

Here, in API clients, click in “Add new”:

In “Client ID” you will paste the Unique ID you have copied in the step below and in OAuth Scopes, you will paste and click to Authorise:

https://www.googleapis.com/auth/drive

That is all. Now your Google Account is ready to receive the backup. Let’s configure the backup now on your server.

Configure rClone with Google Drive

To configure the rClone, simply follow these steps in your terminal:

rclone config

n/s/q> n         # New
name>gdrive      # gdrive is an example name
Storage>         # Select the number shown for Google Drive
client_id>       # Can be left empty
client_secret>   # Can be left empty
scope>           # Select your scope, 1 for example
root_folder_id>  # Can be left empty
service_account_file> /var/www/vhosts/myhost.com/thejsonfile.json # This is where the JSON file goes!
y/n>             # Auto config, n
Configure this as a Shared Drive (Team Drive)? N
Keep this "gdrive" remote? Y

Backup command line

You can create a file.sh with this command line:

#!/bin/bash
rclone -v --drive-impersonate [email protected] sync --progress "/var/www/folderyouwanttosync" "gdrive:folderinGoogleDrive"

Notice, that you can put any email you want, but the email in your Google account is recommended.

The folder to Backup has to be in a complete route, for example: “/var/www/folderyouwanttosync”

In the command “gdrive:folderinGoogleDrive”, gdrive is the name you put in the configuration of rclone and folderinGoogleDrive is the folder from the root of your Google Drive. Another example can be: “gdrive:backups/myserver1/”. It is important to not write any spaces inside the brackets. The folders have to be without them.

The command sync will synchronize the folder in your server with the folder in Google Drive. You can also see the copy command on the official website here.

With these instructions, you should be able to add this file or the command line to your server’s crontab.

Leave a Reply

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