Last Updated: November 02, 2017
· lukom

Backup DB and store on Google Drive

Backup DB is easy, but send backup file to Google Drive is another story. You should enable Drive API in your account settings and also create service account, and even then you cannot upload file directly to your drive – you should upload to service account and then grant permissions for your account. Maybe there is an easier way but I haven`t found it yet:

# lib/tasks/server_db.rake
namespace :server_db do
  desc 'Backup DB and upload to Google Drive'
  task :backup do
    pg_dump = 'pg_dump --no-acl --no-owner -U myproject_deploy myproject_production'
    timestamp ='%Y-%m-%d-%H%M%S')
    archive_path = "#{Rails.root}/db/myproject_production_#{timestamp}.sql.bz2"
    `cd /var/www/myproject/current && #{pg_dump} | bzip2 - - > #{archive_path}`

    require 'google/apis/drive_v2'
    ENV['GOOGLE_APPLICATION_CREDENTIALS'] = "#{Rails.root}/config/google_api_credentials.json"
    drive =
    drive.authorization = Google::Auth.get_application_default([Google::Apis::DriveV2::AUTH_DRIVE_FILE])

    metadata = {title: File.basename(archive_path, '.sql.bz2')}
    file = drive.insert_file(metadata, upload_source: archive_path, content_type: 'application/x-bzip2')

    perm_id = drive.get_permission_id_for_email('')
    perm = 'writer', id:, type: 'user')
    drive.insert_permission(, perm)

# Gemfile
gem 'google-api-client', require: false
// config/google_api_credentials.json
  "type": "service_account",
  "project_id": "myproject",
  "private_key_id": "20d9dae73987xxxxxxxxxxxx",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgxxxxxxxxxxx==\n-----END PRIVATE KEY-----\n",
  "client_email": "",
  "client_id": "103720xxxxxxxxxxx",
  "auth_uri": "",
  "token_uri": "",
  "auth_provider_x509_cert_url": "",
  "client_x509_cert_url": ""
# config/schedule.rb (whenever)
every(, at: '02:00') { rake 'server_db:backup' }

Also there is another idea of where to store DB dumps – in git repo! Because it stores only diffs so repo with bare dump.sql file will grow not so fast, and you can easily see the daily differences (if in your dump.sql file each db row is on its own line), and you can easily get free private git repo on But this is a different story.