Skip to content

Commit

Permalink
added PostgreSQL support
Browse files Browse the repository at this point in the history
  • Loading branch information
markmansour committed Apr 25, 2009
1 parent 80cac2b commit e0097c8
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ coverage
rdoc
pkg
tmp
*~
17 changes: 14 additions & 3 deletions README.markdown
@@ -1,7 +1,7 @@
astrails-safe
=============

Simple mysql and filesystem backups with S3 support (with optional encryption)
Simple database and filesystem backups with S3 support (with optional encryption)

Motivation
----------
Expand All @@ -12,6 +12,7 @@ We needed a backup solution that will satisfy the following requirements:
* simple to install and configure
* support for simple ‘tar’ backups of directories (with includes/excludes)
* support for simple mysqldump of mysql databases
* support for simple pg_dump of PostgreSQL databases
* support for symmetric or public key encryption
* support for local filesystem and Amazon S3 for storage
* support for backup rotation. we don’t want backups filling all the diskspace or cost a fortune on S3
Expand Down Expand Up @@ -62,7 +63,7 @@ The procedure to create and transfer the key is as follows:

4. import public key on the remote system:
<pre>
$ gpg --import test@example.com.pub
$ gpg --import test@example.com.pub
gpg: key 45CA9403: public key "Test Backup <test@example.com>" imported
gpg: Total number processed: 1
gpg: imported: 1
Expand All @@ -83,7 +84,7 @@ The procedure to create and transfer the key is as follows:
4 = I trust fully
5 = I trust ultimately
m = back to the main menu

Your decision? 5
...
Command> quit
Expand Down Expand Up @@ -136,6 +137,16 @@ Example configuration

end

pgdump do
options "-i -x -O" # -i => ignore version, -x => do not dump privileges (grant/revoke), -O => skip restoration of object ownership in plain text format

user "username"
password "............" # shouldn't be used, instead setup ident. Current functionality exports a password env to the shell which pg_dump uses - untested!

database :blog
database :stateofflux_com
end

tar do
archive "git-repositories", :files => "/home/git/repositories"
archive "dot-configs", :files => "/home/*/.[^.]*"
Expand Down
6 changes: 3 additions & 3 deletions Rakefile
Expand Up @@ -5,11 +5,11 @@ begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "safe"
gem.summary = %Q{Backup filesystem and MySQL to Amazon S3 (with encryption)}
gem.description = "Simple tool to backup MySQL databases and filesystem locally or to Amazon S3 (with optional encryption)"
gem.summary = %Q{Backup filesystem and databases (MySQL and PostgreSQL) to Amazon S3 (with encryption)}
gem.description = "Simple tool to backup databases (MySQL and PostgreSQL) and filesystem locally or to Amazon S3 (with optional encryption)"
gem.email = "we@astrails.com"
gem.homepage = "http://github.com/astrails/safe"
gem.authors = ["Astrails Ltd."]
gem.authors = ["Astrails Ltd.", "Mark Mansour"]
gem.files = FileList["[A-Z]*.*", "{bin,examples,generators,lib,rails,spec,test,templates}/**/*", 'Rakefile', 'LICENSE*']

gem.add_dependency("aws-s3")
Expand Down
4 changes: 2 additions & 2 deletions VERSION.yml
@@ -1,4 +1,4 @@
---
---
:major: 0
:minor: 1
:patch: 6
:patch: 7
49 changes: 49 additions & 0 deletions examples/unit/config_example.rb
@@ -1,6 +1,55 @@
require File.expand_path(File.dirname(__FILE__) + '/../example_helper')

describe Astrails::Safe::Config do
it "pgdump" do
config = Astrails::Safe::Config::Node.new do
local do
path "path"
end

pgdump do
# `pg_dump -U "#{abcs[RAILS_ENV]["username"]}" -i -x -O #{abcs[RAILS_ENV]["database"]} -f db/#{filename}`
options "-i -x -O"

user "astrails"
password ""
host "localhost"
port 5432

database :blog

database :production do
keep :local => 3

skip_tables [:logger_exceptions, :request_logs]
end

end
end

expected = {
"local" => {"path" => "path"},

"pgdump" => {
"options" => "-i -x -O",
"user" => "astrails",
"password" => "",
"host" => "localhost",
"port" => 5432,

"databases" => {
"blog" => {},
"production" => {
"keep" => {"local" => 3},
"skip_tables" => ["logger_exceptions", "request_logs"],
},
},
}
}

config.to_hash.should == expected
end

it "foo" do
config = Astrails::Safe::Config::Node.new do
local do
Expand Down
2 changes: 2 additions & 0 deletions lib/astrails/safe.rb
Expand Up @@ -8,6 +8,7 @@

require 'astrails/safe/source'
require 'astrails/safe/mysqldump'
require 'astrails/safe/pgdump'
require 'astrails/safe/archive'

require 'astrails/safe/pipe'
Expand All @@ -32,6 +33,7 @@ def safe(&block)
#config.dump

Astrails::Safe::Mysqldump.run(config[:mysqldump, :databases])
Astrails::Safe::Pgdump.run(config[:pgdump, :databases])
Astrails::Safe::Archive.run(config[:tar, :archives])

Astrails::Safe::TmpFile.cleanup
Expand Down
2 changes: 1 addition & 1 deletion lib/astrails/safe/config/builder.rb
Expand Up @@ -3,7 +3,7 @@ module Safe
module Config
class Builder
COLLECTIONS = %w/database archive/
ITEMS = %w/s3 key secret bucket path gpg password keep local mysqldump options
ITEMS = %w/s3 key secret bucket path gpg password keep local mysqldump pgdump options
user host port socket skip_tables tar files exclude filename/
NAMES = COLLECTIONS + ITEMS
def initialize(node)
Expand Down
43 changes: 43 additions & 0 deletions lib/astrails/safe/pgdump.rb
@@ -0,0 +1,43 @@
module Astrails
module Safe
class Pgdump < Source

def command
@commanbd ||= "pg_dump #{postgres_options} #{postgres_username} #{postgres_password} #{postgres_host} #{postgres_port} #{@id}"
# @commanbd ||= "pg_dump -U #{@config["user"]} #{@config[:option]} #{@config["database"]} -f #{filename}"
# @commanbd ||= "mysqldump --defaults-extra-file=#{mysql_password_file} #{@config[:options]} #{mysql_skip_tables} #{@id}"
end

def extension; '.sql'; end

protected

def postgres_options
@config[:options]
end

def postgres_host
@config["host"] ? "--host='#{@config["port"]}'" : ""
end

def postgres_port
@config["port"] ? "--port='#{@config["port"]}'" : ""
end

def postgres_username
@config["user"] ? "--username='#{@config["user"]}'" : ""
end

def postgres_password
`export PGPASSWORD=#{@config["password"]}` if @config["password"]
end

def postgres_skip_tables
if skip_tables = @config[:skip_tables]
[*skip_tables].map { |t| "--exclude-table=#{@id}.#{t}" } * " "
end
end

end
end
end
10 changes: 5 additions & 5 deletions safe.gemspec
Expand Up @@ -2,13 +2,13 @@

Gem::Specification.new do |s|
s.name = %q{safe}
s.version = "0.1.6"
s.version = "0.1.7"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Astrails Ltd."]
s.date = %q{2009-04-16}
s.authors = ["Astrails Ltd.", "Mark Mansour"]
s.date = %q{2009-04-24}
s.default_executable = %q{astrails-safe}
s.description = %q{Simple tool to backup MySQL databases and filesystem locally or to Amazon S3 (with optional encryption)}
s.description = %q{Simple tool to backup MySQL and PostgreSQL databases and filesystem locally or to Amazon S3 (with optional encryption)}
s.email = %q{we@astrails.com}
s.executables = ["astrails-safe"]
s.extra_rdoc_files = ["README.markdown", "LICENSE"]
Expand All @@ -18,7 +18,7 @@ Gem::Specification.new do |s|
s.rdoc_options = ["--inline-source", "--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.1}
s.summary = %q{Backup filesystem and MySQL to Amazon S3 (with encryption)}
s.summary = %q{Backup filesystem and database (MySQL and PostgreSQL) to Amazon S3 (with encryption)}

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
Expand Down
13 changes: 13 additions & 0 deletions templates/script.rb
Expand Up @@ -72,6 +72,19 @@

end

# # uncomment to enable
# # backup PostgreSQL databases with pg_dump
# pgdump do
# option "-i -x -O"
#
# user "markmansour"
# # password "" - leave this out if you have ident setup
#
# # database is a 'collection' element. it must have a hash or block parameter
# # it will be 'collected' in a 'databases', with database id (1st arg) used as hash key
# database :blog
# database :production
# end

tar do
# 'archive' is a collection item, just like 'database'
Expand Down

0 comments on commit e0097c8

Please sign in to comment.