Class: EasyBackup::Adapter::Db::PostgreSQL

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_backup/adapter/db/postgre_sql.rb

Instance Method Summary collapse

Instance Method Details

#database(database = nil) ⇒ Object



15
16
17
# File 'lib/easy_backup/adapter/db/postgre_sql.rb', line 15

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

#dump_file(file_name = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/easy_backup/adapter/db/postgre_sql.rb', line 31

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

#host(host = nil) ⇒ Object



11
12
13
# File 'lib/easy_backup/adapter/db/postgre_sql.rb', line 11

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

#password(password = nil) ⇒ Object



23
24
25
# File 'lib/easy_backup/adapter/db/postgre_sql.rb', line 23

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

#port(port = nil) ⇒ Object



27
28
29
# File 'lib/easy_backup/adapter/db/postgre_sql.rb', line 27

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

#send_to(storages) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/easy_backup/adapter/db/postgre_sql.rb', line 55

def send_to(storages)
  dump_file_name = path_to(dump_file)
  zip_file_name = path_to(zip_file)
  
  FileUtils.mkpath File.dirname(dump_file_name) unless Dir.exist? File.dirname(dump_file_name)

  EasyBackup.logger.info "[PostgreSQL] Dump postgres://#{username}:*****@#{host}:#{port}/#{database}\n#{' '*15}to #{dump_file_name}"

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

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

#username(username = nil) ⇒ Object



19
20
21
# File 'lib/easy_backup/adapter/db/postgre_sql.rb', line 19

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

#zipObject



51
52
53
# File 'lib/easy_backup/adapter/db/postgre_sql.rb', line 51

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

#zip_file(file_name = nil) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/easy_backup/adapter/db/postgre_sql.rb', line 43

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