Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sunkencity/451320 to your computer and use it in GitHub Desktop.
Save sunkencity/451320 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# backup script for postgresql databases
# connects via ssh and copies the file to a local path
@ssh_host = ""
@ssh_user = ""
@ssh_pass = ""
@db_name = ""
@db_host = ""
@db_user = ""
@db_pass = ""
@local_backup_path = ""
require 'rubygems'
require 'net/ssh'
require 'net/scp'
@file = "#{@db_name}_#{Time.now.strftime("%Y_%m.%d_%H.%M")}.sql.bz2"
@remote_file = "/tmp/#{@file}"
@backup_command = "pg_dump --clean --no-owner --no-privileges -U#{@db_user} -h#{@db_host} #{@db_name} | bzip2 > #{@remote_file}"
Net::SSH.start(@ssh_host, @ssh_user, :password => @ssh_pass) do |ssh|
ssh.exec!(@backup_command) { |ch, stream, out| ch.send_data "#{@db_pass}\n" if out =~ /^Password:/ }
Net::SCP.new(ssh).download(@remote_file, "#{@local_backup_path}#{@file}")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment