Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix bug in cleaning old files and databases backups

  • Loading branch information...
commit 858b4029cc66bbf9c20a53071eb515dd9f6688f5 1 parent d777f1b
@fguillen authored
View
1  .gitignore
@@ -2,3 +2,4 @@
*.gem
.bundle
pkg/*
+tmp
View
18 lib/sweety_backy/commander.rb
@@ -27,39 +27,49 @@ def self.clean( opts )
end
def self.clean_files( opts )
- SweetyBacky::Utils.log "cleaning files on #{opts[:working_path]}/files/"
+ SweetyBacky::Utils.log "cleaning files on #{opts[:target_path]}/files/"
opts[:paths].each do |path|
SweetyBacky::Utils.log "cleaning file #{path}"
[:yearly, :monthly, :weekly, :daily].each do |period|
paths_in(
- "#{opts[:working_path]}/files/#{SweetyBacky::Utils.namerize( path )}.*.#{period.to_s}.tar.gz",
+ "#{opts[:target_path]}/files/#{SweetyBacky::Utils.namerize( path )}.*.#{period.to_s}.tar.gz",
opts
).sort[0..(-1*(opts[period]+1))].each do |file_path|
remove_path( file_path, opts )
+ remove_path( "#{file_path}.md5", opts ) if exists?( "#{file_path}.md5", opts )
end
end
end
end
def self.clean_databases( opts )
- SweetyBacky::Utils.log "cleaning databases on #{opts[:working_path]}/databases/"
+ SweetyBacky::Utils.log "cleaning databases on #{opts[:target_path]}/databases/"
opts[:databases].each do |database_name|
SweetyBacky::Utils.log "cleaning database #{database_name}"
[:yearly, :monthly, :weekly, :daily].each do |period|
paths_in(
- "#{opts[:working_path]}/databases/#{database_name}.*.#{period.to_s}.sql.tar.gz",
+ "#{opts[:target_path]}/databases/#{database_name}.*.#{period.to_s}.sql.tar.gz",
opts
).sort[0..(-1*(opts[period]+1))].each do |file_path|
remove_path( file_path, opts )
+ remove_path( "#{file_path}.md5", opts ) if exists?( "#{file_path}.md5", opts )
end
end
end
end
+ def self.exists?( path, opts )
+ if( opts[:storage_system].to_sym == :s3 )
+ return SweetyBacky::S3.exists?( path, opts[:s3_opts] )
+ else
+ return File.exists?( path )
+ end
+ end
+
def self.paths_in( path, opts )
if( opts[:storage_system].to_sym == :s3 )
return SweetyBacky::S3.paths_in( path, opts[:s3_opts] )
View
10 lib/sweety_backy/runner.rb
@@ -29,8 +29,10 @@ def config( opts )
if( @opts[:storage_system].to_sym == :s3 )
@opts[:working_path] = File.join( Dir::tmpdir, "sweety_backy_#{Time.now.to_i}" )
+ @opts[:target_path] = @opts[:s3_opts][:path]
else
@opts[:working_path] = @opts[:local_opts][:path]
+ @opts[:target_path] = @opts[:local_opts][:path]
end
end
@@ -126,13 +128,13 @@ def run
def upload_databases_backup_to_s3( backup_path, md5_path )
SweetyBacky::S3.upload(
backup_path,
- "#{@opts[:s3_opts][:path]}/databases/#{File.basename( backup_path )}",
+ "#{@opts[:target_path]}/databases/#{File.basename( backup_path )}",
@opts[:s3_opts]
)
SweetyBacky::S3.upload(
md5_path,
- "#{@opts[:s3_opts][:path]}/databases/#{File.basename( md5_path )}",
+ "#{@opts[:target_path]}/databases/#{File.basename( md5_path )}",
@opts[:s3_opts]
)
@@ -143,13 +145,13 @@ def upload_databases_backup_to_s3( backup_path, md5_path )
def upload_files_backup_to_s3( backup_path, md5_path )
SweetyBacky::S3.upload(
backup_path,
- "#{@opts[:s3_opts][:path]}/files/#{File.basename( backup_path )}",
+ "#{@opts[:target_path]}/files/#{File.basename( backup_path )}",
@opts[:s3_opts]
)
SweetyBacky::S3.upload(
md5_path,
- "#{@opts[:s3_opts][:path]}/files/#{File.basename( md5_path )}",
+ "#{@opts[:target_path]}/files/#{File.basename( md5_path )}",
@opts[:s3_opts]
)
View
12 lib/sweety_backy/s3.rb
@@ -22,15 +22,19 @@ def self.object( path, opts )
object
end
+ def self.exists?( path, opts )
+ return object( path, opts ).exists?
+ end
+
def self.paths_in( path, opts )
s3 = AWS::S3.new( read_s3_password( opts[:passwd_file] ) )
bucket = s3.buckets[ opts[:bucket] ]
regex = Regexp.escape( path ).gsub('\*', '.*').gsub('\?', '.')
-
- objects = bucket.objects.select { |e| e.key =~ /#{regex}/ }
- paths = objects.map(&:key)
-
+
+ objects = bucket.objects.select { |e| e.key =~ /^#{regex}$/ }
+ paths = objects.map(&:key)
+
return paths
end
View
2  lib/sweety_backy/version.rb
@@ -1,3 +1,3 @@
module SweetyBacky
- VERSION = "0.0.17"
+ VERSION = "0.0.19"
end
View
5 test/commander_test.rb
@@ -56,7 +56,8 @@ def test_clean
:local_opts => {
:path => @tmp_dir
},
- :working_path => @tmp_dir
+ :working_path => @tmp_dir,
+ :target_path => @tmp_dir
}
Dir.mkdir( "#{@tmp_dir}/files" ) unless File.exists?( "#{@tmp_dir}/files" )
@@ -92,7 +93,7 @@ def test_clean
SweetyBacky::Commander.clean( opts )
- files_keeped = Dir.glob( "#{@tmp_dir}/files/*" ).join( "\n" )
+ files_keeped = Dir.glob( "#{@tmp_dir}/files/*" ).join( "\n" )
databases_keeped = Dir.glob( "#{@tmp_dir}/databases/*" ).join( "\n" )
# files to keep
View
2  test/runner_test.rb
@@ -100,6 +100,8 @@ def test_initialize_with_config_file
assert_equal( 4, runner.opts[:daily] )
assert_equal( :local, runner.opts[:storage_system] )
assert_equal( '/local/path', runner.opts[:local_opts][:path] )
+ assert_equal( '/local/path', runner.opts[:working_path] )
+ assert_equal( '/local/path', runner.opts[:target_path] )
end
end
View
3  test/s3/commander_s3_test.rb
@@ -18,7 +18,8 @@ def setup
:path => 'test/path',
:passwd_file => '~/.s3.passwd'
},
- :working_path => @tmp_dir
+ :working_path => @tmp_dir,
+ :target_path => 'test/path'
}
s3 = AWS::S3.new( SweetyBacky::S3.read_s3_password( @opts[:s3_opts][:passwd_file] ) )
View
24 test/s3/runner_s3_test.rb
@@ -67,6 +67,30 @@ def test_do_backup_daily
].exists?
)
end
+
+ def test_initialize_with_config_file
+ SweetyBacky::OptsReader.expects( :read_opts ).with( '/path/config.yml' ).returns(
+ {
+ :paths => [ 'pepe', 'juan' ],
+ :storage_system => :s3,
+ :s3_opts => {
+ :path => '/s3/path'
+ }
+ }
+ )
+
+ runner = SweetyBacky::Runner.new( "/path/config.yml" )
+
+ assert_equal( [ "pepe", "juan" ], runner.opts[:paths] )
+ assert_equal( [], runner.opts[:databases] )
+ assert_equal( 1, runner.opts[:yearly] )
+ assert_equal( 1, runner.opts[:monthly] )
+ assert_equal( 2, runner.opts[:weekly] )
+ assert_equal( 4, runner.opts[:daily] )
+ assert_equal( :s3, runner.opts[:storage_system] )
+ assert_equal( '/s3/path', runner.opts[:s3_opts][:path] )
+ assert_equal( '/s3/path', runner.opts[:target_path] )
+ end
end
View
6 test/s3/s3_test.rb
@@ -57,4 +57,10 @@ def test_delete
assert( !@bucket.objects[ "test/path/file2.txt" ].exists? )
end
+ def test_exists
+ SweetyBacky::S3.upload( "#{FIXTURES_PATH}/file.txt", "test/path/file1.txt", @opts )
+ assert_equal( true, SweetyBacky::S3.exists?( "test/path/file1.txt", @opts ) )
+ assert_equal( false, SweetyBacky::S3.exists?( "test/path/file2.txt", @opts ) )
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.