Created
October 17, 2012 14:05
-
-
Save danhigham/3905672 to your computer and use it in GitHub Desktop.
Example of how to use the vmc library to do an automatic mysql backup of a named service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require "rubygems" | |
| require "bundler/setup" | |
| require "vmc" | |
| require "cli" | |
| include VMC::Cli::TunnelHelper | |
| include VMC::Cli::ServicesHelper | |
| class BackupHelper < VMC::Cli::Command::Base | |
| def backup(service, output_file) | |
| ps = client.services | |
| info = ps.select { |s| s[:name] == service }.first | |
| port = pick_tunnel_port(10000) | |
| raise VMC::Client::AuthError unless @client.logged_in? | |
| if not tunnel_pushed? | |
| auth = UUIDTools::UUID.random_create.to_s | |
| push_caldecott(auth) | |
| bind_service_banner(service, tunnel_appname, false) | |
| start_caldecott | |
| else | |
| auth = tunnel_auth | |
| end | |
| if not tunnel_healthy?(auth) | |
| # We don't expect caldecott not to be running, so take the | |
| # most aggressive restart method.. delete/re-push | |
| client.delete_app(tunnel_appname) | |
| invalidate_tunnel_app_info | |
| push_caldecott(auth) | |
| bind_service_banner(service, tunnel_appname, false) | |
| start_caldecott | |
| end | |
| if not tunnel_bound?(service) | |
| bind_service_banner(service, tunnel_appname) | |
| end | |
| conn_info = tunnel_connection_info info[:vendor], service, auth | |
| start_tunnel(port, conn_info, auth) | |
| cmd = "mysqldump --protocol=TCP --host=localhost --port=#{port} --user=#{conn_info['user']} --password=#{conn_info['password']} #{conn_info['name']} > #{output_file}" | |
| puts cmd | |
| `#{cmd}` | |
| unbind_service_banner(service, tunnel_appname) | |
| end | |
| end | |
| service_name = ARGV[0] | |
| output_file = ARGV[1] | |
| b = BackupHelper.new | |
| b.backup(service_name, output_file) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| source :rubygems | |
| source "http://rubygems.org" | |
| gem 'vmc', '0.3.18' | |
| gem 'caldecott', '>=0.0.5' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment