Skip to content

Instantly share code, notes, and snippets.

@guycall
Created April 19, 2014 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guycall/11088683 to your computer and use it in GitHub Desktop.
Save guycall/11088683 to your computer and use it in GitHub Desktop.
Very quick and dirty Ruby script to download a backup of AppFog database via the AppFog API
# Purpose: Download a full export of an AppFog database
# API details taken from https://github.com/lucperkins/appfog-api-docs
require 'json'
require 'httparty'
require 'zip'
# Fetch your AppFog token
# You may need to run the command 'af login' if your token has expired
file_contents = File.read('/home/vagrant/.af_token')
parsed = JSON.parse(file_contents)
af_token = parsed["https://api.appfog.com"]
# Trigger database export & get the url (i.e. equivalent to 'af export-service SERVICENAME')
response = HTTParty.get "https://api.appfog.com/services/export/YOURSERVICENAME",
:headers => {"Authorization" => "#{af_token}"}
uri = response.parsed_response["uri"]
# Download the file
root_filename = "database_backup_" + Time.now.strftime("%Y_%m%d_%H%M%S")
File.open(root_filename + ".zip", "wb") do |f|
f.write HTTParty.get(uri).parsed_response
end
# Extract the actual backup file from the zip
Zip::ZipFile.open(root_filename + ".zip") do |zipfile|
zipfile.each do |file|
if file.name[-7..-1] == '.sql.gz'
file.extract (root_filename + ".sql.gz")
end
end
end
# The MIT License (MIT)
# Copyright (c) 2014 guycall
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment