db2s3 0.2.6 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. data/HISTORY +4 -1
  2. data/README +2 -4
  3. data/VERSION +1 -1
  4. data/lib/db2s3.rb +44 -0
  5. data/tasks/tasks.rake +25 -20
  6. metadata +2 -2
data/HISTORY CHANGED
@@ -1,4 +1,7 @@
1
- 0.2.6
1
+ 0.3.0 (6 Dec 2009)
2
+ - Added db2s3:backup:clean task to delete old backups
3
+ - Added dependency on activesupport for the clean task
4
+ 0.2.6 (6 Dec 2009)
2
5
  - Remove metrics task, since it was far too inaccurate
3
6
  - Add statistics task to show you the size of your tables
4
7
  - Only add username to mysql command line if provided in database.yml
data/README CHANGED
@@ -27,11 +27,9 @@ Usage:
27
27
  rake db2s3:backup:incremental # Unimplemented
28
28
 
29
29
  # Handy tasks
30
- rake db2s3:metrics # Estimated costs
30
+ rake db2s3:statistics # Shows you the size of your DB
31
31
  rake db2s3:backup:restore # You should be testing this regularly
32
-
33
- Caveats:
34
- Currently does not clean up old back ups
32
+ rake db2s3:backup:clear # Clean up old backups - cron this
35
33
 
36
34
  Kudos:
37
35
  http://github.com/pauldowman/blog_code_examples/tree/master/mysql_s3_backup
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.6
1
+ 0.3.0
@@ -1,3 +1,4 @@
1
+ require 'activesupport'
1
2
  require 'aws/s3'
2
3
  require 'tempfile'
3
4
 
@@ -20,6 +21,38 @@ class DB2S3
20
21
  run "gunzip -c #{file.path} | mysql #{mysql_options}"
21
22
  end
22
23
 
24
+ # TODO: This method really needs specs
25
+ def clean
26
+ to_keep = []
27
+ filelist = store.list
28
+ files = filelist.reject {|file| file.ends_with?(most_recent_dump_file_name) }.collect do |file|
29
+ {
30
+ :path => file,
31
+ :date => Time.parse(file.split('-').last.split('.').first)
32
+ }
33
+ end
34
+ # Keep all backups from the past day
35
+ files.select {|x| x[:date] >= 1.day.ago }.each do |backup_for_day|
36
+ to_keep << backup_for_day
37
+ end
38
+
39
+ # Keep one backup per day from the last week
40
+ files.select {|x| x[:date] >= 1.week.ago }.group_by {|x| x[:date].strftime("%Y%m%d") }.values.each do |backups_for_last_week|
41
+ to_keep << backups_for_last_week.sort_by{|x| x[:date].strftime("%Y%m%d") }.first
42
+ end
43
+
44
+ # Keep one backup per week since forever
45
+ files.group_by {|x| x[:date].strftime("%Y%W") }.values.each do |backups_for_week|
46
+ to_keep << backups_for_week.sort_by{|x| x[:date].strftime("%Y%m%d") }.first
47
+ end
48
+
49
+ to_destroy = filelist - to_keep.uniq.collect {|x| x[:path] }
50
+ to_destroy.delete_if {|x| x.ends_with?(most_recent_dump_file_name) }
51
+ to_destroy.each do |file|
52
+ store.delete(file.split('/').last)
53
+ end
54
+ end
55
+
23
56
  private
24
57
 
25
58
  def dump_db
@@ -88,6 +121,17 @@ class DB2S3
88
121
  file
89
122
  end
90
123
 
124
+ def list
125
+ ensure_connected
126
+ AWS::S3::Bucket.find(bucket).objects.collect {|x| x.path }
127
+ end
128
+
129
+ def delete(file_name)
130
+ if object = AWS::S3::S3Object.find(file_name, bucket)
131
+ object.delete
132
+ end
133
+ end
134
+
91
135
  private
92
136
 
93
137
  def bucket
@@ -10,26 +10,31 @@ namespace :db2s3 do
10
10
  DB2S3.new.restore
11
11
  end
12
12
 
13
- desc "Show table sizes for your database"
14
- task :statistics => :environment do
15
- # From http://mysqlpreacher.com/wordpress/tag/table-size/
16
- results = ActiveRecord::Base.connection.execute(<<-EOS)
17
- SELECT
18
- engine,
19
- ROUND(data_length/1024/1024,2) total_size_mb,
20
- ROUND(index_length/1024/1024,2) total_index_size_mb,
21
- table_rows,
22
- table_name article_attachment
23
- FROM information_schema.tables
24
- WHERE table_schema = 'rhnh_production'
25
- ORDER BY 3 desc;
26
- EOS
27
-
28
- rows = []
29
- header = [["Type", "Data MB", "Index", "Rows", "Name"], []]
30
- results.each {|x| rows << x.to_a }
31
- rows.sort_by {|x| -x[3].to_i }
32
- puts (header + rows).collect {|x| x.join("\t") }
13
+ desc "Keep all backups for the last day, one per day for the last week, and one per week before that. Delete the rest."
14
+ task :clean => :environment do
15
+ DB2S3.new.clean
33
16
  end
34
17
  end
18
+
19
+ desc "Show table sizes for your database"
20
+ task :statistics => :environment do
21
+ # From http://mysqlpreacher.com/wordpress/tag/table-size/
22
+ results = ActiveRecord::Base.connection.execute(<<-EOS)
23
+ SELECT
24
+ engine,
25
+ ROUND(data_length/1024/1024,2) total_size_mb,
26
+ ROUND(index_length/1024/1024,2) total_index_size_mb,
27
+ table_rows,
28
+ table_name article_attachment
29
+ FROM information_schema.tables
30
+ WHERE table_schema = 'rhnh_production'
31
+ ORDER BY 3 desc;
32
+ EOS
33
+
34
+ rows = []
35
+ header = [["Type", "Data MB", "Index", "Rows", "Name"], []]
36
+ results.each {|x| rows << x.to_a }
37
+ rows.sort_by {|x| -x[3].to_i }
38
+ puts (header + rows).collect {|x| x.join("\t") }
39
+ end
35
40
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db2s3
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xavier Shay
@@ -61,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  requirements: []
62
62
 
63
63
  rubyforge_project:
64
- rubygems_version: 1.3.4
64
+ rubygems_version: 1.3.5
65
65
  signing_key:
66
66
  specification_version: 3
67
67
  summary: Summarize your gem