How to Backup File to Google Drive with Bash

Introduction

Google Drive is a cloud-based storage solution that allows you to store and share files and directories online and access them from anywhere. It offers very useful features such as easier file sharing and having a remote location to backup your files. It can sync all your documents, photos, and more across remote devices, including mobiles, tablets, and PCs.

SnapShooter is a leading backup solution provider that allows you to backup files and directories to Google Drive easily. SnapShooter provides an intuitive, simple, and user-friendly web interface that allows beginners to control and manage their backups.

This post will show you how to backup file to Google Drive manually and using SnapShooter.

Backup File to Google Drive Manually

In this section, we will show you how to backup files and directories to Google Drive using gdrive utility.

Install and Configure Gdrive

You will need to install gdrive on the server from where you want to backup files and directories to Google Drive. Gdrive is a command-line utility used for interacting with Google Drive. With gdrive, you can manage, upload, download, delete and share files on Google Drive via command-line.

First, install the required dependencies with the following command:

apt-get install musl gnupg2 wget -y

Next, download the latest version of gdrive from the GitHub using the following command:

wget https://github.com/prasmussen/gdrive/releases/download/2.1.1/gdrive_2.1.1_linux_amd64.tar.gz

Once the download is completed, extract the downloaded file with the following command:

tar -xvzf gdrive_2.1.1_linux_amd64.tar.gz

Next, run the following command to install the gdrive to the system path:

sudo install gdrive /usr/bin/gdrive

Next, you will need to configure gdrive to access Google Drive. You can do it by running the following command:

gdrive about

You will get the following URL to authenticate with your Google account:

Authentication needed
Go to the following url in your browser:
https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=367116221053-7n0vf5akeru7on6o2fjinrecpdoe99eg.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=state

Now, open your web browser and access the above URL. You will get the following page:

Click on the Allow button. You should see the Authorization Code on the following page:

Copy the authorization code from the above page and paste it into your command-line interface:

Enter verification code: 4/1AX4XfsfdsgdgdstRgB_BJsdgdhdhhDmb0TxwbVwbTM49D8aiaeG4nFlz6ILQgzUO18

Once gdrive is connected with your Google Drive, you will get the following output:

User: Hitesh Jethva, hitjethva75@gmail.com
Used: 550.7 KB
Free: 16.1 GB
Total: 16.1 GB
Max upload size: 5.2 TB

You can verify the gdrive version using the following command:

gdrive version

You will get the following output:

gdrive: 2.1.1
Golang: go1.11.13
OS/Arch: linux/amd64

Use Gdrive to Backup File to Google Drive

At this point, gdrive is configured to manage Google Drive. You can list all available file on your Google Drive using the following command:

gdrive list

You will get the following output:

Id Name Type Size Created
1CUc3AmOVY9V4cJkNNE0ZVvdb-JOtZsnJ Article dir 2022-02-08 12:49:10

To upload a file named file.tar.gz from your server to the Google Drive, run the following command:

gdrive upload file.tar.gz

You will get the following output:

Uploading file.tar.gz
Uploaded 1VOlf85L4uby7VeLZfP2TI3wUgIIB80I6 at 382.8 KB/s, total 382.8 KB

To upload a directory to the Google Drive, run the following command:

gdrive upload --recursive directory

You will get the following output:

Creating directory directory
Uploading directory/apache.conf
Uploading directory/mysql.bak
Uploading directory/server.conf
Uploading directory/backup.zip

You can now verify your uploaded file and directory using the following command:

gdrive list

You will get the following output:

Id Name Type Size Created
1um3dJEpx5wUWlJUH4ibKbdTx8CZm-h5o backup.zip bin 20.5 MB 2022-04-13 08:25:50
1BUZJZkfstviGv951MLcAfS4pEYmFStSI server.conf bin 20.5 KB 2022-04-13 08:25:47
1PhcponYX9_2Tzci5q9dd7f_fjvNBlYV2 mysql.bak bin 2.1 MB 2022-04-13 08:25:47
1nggr4j3FTq6cwlBM2YJoxgr3eo_KTi9V apache.conf bin 20.5 KB 2022-04-13 08:25:46
13AxeIB5h3cgBpUg-luQzkP8c087S29or directory dir 2022-04-13 08:25:45
1VOlf85L4uby7VeLZfP2TI3wUgIIB80I6 file.tar.gz bin 382.8 KB 2022-04-13 08:25:30
1CUc3AmOVY9V4cJkNNE0ZVvdb-JOtZsnJ Article dir 2022-02-08 12:49:10

You can also access your Google Drive via a web browser to see your uploaded file and directory as shown below:

Scheduled Server File Backups SnapShooter

Server File Backup to your external storage

Learn more about Server File Backups

Get started for free
No credit card required. Cancel anytime!

Was this page helpful?

Thank you for helping us improve!