Class: PgS3Dumper::Dumper

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_s3_dumper/dumper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Dumper

Returns a new instance of Dumper.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pg_s3_dumper/dumper.rb', line 18

def initialize(options)
  @database_url = options[:database_url] || ENV['DATABASE_URL']
  aws_region = options[:aws_region] || ENV['AWS_REGION']
  aws_key = options[:aws_key] || ENV['AWS_ACCESS_KEY_ID']
  aws_secret = options[:aws_secret] || ENV['AWS_SECRET_ACCESS_KEY']
  aws_url = options[:aws_url] || ENV['AWS_URL']
  @prune = options.has_key?(:prune) ? options[:prune] : false

  v = `pg_dump --version`
  v =~ /pg_dump \(PostgreSQL\) 9\.\d\.\d/ or raise Error, "pg_dump version 9.x.x is required and must be in the PATH"
  database_url or raise Error, "Database URL required."
  aws_url =~ %r{^s3://([^/]+)/(\S*?)/?$} or raise Error, "Invalid AWS URL"

  bucket_name = $1
  @prefix = "#{$2}/"

  database_url =~ %r{postgres://[^/]*/(.+)\??.*$} or raise Error, "Invalid database URL"
  @database_name = $1
  @prefix = File.join(prefix, database_name)

  aws_key && aws_secret or raise Error, "AWS key and secret required"

  cred = Aws::Credentials.new(aws_key, aws_secret)
  client = Aws::S3::Client.new(:credentials => cred, :region => aws_region)
  s3 = Aws::S3::Resource.new(:client => client)
  @bucket = s3.bucket(bucket_name)
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



9
10
11
# File 'lib/pg_s3_dumper/dumper.rb', line 9

def bucket
  @bucket
end

#database_nameObject (readonly)

Returns the value of attribute database_name.



9
10
11
# File 'lib/pg_s3_dumper/dumper.rb', line 9

def database_name
  @database_name
end

#database_urlObject (readonly)

Returns the value of attribute database_url.



9
10
11
# File 'lib/pg_s3_dumper/dumper.rb', line 9

def database_url
  @database_url
end

#prefixObject (readonly)

Returns the value of attribute prefix.



9
10
11
# File 'lib/pg_s3_dumper/dumper.rb', line 9

def prefix
  @prefix
end

Instance Method Details

#backupObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/pg_s3_dumper/dumper.rb', line 74

def backup
  backups = find_backups

  # Keep daily backups for one week
  # Keep weekly backups for one month
  # Keep monthly backups for one year
  7.times.map{|i| i.days.ago}.each do |ts|
    backups.each do |b|
      b.keep_on_day(ts)
      # We kept one backup on day ts, go to next day
      break if b.keep
    end
  end

  4.times.map{|i| i.weeks.ago}.each do |ts|
    backups.each do |b|
      b.keep_on_week(ts)
      # We kept one backup on day ts, go to next day
      break if b.keep
    end
  end

  12.times.map{|i| i.months.ago}.each do |ts|
    backups.each do |b|
      b.keep_on_month(ts)
      # We kept one backup on day ts, go to next day
      break if b.keep
    end
  end


  # Keep all backups for today
  backups.each do |b|
    b.keep_on_day(Date.today)
  end

  # Make new backup
  key = "#{now.iso8601}-#{database_name.split('/').last}.dmp"
  file = Tempfile.new(key)

  puts "## Creating backup"

  system "pg_dump -Fc #{database_url} > #{file.path}" or fail 'Cannot create dump'

  obj = bucket.object(File.join(prefix, key))

  obj.upload_file(file.path, :metadata => {:backup_id => Backup.generate_id})
  file.close
  file.unlink
  bck = Backup.new(obj)
  puts "Created backup: #{bck}"

  if prune?
    puts "## Pruning old backups"
    backups.each do |b|
      b.prune
    end
  end
end

#cleanupObject



68
69
70
71
72
# File 'lib/pg_s3_dumper/dumper.rb', line 68

def cleanup
  find_backups.each do |b|
    b.delete
  end
end

#listObject



59
60
61
62
63
64
65
66
# File 'lib/pg_s3_dumper/dumper.rb', line 59

def list
  f = "% 12s% 30s"
  puts f % ['id', 'date']
  puts "-" * 42
  find_backups.each do |b|
    puts f % [b.short_id, b.ts]
  end
end

#nowObject



134
135
136
# File 'lib/pg_s3_dumper/dumper.rb', line 134

def now
  Time.now.utc
end

#prune?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/pg_s3_dumper/dumper.rb', line 14

def prune?
  @prune
end

#run(command) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pg_s3_dumper/dumper.rb', line 46

def run(command)
  case command
  when :list
    list
  when :backup
    backup
  when :cleanup
    cleanup
  else
    raise Error, "Invalid command '#{command}'"
  end
end