Love it or hate it, but Google Drive/Docs are two very popular products used by tons of businesses. There is a lot to like (collaborative editing, access from everywhere, to name my two favorite features), but there is one thing I really hate about Google Drive: it doesn’t make it easy to backup its contents.
Google provides some tools that make it easy for a personal account. For example
– Google Takeout is great, but it only allows to backup one specific account, it cannot be used to backup all files from all users.
– Google Drive for Desktop has an option to mirror the remote contents, instead of the default streaming mode. However, just like many other approaches to backuping GDrive, it only creates and illusion of your files being safely backed up.
Not sure what I mean? Fire up a plain text editor and take a look at one of those .gdoc or .gsheet files you have “backed up” from Google Drive. You will see that all these files are very small and only contain the information about where to find the file on Google Drive, but not the file contents itself. This means that if anything will happen to your GDrive account, this file will be absolutely useless and you will lose all your real document content.
There are some commercial solutions that solve this problem that involve installing sketchy programs with a very broad access permissions. Personally, I won’t give such access to any 3rd party program except for some very reputable solutions (and I don’t know any “reputable” enough solution at the moment). For this reason I wrote a shell script that would allow you to
– scan directory (say, mirror of your google drive)
– find all .gdoc, .gsheet and .gslides files
– export them all and save a local copy of the actual file contents as .docx/.xlsx files that you will be able to edit with Libre Office, Microsoft Office or other similar programs
Here is how to use it in a simple 15-step process :) (Yes, there are a lot of steps thanks to google security mechanisms, but they are very simple)
- Go to https://developers.google.com/oauthplayground/
- In the left sidebar you will see a long list of available APIs. Find the category called “Drive API v3” and expand it.
- In that category click/check the line that looks like “https://www.googleapis.com/auth/drive”. Here is how this will look like:
- Click the blue button called “Authorize APIs”
- This will bring you to the page where you will need to select the account from which you want to backup the google drive files. In most cases this would be the admin of the Google Drive for Business account.
- On the next page check the checkbox called “See, edit, create, and delete all of your Google Drive files” and click Continue.
- This will bring you back to the original Oauth 2.0 Playground page, but this time you will be on “Step 2. Exchange authorization code for tokens”, as shown below:
- Click “Exchange authorization code for tokens” button and this will fill the Refresh token and Access token fields.
- For the backup script to work you will only need the “Access token”. Note that this token only lasts for 24 hours. If your is close to expiration, you can click “Refresh access token”. You may need to start from step 2 in order to do this.
- After hitting “Exchange authorization code for tokens” the interface may automatically switch to step 3 “configure request to API”, in which case simply go back to step 2.
IMPORTANT: under no circumstances you should share any of the tokens on this page with unauthorized users and programs. These tokens provide full access to the Google Drive files. Keep these safe - Get the script code:
- Either clone this repository:
git clone https://github.com/craftus/fetch-google-docs.git
- Or just copypaste the script to a local file
- Either clone this repository:
- Make sure the script is executable:
chmod +x fetch_google_docs.sh
- Run it as
fetch_google_docs.sh <GoogleDriveDir> <DownloadDir> <GoogleDriveApiToken>
where- GoogleDriveDir is the directory where GoogleDrive keeps its files on your local disk. GDrive can either “Stream files” or “Mirror files”. Make sure that you’re mirroring files by going to Google Drive desktop utility preferences and selecting this option. This will ensure that all files you want to backup are actually in place
- DownloadDir is the directory where the script will store the downloaded files
- GoogleDriveApiToken is token you obtained in the first 10 steps of this guide.
- That’s it. Just let the script do its job and you will get local editable copies of the google docs with the real contents. You can also start sleeping better knowing that even if something will happen to you GDrive account, you have your content safely backed up off GDrive.
Please feel free to contribute into the script development. The last 3 lines of the script provide the mapping between the GDrive files and their export formats. If you want to export something besides the 3 types of docs currently supported, just add the line for the new file type and submit a pull request, I’ll be happy to merge it.