Yes, you read that direction correctly: this is a how to covering backing Google Drive up to a Raspberry Pi, not the other way around.
For the impatient, here’s a link to the scripts on GitHub, but it’ll probably help put things in context if you at least skim the rest of this post since there are a couple of dependencies in the mix.
Background
I do volunteer tech work for an organization that, like many organizations, uses Google Drive heavily, and doesn’t have much money to throw around. This organization’s Tech Committee has discussed whether or not we need to have a backup of the Google Drive files numerous times, but we keep kicking the can down the road since Google Drive versions files and retains trash until someone empties it, lessening the necessity of having a backup.
But, catastrophes happen. If someone were to delete all the files and empty the trash, you do have 25 days to contact Google and they can try to get your files back for you. Alternatively, you could hope someone has a copy of everything on their local machine. I’m sure some folks do, me being one, but crossing one’s fingers does not a good backup policy make, and if someone is frantically looking for a file, I’m not sure I want to have to contact Google to hunt it down. Plus this seemed like a fun little project.
Another alternative if you want to spend some money would be to use one of the services that will backup Google Drive to another cloud storage. In my situation we didn’t want to spend any budget on this, and we didn’t want to make it a task someone had to remember to do manually. Since I have a few Raspberry Pis laying around I decided to take a run at coming up with a solution I could just have running quietly in my house that would cover us in the event of a disaster.
Requirements and Constraints
The requirements/constraints I placed on myself were as follows:
- It can’t cost any money other than the cost of the Raspberry Pi and SD card. (And since I have several I’m not using, this was “free.”) I do cheat just a hair on this point in a couple different ways; see below.
- It can’t take me an inordinate amount of time to set up since I have a million things to do. So while digging into the Google Drive API and rolling my own solution from scratch sounds like fun, now isn’t the time for me to do that, so I was looking for solutions that will do as much of the work for me as possible.
- Since the Raspberry Pi is going to be sitting on a shelf somewhere not hooked up to a monitor, I’ll need some sort of notification that the backup worked, so if I don’t get notified I’ll know something’s up. This doesn’t need to be fancy, I just need to know it’s working.
- This is a last resort backup, so I’m not going to be doing any fancy file versioning or anything along those lines. The sole intent for this is to have a backup of the files if none exists anywhere else.
Step One: Configure the Raspberry Pi
Since I hadn’t messed with Raspberry Pis in a while and wasn’t sure what state the operating systems on the ones I have were in, I just grabbed one with a 64GB SD card and started from scratch.
- Download and install Etcher if you don’t already have a program you use to write images to SD cards
- Download Raspbian
- Use Etcher to write the Raspbian to the SD card
- Stick the SD card in the Raspberry Pi and boot it up
Next, we’ll want to install some updates and configure the Pi.
- Open a terminal
- sudo apt update && sudo apt upgrade
- sudo raspi-config
- You can change whatever settings you want here (root password, etc.), but the critical one is to make sure the clock sets from the network on boot, so …
- Select “Localisation Options”
- Change Locale as appropriate
- Select “Change Timezone” and set your timezone
- You’ll also likely want to go to “Advanced Options” and select “Expand Filesystem” so the OS will use your whole SD card (I’m not using an external hard drive in my setup; I just stuck a 64GB SD card in there which will be plenty to backup what I need)
- I had to change my keyboard layout from the default British layout to American; if you try to type an @ and see # instead, that’s a telltale sign you’re in the same boat.
- Since I may have multiple Raspberry Pis on my network soon I changed the hostname to something specific to this task.
- Choose “Update” to update the config tool to the latest version
- Reboot the Pi
Install and Configure overGrive
This is where I cheated one of my constraints but met another.
overGrive is a Google Drive client for Linux, and since Google doesn’t officially support one anymore, this one cost $5. That said, this also saved my tons of time so I met that aspect of my goals, and I don’t mind at all supporting people who build useful tools such as this.
Basically what the solution winds up being is I’ll use overGrive to sync the specific folder from Google Drive I want to back up, and then I’ll use good ol’ rsync to copy the files to another directory, thereby having a backup of what’s on Google Drive no matter what someone does to the files online.
Basically all you need to to for this step is follow the installation instructions here. It worked without a hitch for me.
Once overGrive is installed, do the following:
- Click on the Raspberry menu, then “Accessories,” then “overGrive”
- Click “OK” on the “Setup Required” screen
- Click “Connect account” — this will open up a Google login screen in a browser
- Log in with the Google account that has access to the Google Drive you want to back up. NOTE: overGrive does not seem to support “Shared With Me” Google Drive folders, so you’ll want to use the Google account that owns the files you want to back up.
- Accept the overGrive permissions
- Copy the code on the screen that reads “Please copy this code, switch to our application and paste it there”
- Switch back to the overGrive settings application
- Paste the code into the “Copy and paste the Google Drive Authentication code here” box
- Click “Validate”
- Click “Activate” if you bought an overGrive code, or click “Continue” if you want to try it out free for 14 days
- At this point you’re back on the “Setup Step 2 of 3” screen. Here, since I only want to back up one folder, I clicked the “Only sync certain folders to this computer” option and chose the folder I want to back up.
- Make sure “Auto Sync” is checked so overGrive will automatically stay in sync with changes on Google Drive.
- Click “Start Sync.” This will, as the button says, start the files syncing. According to the overGrive documentation it’s important that you let this finish so if you have a lot of files, this might be a good time to call it a night and check on things in the morning, when we’ll deal with the script to do the actual backup and send notifications.
The Backup and Archive Scripts
The “backup” concept I’m using for this little project is a rather braindead but effective one for my needs: copy the files from the directory that syncs with Google Drive to another directory, thereby having a copy of the files detached from Google Drive so if disaster strikes, we have an isolated copy.
To cover the unlikely scenario of perfect timing whereby the directory is empty when the directory sync happens, I’ll zip up a copy of everything nightly and set that aside, keeping two days worth of archives. (You could of course keep as many as you want but I’m limiting myself to two days since my SD card is only 64GB, and I didn’t want to attach a portable hard drive to the Raspberry Pi for these purposes.)
To provide a bit more detail and put all of this in steps to make it more clear:
- Since I’m a Python programmer the backup script will be in Python, and I’ll use dirsync (a Python implementation of a robocopy or rsync-type utility) to sync the Google Drive directory to my backup directory. dirsync only syncs changes so after the initial backup we can safely and efficiently run this hourly.
- So I know whether or not the sync worked, I’ll have the script notify me either way. I thought about having it only notify me if there are errors but if someone kicks the cord out of the Raspberry Pi (I’m looking at you, cats) not getting a notification will tell me something went wrong.
- Nightly a script will run that zips up the backup Google Drive directory and puts the zip files in an archives directory. This script will also delete files that are more than 48 hours old.
Brief Tangent: Notifications
I thought about using email for notifications since that’s the obvious, easy, lowest-common-denominator solution. But when I do little side projects like this I like to use them as an opportunity to learn something new, so I decided instead to use Pushover and have notifications come to my phone. (I thought briefly about looking into doing a Google Home or Alexa app but that was a bridge too far for this project.)
The long and short of Pushover is it’s a service that lets you send notifications to Android or iOS programmatically. So if you want to try this out just sign up for Pushover (they have a 7-day free trial), verify your email address, and then create an app to get the token you’ll need to use in the script.
Here again I cheated on the “has to be free” requirement and paid the one-time $5 fee for Pushover on my phone, not only to support them but because I may wind up using this on other similar projects.
Back to the Scripts
Refer to GitHub for the full details, but I do want to highlight a couple of things about the scripts.
First, they’re actually totally generic and have nothing directly to do with Google Drive, so the name of the project is probably a bit of a misnomer. overGrive handles the Google Drive sync, and these scripts are really just doing an rsync-style copy from one place to another, and periodically zipping up the results for a bit longer retention. It might be interesting at some future date to make this a truly comprehensive solution and leverage the Google Drive API to do the syncing from Google Drive, but I decided paying $5 for overGrive was a better choice at this particular juncture.
Second, by default the sync script will purge the target directory, meaning files that don’t exist in the source directory will be deleted from the target directory. I went back and forth a bit on that point but decided I wanted an accurate representation of the state of the Google Drive at the point at which the sync occurs, and I didn’t want deleted files cluttering up the backups. Again, people can always get deleted files from the trash on Google Drive unless someone empties the trash or explicitly permanently deletes a file. This is an easy setting to change if you want it to behave differently.
Conclusion
Raspberry Pi + overGrive + Pushover + some Python = success! I now have a handy setup for backing up Google Drive that all told probably took less time to put together than it took to write this blog post about it. I’d be interested to hear if others have solved this problem in different ways.