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
# File 'lib/easy_backup/resources/postgres.rb', line 30

def dump_file(file_name=nil)
  if file_name
    @dump = file_name
  else
    @dump_file || update_dump_file
  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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/easy_backup/resources/postgres.rb', line 46

def send_to(*storages)
  update_dump_file

  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 update_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



42
43
44
# File 'lib/easy_backup/resources/postgres.rb', line 42

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

#zip_fileObject



38
39
40
# File 'lib/easy_backup/resources/postgres.rb', line 38

def zip_file
  @zip_file || update_zip_file
end