views:

44

answers:

0

I am trying to connect with Amazon S3 I have the aws-s3 (0.6.2) gem installed. But I keep getting the following error:

undefined method `xml_in' for nil:NilClass

I can't find out what's the problem, also several searches on Google turns out in to nothing. I hope someone here can tell me more about this error.

My code:

require 'rake'
require 'tasks/rake_helper'
require 'aws/s3'

BACKUP_BUCKET = ''

namespace :db do

  desc "Backs up db and uploads to s3"
  task :backup => :environment do
    puts "Back up started @ #{Time.now}"
    timestamp = Time.now.to_i

    puts "Creating postgres dump..."
    pg_dump(timestamp)

    puts "Compressing backup..."
    tar_filename = "bkp.#{timestamp}.tar.gz"
    `tar -czf tmp/#{tar_filename} tmp/#{timestamp}*`

    puts "Preparing to upload #{tar_filename} to S3..."

    AWS::S3::Base.establish_connection!(
      :access_key_id => '', 
      :secret_access_key => ''
    )

    begin
      bucket = AWS::S3::Bucket.find BACKUP_BUCKET
      puts "Bucket found."
    rescue AWS::S3::NoSuchBucket
      puts "Bucket not found, creating it..."
      AWS::S3::Bucket.create BACKUP_BUCKET
      bucket = AWS::S3::Bucket.find BACKUP_BUCKET
    end

    puts "Starting upload..."
    AWS::S3::S3Object.store tar_filename, File.read("tmp/#{tar_filename}"), bucket.name, :content_type => 'application/x-gzip'

    #s3_upload(BACKUP_BUCKET, "tmp/#{tar_filename}")

    puts "Cleaning up"
    `rm tmp/#{timestamp}*`

    puts "Done @ #{Time.now}"
  end

end

pg_dump function:

def pg_dump(timestamp)
  settings = get_db_settings
  filename = "tmp/#{timestamp}.pgdump"

  ENV['PGPASSWORD'] = settings[:password]
  `pg_dump -i -h #{settings[:host]} -U #{settings[:username]} -F c #{settings[:db_name]} > #{filename}`
  filename
end