sweety_backy 0.0.5 → 0.0.7

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.
data/lib/sweety_backy.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rubygems'
2
2
  require "s3"
3
+ require 'digest/md5'
3
4
 
4
5
  require "#{File.dirname(__FILE__)}/sweety_backy/version"
5
6
  require "#{File.dirname(__FILE__)}/sweety_backy/runner"
@@ -1,6 +1,6 @@
1
1
  module SweetyBacky
2
2
  module Commander
3
- def self.do_files_backup( path, backup_path, opts )
3
+ def self.do_files_backup( path, backup_path )
4
4
  SweetyBacky::Utils.log "doing files backup of #{path} to #{backup_path}"
5
5
 
6
6
  FileUtils.mkdir_p( File.dirname( backup_path ) )
@@ -14,19 +14,19 @@ module SweetyBacky
14
14
  tmp_sql_file_path = File.join( Dir::tmpdir, "#{File.basename( backup_path, '.tar.gz' )}" )
15
15
 
16
16
  database_pass = opts[:database_pass].empty? ? '' : "-p#{opts[:database_pass]}"
17
-
17
+
18
18
  SweetyBacky::Utils::command( "mysqldump -u#{opts[:database_user]} #{database_pass} #{database_name} > #{tmp_sql_file_path}" )
19
19
  SweetyBacky::Utils::command( "tar -cz --same-permissions --file #{backup_path} --directory #{File.dirname(tmp_sql_file_path)} #{File.basename(tmp_sql_file_path)}" )
20
20
 
21
21
  File.delete( tmp_sql_file_path )
22
22
  end
23
23
 
24
- def self.clear( opts )
25
- clear_files( opts )
26
- clear_databases( opts )
24
+ def self.clean( opts )
25
+ clean_files( opts )
26
+ clean_databases( opts )
27
27
  end
28
28
 
29
- def self.clear_files( opts )
29
+ def self.clean_files( opts )
30
30
  SweetyBacky::Utils.log "cleaning files on #{opts[:working_path]}/files/"
31
31
 
32
32
  opts[:paths].each do |path|
@@ -44,7 +44,7 @@ module SweetyBacky
44
44
  end
45
45
  end
46
46
 
47
- def self.clear_databases( opts )
47
+ def self.clean_databases( opts )
48
48
  SweetyBacky::Utils.log "cleaning databases on #{opts[:working_path]}/databases/"
49
49
 
50
50
  opts[:databases].each do |database_name|
@@ -78,5 +78,11 @@ module SweetyBacky
78
78
  end
79
79
  end
80
80
 
81
+ def self.do_md5( path, md5_path )
82
+ digest = Digest::MD5.hexdigest( File.read( path ) )
83
+ File.open( md5_path, 'w' ) { |f| f.write digest }
84
+
85
+ return digest
86
+ end
81
87
  end
82
88
  end
@@ -6,12 +6,14 @@ require File.dirname(__FILE__) + "/utils.rb"
6
6
 
7
7
  module SweetyBacky
8
8
  class Runner
9
- attr_reader :opts
9
+ attr_reader :opts, :results
10
10
 
11
11
  def initialize( path = nil )
12
12
  if( !path.nil? )
13
13
  config( SweetyBacky::OptsReader.read_opts( path ) )
14
14
  end
15
+
16
+ @results = []
15
17
  end
16
18
 
17
19
  def config( opts )
@@ -39,42 +41,80 @@ module SweetyBacky
39
41
 
40
42
  def do_files_backup
41
43
  @opts[:paths].each do |path|
44
+ success = nil
42
45
  backup_path = "#{@opts[:working_path]}/files/#{SweetyBacky::Utils.namerize( path )}.#{Date.today.strftime('%Y%m%d')}.#{SweetyBacky::Utils.period}.tar.gz"
43
- SweetyBacky::Commander.do_files_backup( path, backup_path, @opts )
46
+ md5_path = "#{backup_path}.md5"
47
+
48
+ begin
49
+
50
+ SweetyBacky::Commander.do_files_backup( path, backup_path )
51
+ SweetyBacky::Commander.do_md5( backup_path, md5_path )
44
52
 
45
- if( @opts[:storage_system].to_sym == :s3 )
46
- SweetyBacky::S3.upload(
47
- backup_path,
48
- "#{@opts[:s3_opts][:path]}/files/#{File.basename( backup_path )}",
49
- @opts[:s3_opts]
50
- )
53
+ if( @opts[:storage_system].to_sym == :s3 )
54
+ upload_databases_backup_to_s3( backup_path, md5_path )
55
+ end
56
+
57
+ success = true
51
58
 
52
- FileUtils.rm_rf backup_path
59
+ rescue Exception => e
60
+ Utils.log( "ERROR: backing up file: '#{path}', e: #{e.message}" )
61
+ Utils.log( e.backtrace.join("\n") )
62
+
63
+ success = false
53
64
  end
65
+
66
+ @results << { :name => "file: #{path}", :success => success }
54
67
  end
55
68
  end
56
69
 
57
70
  def do_databases_backup
58
71
  @opts[:databases].each do |database_name|
72
+
73
+ success = nil
59
74
  backup_path = "#{@opts[:working_path]}/databases/#{database_name}.#{Date.today.strftime('%Y%m%d')}.#{SweetyBacky::Utils.period}.sql.tar.gz"
60
- SweetyBacky::Commander.do_database_backup( database_name, backup_path, @opts)
75
+ md5_path = "#{backup_path}.md5"
61
76
 
62
- if( @opts[:storage_system].to_sym == :s3 )
63
- SweetyBacky::S3.upload(
64
- backup_path,
65
- "#{@opts[:s3_opts][:path]}/databases/#{File.basename( backup_path )}",
66
- @opts[:s3_opts]
67
- )
77
+ begin
78
+ SweetyBacky::Commander.do_database_backup( database_name, backup_path, @opts)
79
+ SweetyBacky::Commander.do_md5( backup_path, md5_path )
80
+
81
+ if( @opts[:storage_system].to_sym == :s3 )
82
+ upload_databases_backup_to_s3( backup_path, md5_path )
83
+ end
84
+
85
+ success = true
68
86
 
69
- FileUtils.rm_rf backup_path
87
+ rescue Exception => e
88
+ Utils.log( "ERROR: backing up database: '#{database_name}', e: #{e.message}" )
89
+ Utils.log( e.backtrace.join("\n") )
90
+
91
+ success = false
70
92
  end
93
+
94
+ @results << { :name => "database: #{database_name}", :success => success }
95
+ end
96
+ end
97
+
98
+
99
+
100
+ def clean
101
+ SweetyBacky::Commander.clean( @opts )
102
+ end
103
+
104
+ def print_results
105
+ Utils.log( "RESULTS:" )
106
+ Utils.log( "--------" )
107
+
108
+ @results.each do |result|
109
+ Utils.log( "#{result[:name]} -> #{result[:success] ? 'OK' : 'ERROR'}" )
71
110
  end
72
111
  end
73
112
 
74
113
  def run
75
114
  begin
76
115
  do_backup
77
- SweetyBacky::Commander.clear( @opts )
116
+ clean
117
+ print_results
78
118
  rescue => e
79
119
  SweetyBacky::Utils.log "ERROR: #{e}"
80
120
  SweetyBacky::Utils.log "BACKTRACE: #{e.backtrace.join("\n")}"
@@ -82,6 +122,41 @@ module SweetyBacky
82
122
  end
83
123
  end
84
124
 
125
+ private
126
+
127
+ def upload_databases_backup_to_s3( backup_path, md5_path )
128
+ SweetyBacky::S3.upload(
129
+ backup_path,
130
+ "#{@opts[:s3_opts][:path]}/databases/#{File.basename( backup_path )}",
131
+ @opts[:s3_opts]
132
+ )
133
+
134
+ SweetyBacky::S3.upload(
135
+ backup_path,
136
+ "#{@opts[:s3_opts][:path]}/databases/#{File.basename( md5_path )}",
137
+ @opts[:s3_opts]
138
+ )
139
+
140
+ FileUtils.rm backup_path
141
+ FileUtils.rm md5_path
142
+ end
143
+
144
+ def upload_files_backup_to_s3( backup_path, md5_path )
145
+ SweetyBacky::S3.upload(
146
+ backup_path,
147
+ "#{@opts[:s3_opts][:path]}/files/#{File.basename( backup_path )}",
148
+ @opts[:s3_opts]
149
+ )
150
+
151
+ SweetyBacky::S3.upload(
152
+ md5_path,
153
+ "#{@opts[:s3_opts][:path]}/files/#{File.basename( md5_path )}",
154
+ @opts[:s3_opts]
155
+ )
156
+
157
+ FileUtils.rm backup_path
158
+ FileUtils.rm md5_path
159
+ end
85
160
 
86
161
  end
87
162
  end
@@ -23,7 +23,7 @@ module SweetyBacky
23
23
  def self.command( _command )
24
24
  Utils.log "command: #{_command}"
25
25
 
26
- result = %x( #{_command} 2>&1 )
26
+ result = %x( #{_command} )
27
27
 
28
28
  raise "ERROR: on command: '#{_command}', result: '#{result}'" if $?.exitstatus != 0
29
29
 
@@ -1,3 +1,3 @@
1
1
  module SweetyBacky
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -17,8 +17,7 @@ class CommanderTest < Test::Unit::TestCase
17
17
  def test_do_files_backup
18
18
  SweetyBacky::Commander.do_files_backup(
19
19
  "#{FIXTURES_PATH}/path",
20
- "#{@tmp_dir}/back.tar.gz",
21
- {}
20
+ "#{@tmp_dir}/back.tar.gz"
22
21
  )
23
22
 
24
23
  result = %x(tar -tzvf #{@tmp_dir}/back.tar.gz)
@@ -45,7 +44,7 @@ class CommanderTest < Test::Unit::TestCase
45
44
  end
46
45
 
47
46
 
48
- def test_clear
47
+ def test_clean
49
48
  opts = {
50
49
  :paths => [ 'name1', 'name2' ],
51
50
  :databases => [ 'name1', 'name2' ],
@@ -91,7 +90,7 @@ class CommanderTest < Test::Unit::TestCase
91
90
  # puts @tmp_dir
92
91
  # exit 1
93
92
 
94
- SweetyBacky::Commander.clear( opts )
93
+ SweetyBacky::Commander.clean( opts )
95
94
 
96
95
  files_keeped = Dir.glob( "#{@tmp_dir}/files/*" ).join( "\n" )
97
96
  databases_keeped = Dir.glob( "#{@tmp_dir}/databases/*" ).join( "\n" )
@@ -128,7 +127,15 @@ class CommanderTest < Test::Unit::TestCase
128
127
  assert_no_match( /#{file_part}.sql.tar.gz/, databases_keeped )
129
128
  end
130
129
  end
131
-
130
+
131
+ def test_do_md5
132
+ md5_path = "#{@tmp_dir}/#{Time.now.to_i}.md5"
133
+ fixture_file_path = "#{FIXTURES_PATH}/file_for_md5.txt"
132
134
 
135
+ md5 = SweetyBacky::Commander.do_md5( fixture_file_path, md5_path )
136
+
137
+ assert_equal( '75fca8d37b0f7d75d11ccc8d255debe5', md5 )
138
+ assert_equal( '75fca8d37b0f7d75d11ccc8d255debe5', File.read( md5_path ) )
139
+ end
133
140
  end
134
141
 
@@ -0,0 +1 @@
1
+ This is the content
data/test/runner_test.rb CHANGED
@@ -38,8 +38,11 @@ class RunnerTest < Test::Unit::TestCase
38
38
  @runner.do_backup
39
39
  end
40
40
 
41
- assert( File.exists?( "#{@tmp_dir}/files/#{SweetyBacky::Utils.namerize( @opts[:paths][0] )}.20091231.yearly.tar.gz") )
42
- assert( File.exists?( "#{@tmp_dir}/databases/test.20091231.yearly.sql.tar.gz") )
41
+ assert( File.exists?( "#{@tmp_dir}/files/#{SweetyBacky::Utils.namerize( @opts[:paths][0] )}.20091231.yearly.tar.gz" ) )
42
+ assert( File.exists?( "#{@tmp_dir}/databases/test.20091231.yearly.sql.tar.gz" ) )
43
+
44
+ assert( File.exists?( "#{@tmp_dir}/files/#{SweetyBacky::Utils.namerize( @opts[:paths][0] )}.20091231.yearly.tar.gz.md5" ) )
45
+ assert( File.exists?( "#{@tmp_dir}/databases/test.20091231.yearly.sql.tar.gz.md5" ) )
43
46
  end
44
47
 
45
48
  def test_do_backup_monthly
@@ -71,7 +74,8 @@ class RunnerTest < Test::Unit::TestCase
71
74
 
72
75
  def test_run
73
76
  @runner.expects(:do_backup)
74
- SweetyBacky::Commander.expects(:clear)
77
+ @runner.expects(:print_results)
78
+ SweetyBacky::Commander.expects(:clean)
75
79
 
76
80
  @runner.run
77
81
  end
@@ -31,7 +31,7 @@ class CommanderS3Test < Test::Unit::TestCase
31
31
  @bucket.destroy( true )
32
32
  end
33
33
 
34
- def test_clear
34
+ def test_clean
35
35
  [
36
36
  'name1.20081231.yearly',
37
37
  'name1.20081232.yearly',
@@ -57,7 +57,7 @@ class CommanderS3Test < Test::Unit::TestCase
57
57
  SweetyBacky::S3.upload( "#{FIXTURES_PATH}/file.txt", "#{@opts[:s3_opts][:path]}/databases/#{file_part}.sql.tar.gz", @opts[:s3_opts] )
58
58
  end
59
59
 
60
- SweetyBacky::Commander.clear( @opts )
60
+ SweetyBacky::Commander.clean( @opts )
61
61
 
62
62
  files_keeped = SweetyBacky::S3.paths_in( "#{@opts[:s3_opts][:path]}/files/*", @opts[:s3_opts] ).join( "\n" )
63
63
  databases_keeped = SweetyBacky::S3.paths_in( "#{@opts[:s3_opts][:path]}/databases/*", @opts[:s3_opts] ).join( "\n" )
@@ -53,6 +53,20 @@ class RunnerS3Test < Test::Unit::TestCase
53
53
  "test/path/databases/test.#{Date.today.strftime('%Y%m%d')}.daily.sql.tar.gz"
54
54
  ).exists?
55
55
  )
56
+
57
+ assert(
58
+ @bucket.
59
+ object(
60
+ "test/path/files/#{SweetyBacky::Utils.namerize( @opts[:paths][0] )}.#{Date.today.strftime('%Y%m%d')}.daily.tar.gz.md5"
61
+ ).exists?
62
+ )
63
+
64
+ assert(
65
+ @bucket.
66
+ object(
67
+ "test/path/databases/test.#{Date.today.strftime('%Y%m%d')}.daily.sql.tar.gz.md5"
68
+ ).exists?
69
+ )
56
70
  end
57
71
 
58
72
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sweety_backy
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 7
10
+ version: 0.0.7
11
11
  platform: ruby
12
12
  authors:
13
13
  - Fernando Guillen
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-05 00:00:00 +02:00
18
+ date: 2011-07-18 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -105,6 +105,7 @@ files:
105
105
  - test/commander_test.rb
106
106
  - test/fixtures/config_s3.yml
107
107
  - test/fixtures/file.txt
108
+ - test/fixtures/file_for_md5.txt
108
109
  - test/fixtures/path/a/file2.txt
109
110
  - test/fixtures/path/b/file3.txt
110
111
  - test/fixtures/path/file1.txt
@@ -154,6 +155,7 @@ test_files:
154
155
  - test/commander_test.rb
155
156
  - test/fixtures/config_s3.yml
156
157
  - test/fixtures/file.txt
158
+ - test/fixtures/file_for_md5.txt
157
159
  - test/fixtures/path/a/file2.txt
158
160
  - test/fixtures/path/b/file3.txt
159
161
  - test/fixtures/path/file1.txt