class NixAdm::PostgreSQL::Import

This class imports everything exported from the Export class. Before importing, you need to make sure you initialize your database with proper encoding and collation. To do so, switch user to postgres. Then run:

/usr/local/bin/initdb --pgdata=/var/lib/pgsql9/data/ -E 'UTF-8' \
  --lc-collate='en_US.UTF-8' --lc-ctype='en_US.UTF-8';

/usr/local/bin/createuser -sl root

Then start Postgres. Now you can import databases

Public Class Methods

new(host, options = {}) click to toggle source
Calls superclass method NixAdm::Backup::new
# File src/lib/nixadm/db/postgresql.rb, line 170
def initialize(host, options = {})
  super host, options: options

  @ssh_opts = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
end

Public Instance Methods

import_databases() click to toggle source
# File src/lib/nixadm/db/postgresql.rb, line 188
def import_databases()
  dbs = Dir.entries($backup_dir)

  # Restore globals first
  system "psql -p 5433 -d postgres -f #{$backup_dir}/globals.sql"

  dbs.each do |file|
    next if File.extname(file) != '.db'

    db = file.split('.')[0]
    puts "Loading #{db}"

    sql   = %q{ select count(*) from pg_database where datname='dba' }
    count = %x{ psql -p 5433 postgres -At -c "#{sql}" }
    count.strip!

    if count == '1'
      %x{ psql -p 5433 postgres -c 'drop database #{db}' }
    end

    %x{ psql -p 5433 postgres -c 'create database #{db}' }
    %x{ psql -p 5433 -d #{db} -f #{$backup_dir}/#{file}  }
    %x{ psql -p 5433 #{db}    -c 'analyze' }
  end
end
run() click to toggle source
# File src/lib/nixadm/db/postgresql.rb, line 180
def run()
  begin
    import_databases()
  rescue Exception => ex
    log "Import failed: #{ex.to_s}\n #{ex.backtrace}"
  end
end