Where do you keep backups? I guess that depends on what do you backup. You might have a very clever answer for a business critical data but what about less important content? The best example would be a private blog. It will hurt if you lose your data but the odds are you’re not willing to pay for any reliable storage. If you care enough to backup it’s going to be to another server (if you own one), your own laptop or external hard drive. S3 is gaining popularity but not everybody have and want to open account on Amazon. On the other hand there is one reliable storage, which is 100% free and almost everybody have access to it. Yes, I’m talking about Google Drive.
In order to integrate your program with Google Drive you need to create a Google API project. You can do it at Google console page.
Press “Create Project” button and select services you want to use with the project. For the purpose of this example “Drive API” is enough.
Once the service is enabled click on API Access from the left hand side navigation.
Click on “Create an OAuth 2.0 client ID” button. Make up a project name and click “next”. On the Client ID settings page choose “Service Account” as the application type.
Press “Create client ID” button. Click “Download private key” to download… you guess it – private key! You need it to access your account. Bear in mind you can download it only once.
Now your service account it created. You will need the client id and email address in a second. Leave the Google console page open.
There is one important thing you need to be aware of. Service account is not your Google account. If you upload files to the service account’s drive you won’t see them in your Google Drive. It’s not a big problem because the uploaded files can be shared.
If for some reason you need to have files uploaded directly to your account you can’t use the service account. You will have to create a web application instead. That change the way how you authenticate. Web application requires a manual journey though OAuth. Backups usually work in background and there is no web interface for OAuth redirections. For that reason I prefer to use a private key.
Now when your API project is created you can download an example script I prepared for this post. It’s a command line utility in PHP which uploads a file to shared folder on Google Drive. It’s available on my GitHub account: cp2goole. For your convenience the script comes with Google API but don’t use it with your projects. Download the latest API with examples from the official page https://developers.google.com/drive/quickstart-php.
$ git clone https://github.com/lukaszkujawa/cp2google.git $ cd cp2google/ $ vim cp2google.php
You will have to modify first lines of the script.
<?php define( 'BACKUP_FOLDER', 'PHPBackups' ); define( 'SHARE_WITH_GOOGLE_EMAIL', 'firstname.lastname@example.org' ); define( 'CLIENT_ID', '700692987478.apps.googleusercontent.com' ); define( 'SERVICE_ACCOUNT_NAME', 'email@example.com' ); define( 'KEY_PATH', '../866a0f5841d09660ac6d4ac50ced1847b921f811-privatekey.p12');
BACKUP_FOLDER – name of shared folder. The script will create it at the first run.
SHARE_WITH_GOOGLE_EMAIL – your google account.
CLIENT_ID – your project’s client id
SERVICE_ACCOUNT_NAME – your project’s account name. It’s called e-mail address on the console page.
KEY_PATH – path to the downloaded private key.
Replace those values to match your configuration. Save changes are run the file.
$ php cp2google.php README.md Uploading README.md to Google Drive Creating folder... File: 0B9_ZqV369SiSM19KbTROWldqcFk created
Now check your Google Drive. You should find a new folder in the “Shared with me” section. You also should receive an e-mail saying that the file has been shared with you.
I won’t get though the code because it’s quite simple to understand. The only thing worth mentioning is that on Google Drive files and folders are the same thing. Folder has a specific mime type which is “application/vnd.google-apps.folder”.
Google was always very generous when it comes down to storage. There are multiple ways to take advantage of that and backups are one of them. I wouldn’t use it to store business critical data but everything else should be just fine. It feels much more convenient then anything else.