Class: DB2S3

Inherits:
Object
  • Object
show all
Defined in:
lib/db2s3.rb

Defined Under Namespace

Classes: Config, S3Store

Instance Method Summary collapse

Constructor Details

#initializeDB2S3

Returns a new instance of DB2S3.



13
14
# File 'lib/db2s3.rb', line 13

def initialize
end

Instance Method Details

#cleanObject

TODO: This method really needs specs



29
30
31
32
33
# File 'lib/db2s3.rb', line 29

def clean
  files = file_objects_from_paths(store.list("#{dump_file_name_prefix}-"))
  determine_what_to_keep files
  delete_surplus_backups files
end

#delete_surplus_backups(files) ⇒ Object



63
64
65
66
67
# File 'lib/db2s3.rb', line 63

def delete_surplus_backups files
  files.each do |file|
    store.delete(file[:path]) unless file[:keep]
  end
end

#determine_what_to_keep(files) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/db2s3.rb', line 44

def determine_what_to_keep files
  # Keep all backups from the past day
  files.select {|x| x[:date] >= 1.day.ago }.map! do |backup_for_day|
    backup_for_day[:keep] = true
  end
  # Keep one backup per day from the last week
  files.select {|x| x[:date] >= 1.week.ago }.group_by {|x| x[:date].strftime("%u") }.values.map! do |backups_for_last_week|
    backups_for_last_week.sort_by{|x| x[:path] }.first[:keep] = true
  end
  # Keep one backup per week from the last 28 days
  files.select {|x| x[:date] >= 28.days.ago }.group_by {|x| x[:date].strftime("%Y%W") }.values.map! do |backups_for_last_28_days|
    backups_for_last_28_days.sort_by{|x| x[:path] }.first[:keep] = true
  end
  # Keep one backup per month since forever
  files.group_by {|x| x[:date].strftime("%Y%m") }.values.map! do |backups_for_month|
    backups_for_month.sort_by{|x| x[:path] }.first[:keep] = true
  end
end

#file_objects_from_paths(paths) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/db2s3.rb', line 35

def file_objects_from_paths paths
  paths.collect do |path| {
      :path => path,
      :date => Time.parse(path.split('-').last.split('.').first),
      :keep => false
    }
  end
end

#full_backupObject



16
17
18
19
20
# File 'lib/db2s3.rb', line 16

def full_backup
  file_name = dump_file_name(Time.now)
  store.store(file_name, open(dump_db.path))
  store.store(most_recent_dump_file_name, file_name)
end

#restoreObject



22
23
24
25
26
# File 'lib/db2s3.rb', line 22

def restore
  dump_file_name = store.fetch(most_recent_dump_file_name).read
  file = store.fetch(dump_file_name)
  run "gunzip -c #{file.path} | mysql #{mysql_options}"
end

#statisticsObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/db2s3.rb', line 69

def statistics
    # From http://mysqlpreacher.com/wordpress/tag/table-size/
  results = ActiveRecord::Base.connection.execute(<<-EOS)
  SELECT
    engine,
    ROUND(data_length/1024/1024,2) total_size_mb,
    ROUND(index_length/1024/1024,2) total_index_size_mb,
    table_rows,
    table_name article_attachment
    FROM information_schema.tables
    WHERE table_schema = '#{db_credentials[:database]}'
    ORDER BY total_size_mb + total_index_size_mb desc;
  EOS
  rows = []
  results.each {|x| rows << x.to_a }
  rows
end