I am helping one customer with their VPS and they use primarily Google Drive for data management. So the idea poped up in my head…
What if I could grab some files from the VPS server and push them straight into their G-Drive?
This could provide an opportunity for backing up required data from VPS but also push certain data back into VPS without a need for FTP. Hmm…
As an example: you could upload product images for the online shop, where user simply drops them into Google Drive and the rest of the work gets automated on the server with CRON and custom scripts. Same can be done for updating stock via XML files and more.
In this article, I will focus on getting the integration working and leave the rest for your imagination. However, if you would want to know more about the automation part on the server, have even better idea, just leave me a message on LinkedIn or e-mail me. Now let us dive into it.
Although my personal preference is Ubuntu, I decided to play with Debian on my local PC to test the Google Drive integration. I used Hyper-V and Debian-10.1.0-i386-netinst.ISO. My laptop specs: T440, 256GB SSD, 12GB RAM.
First problem, ISO is not booting – getting PXE Boot issue = I create new VM with GEN1 and we are ready to go. Here are more tips on how to run Linux on HyperV.
Second problem, screen size/resolution of the VM. Hyper-V does not control this, instead the VM is in charge. Everyone probably knows how to change the resolution in Windows 10/7 using GUI, but since I have Debian with no GUI, CLI will become my friend for today. So for Debian, you need to edit grub file like this:
sudo vi /etc/default/grub
In particular, this line:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash video=hyperv_fb:1024x768"
I am using laptop T440, so 768px vertical is just ok. If you have to update the grub, reboot and off you go.
Note: If you are not familiar with VI editor (I use usually VIM), just follow the manual – VI basics.
Third “problem”, quite annoying with Linux and HyperV is missing copy / paste feature. You certainly do not want to type all stuff and links manually. Forget about Hyper-V VMConnect then and connect to your VM via SSH (use Putty for example). That way you can copy paste, no drama. There are other options, but this one is by far the easiest, I reckon.
To connect, you will need its IP address, so use a bridge mode (external switch). This will expose the VM to the internet too, so keep that in mind and put ideally a firewall in between. To connect:
Fourth “problem”, you cannot connect. By default root has remote ssh turned off. To turn it on: connect as per usual using VMConnect, open:
Remove # symbol from the line below and change to “yes”, like this:
Don’t forget to restart the SSH:
Ok. We are ready for the next step.
Unfortunately, there is no offical Google Drive app for Linux. So, we will be using drive originally developed by Burcu Dogan, now maintained by Emmanuel T Odeke. It should do the basics. As usual, there are some pre-requisites.
Drive is using GO, an open-source programming language, so we have to install it first.
To download GO, visti the official site and chose the right distribution for you and download it from your VM using wget:
Then install/extract the package as per below (using the correct file name):
tar -C /usr/local -xzf go1.13.3.linux-amd64.tar.gz
Add /usr/local/go/bin to the
PATH environment variable, by editing /etc/profile (system wide) with:
Test it by running creating and building hello app.
Note: if you cannot execute “go build” for your hello.go file, see if you downloaded the correct distro of GO.
First thing, however, we need to update the export PATHs for .bashrc. This is one way of doing it:
cat << ! >> ~/.bashrc > export GOPATH=\$HOME/gopath > export PATH=\$GOPATH:\$GOPATH/bin:\$PATH > ! source ~/.bashrc # To reload the settings and get the newly set ones # Or open a fresh terminal
Then download the DRIVE, I used this option:
go get -u github.com/odeke-em/drive/drive
Problem? missing get? Install it first.
Try to get the DRIVE again:
go get -u github.com/odeke-em/drive/cmd/drive
In my case, this just got stuck, so I went for the other option:
go get -u github.com/odeke-em/drive/drive-google
Which also failed but at least told me that I have ggc missing. You could install it separately, but I used the bundle package to same me some time with other potential dependencies (not suggested in production!):
apt-get install build-essential
Re-try the Drive install again:
go get -u github.com/odeke-em/drive/drive-google
BINGO. You can check it by running its command:
If you expect you click and authorise, then I will dissapoint you. You will have use GCP (Google Cloud Platform), create project, security account, keys, delegate access to G-Drive, download and push json file onto VM. Handy hint: using OAuth and service account;).
So once you have the GCP part done and ready, run the following (update the location of your json file):
drive init --service-account-file <gsa_json_file_path> ~/gdrive cd ~/gdrive
There should be hiddne folder called “.gd” (find by “ls -a”) and within should be a file called credentials.json.
Open and edit this file again with our VI:
Find and edit the Subject=”” with an e-mail address whose Google Drive you wish to use. For example: Subject=”email@example.com”.
Important: if you skipped this steps, the actual projects’ Google Drive will be used instead, meaning none of your actual data will be displayed. It uses the service account’s drive by default. This might be handy for other automation and deployments but not when I want to see my files!
The best is to check the manual first, by typing:
The handy commands are:
list, about, push, pull
Combine this solution with custom scripts to move/copy files around using CRON and you have a nice integration betwen Google Drive and your Debian / VPS. Also, be aware of the GCP requests limits, otherwise your integration might not work that well if you reach them.
Although, not a straightforward solution, but as you can see – it is do-able and fun to play with. Keep in mind however, that if you provide this solution to a third part, you need to maintain it and keep it functional (consider updates, security, etc). I see a fit for this, where a custom solutions are required, but would rather opt-out for CMS-plugins or host-provider’s paid service to facilitate for example a backup solution. If you have ideas to share or discuss with me, please get in touch with me on LinkedIn or contact me.