Class: EyBackup::MysqlBackup

Inherits:
Object
  • Object
show all
Defined in:
lib/ey-backup/mysql_backup.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ MysqlBackup

Returns a new instance of MysqlBackup.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ey-backup/mysql_backup.rb', line 26

def initialize(opts={})
  AWS::S3::Base.establish_connection!(
      :access_key_id     => opts[:aws_secret_id],
      :secret_access_key => opts[:aws_secret_key]
    )
  @dbuser = opts[:dbuser]
  @dbpass = opts[:dbpass]
  @databases = opts[:databases]
  @keep = opts[:keep]
  @bucket = "ey-backup-#{Digest::SHA1.hexdigest(opts[:aws_secret_id])[0..11]}"
  @tmpname = "#{Time.now.strftime("%Y-%m-%dT%H:%M:%S").gsub(/:/, '-')}.sql.gz"
  @id = EyBackup.get_from_ec2('/instance-id')
  FileUtils.mkdir_p '/mnt/tmp'
  begin
    AWS::S3::Bucket.create @bucket
  rescue AWS::S3::BucketAlreadyExists
  end  
end

Instance Method Details

#backup_database(database) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ey-backup/mysql_backup.rb', line 51

def backup_database(database)
  mysqlcmd = "mysqldump -u #{@dbuser} -p'#{@dbpass}' #{database} | gzip - > /mnt/tmp/#{database}.#{@tmpname}"
  if system(mysqlcmd)
    AWS::S3::S3Object.store(
       "/#{@id}.#{database}/#{database}.#{@tmpname}",
       open("/mnt/tmp/#{database}.#{@tmpname}"),
       @bucket,
       :access => :private
    )
    FileUtils.rm "/mnt/tmp/#{database}.#{@tmpname}"
    puts "successful backup: #{database}.#{@tmpname}"
  else
    raise "Unable to dump database#{database} wtf?"
  end
end

#cleanupObject



91
92
93
94
95
96
# File 'lib/ey-backup/mysql_backup.rb', line 91

def cleanup
  list('all',false)[0...-(@keep*@databases.size)].each{|o| 
    puts "deleting: #{o.key}"  
    o.delete
  }
end

#download(index) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ey-backup/mysql_backup.rb', line 67

def download(index)
  idx, db = index.split(":")
  obj =  list(db)[idx.to_i]
  puts "downloading: #{normalize_name(obj)}"
  File.open(normalize_name(obj), 'wb') do |f|
    print "."
    obj.value {|chunk| f.write chunk }
  end
  puts
  puts "finished"
  normalize_name(obj)
end

#list(database = 'all', printer = false) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ey-backup/mysql_backup.rb', line 102

def list(database='all', printer = false)
  puts "listing #{database} database" if printer
  backups = []
  if database == 'all'
    @databases.each do |db|
      backups << AWS::S3::Bucket.objects(@bucket, :prefix => "#{@id}.#{db}")
    end
    backups = backups.flatten.sort
  else  
    backups = AWS::S3::Bucket.objects(@bucket, :prefix => "#{@id}.#{database}").sort
  end
  if printer
    backups.each_with_index do |b,i|
      puts "#{i}:#{database} #{normalize_name(b)}"
    end
  end    
  backups
end

#new_backupObject



45
46
47
48
49
# File 'lib/ey-backup/mysql_backup.rb', line 45

def new_backup
  @databases.each do |db|
    backup_database(db)
  end  
end

#normalize_name(obj) ⇒ Object



98
99
100
# File 'lib/ey-backup/mysql_backup.rb', line 98

def normalize_name(obj)
  obj.key.gsub(/^.*?\//, '')
end

#restore(index) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/ey-backup/mysql_backup.rb', line 80

def restore(index)
  name = download(index)
  db = name.split('.').first
  cmd = "gunzip -c #{name} | mysql -u #{@dbuser} -p'#{@dbpass}' #{db}"
  if system(cmd)
    puts "successfully restored backup: #{name}"
  else
    puts "FAIL"
  end    
end