Class: EasyBackup::Resources::Postgres

Inherits:
Object
  • Object
show all
Includes:
Zip
Defined in:
lib/easy_backup/resources/postgres.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Postgres

Returns a new instance of Postgres.



6
7
8
# File 'lib/easy_backup/resources/postgres.rb', line 6

def initialize(&block)
  instance_eval &block if block_given?
end

Instance Method Details

#database(database = nil) ⇒ Object



14
15
16
# File 'lib/easy_backup/resources/postgres.rb', line 14

def database(database=nil)
  database ? @database = database : @database
end

#dump_file(file_name = nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/easy_backup/resources/postgres.rb', line 30

def dump_file(file_name=nil)
  if file_name
    @dump_file = file_name
  else
    if @dump_file
      path_to(@dump_file.is_a?(Proc) ? @dump_file.call : @dump_file)
    else
      path_to "#{database}_#{Time.now.strftime('%Y%m%d%H%M%S')}.sql"
    end
  end
end

#host(host = nil) ⇒ Object



10
11
12
# File 'lib/easy_backup/resources/postgres.rb', line 10

def host(host=nil)
  host ? @host = host : (@host || 'localhost')
end

#password(password = nil) ⇒ Object



22
23
24
# File 'lib/easy_backup/resources/postgres.rb', line 22

def password(password=nil)
  password ? @password = password : @password
end

#port(port = nil) ⇒ Object



26
27
28
# File 'lib/easy_backup/resources/postgres.rb', line 26

def port(port=nil)
  port ? @port = port : (@port || 5432)
end

#send_to(*storages) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/easy_backup/resources/postgres.rb', line 54

def send_to(*storages)
  FileUtils.mkpath File.dirname(dump_file) unless Dir.exist? File.dirname(dump_file)

  EasyBackup.configuration.logger.info "[EasyBackup] Dump postgres://#{username}:*****@#{host}:#{port}/#{database} to #{dump_file}"

  Open3.popen3 "pg_dump -h #{host} -p #{port} -U #{username} #{database} > #{dump_file}" do |i, o, e, t|
    if t.value.success?
      if zip_file
        EasyBackup.configuration.logger.info "[EasyBackup] Zip #{dump_file} to #{zip_file}"
        ZipFile.open(zip_file, ZipFile::CREATE) do |zip|
          zip.add File.basename(dump_file), dump_file
        end
      end
      storages.each { |s| s.save(zip_file ? zip_file : dump_file) }
    else
      EasyBackup.configuration.logger.error "[EasyBackup] Error: #{e.readlines.join}"
    end
  end

  FileUtils.rm dump_file if File.exist? dump_file
  FileUtils.rm zip_file if zip_file && File.exist?(zip_file)
end

#username(username = nil) ⇒ Object



18
19
20
# File 'lib/easy_backup/resources/postgres.rb', line 18

def username(username=nil)
  username ? @username = username : (@username || 'postgres')
end

#zipObject



50
51
52
# File 'lib/easy_backup/resources/postgres.rb', line 50

def zip
  zip_file lambda { "#{File.basename(dump_file, '.*')}.zip" }
end

#zip_file(file_name = nil) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/easy_backup/resources/postgres.rb', line 42

def zip_file(file_name=nil)
  if file_name
    @zip_file = file_name
  else
    path_to(@zip_file.is_a?(Proc) ? @zip_file.call : @zip_file)
  end
end