Created
June 24, 2010 11:06
-
-
Save sunkencity/451320 to your computer and use it in GitHub Desktop.
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 | |
# 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