This tutorial will demonstrate how I ship my backups to Google Drive using the drive cli utility.
What I really like about the drive cli tool, is that it’s super easy to setup and you can easily script your backups to ship it to google drive.
What we will be doing
We will setup the drive cli tool, authorize it with your google account, then show how to upload your files to google drive from your terminal and then create a script to automatically upload your data to google drive and then include it in a cronjob.
Setup Drive CLI Tool
Head over to the drive releases page and get the latest version, at the moment of writing 0.3.9 is the latest. Then we will move it to our path and make it executable:
1 2 3
You should be getting a output when running version as an argument:
Move to your home directory and initialize, this will ask you to access the google accounts web page, where you will be authorizing this application to use your google drive account. Upon succesful authorization, you will get a authorization code that we will need to paste in our terminal.
This will then write the credentials file to ~/.gd/credentials.json`. Always remember to keep this file safe.
1 2 3 4
You will now see that the credentials for your application has been saved as seen below:
Backup to Google Drive
On Google Drive, I have a backup folder named
Backups and in my local path
/opt/backups/, which has the files that I want to backup to google drive:
1 2 3
Now let’s backup the files to Google Drive, it works as follows
gdrive push -destination (path on google drive) (path on local drive):
1 2 3 4 5 6
As you can see it checks what is on Google Drive and what is on the Local Drive, then determines what needs to be uploaded, and asks you if you want to continue.
If we run that command again, you will see that it does not upload it again, as the content is already on Google Drive:
1 2 3
To test it out, let’s create a new file and verify if it only uploads the new file:
1 2 3 4 5 6
That is all cool and all, but if we want to script this, we don’t want to be prompted to continue, we can do this by adding a argument
Scripting our Backup Task
Let’s create a script that makes a local archive, then uploads it to Google Drive, I will create the file:
/opt/scripts/backup.sh with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Make the file executable:
Then, we want to add it as a cronjob so that it runs every night at 23:10 in my case:
crontab -e and add the following entry:
Backups are important, especially when you rely on them, and it was never made. Plan ahead to not be in that situation.