Skip to content

Commit

Permalink
putting things better
Browse files Browse the repository at this point in the history
  • Loading branch information
fguillen committed Jul 19, 2010
1 parent 54659b3 commit 3d89b9a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
18 changes: 10 additions & 8 deletions sweety_backy.rb
Expand Up @@ -7,7 +7,6 @@ class SweetyBacky

def initialize( conf_path )
@opts = Utils::read_opts( conf_path )
puts "configuration: #{@opts.inspect}"
end

def do_backup
Expand All @@ -32,7 +31,7 @@ def do_backup
end

def do_files_backup( path )
puts "doing files backup"
Utils::log "doing files backup"

FileUtils.mkdir_p( File.dirname( path ) )
exclude_file_path = File.join( Dir::tmpdir, "#{Time.now.to_i}_exclude.txt" )
Expand All @@ -41,7 +40,7 @@ def do_files_backup( path )
end

def do_databases_backup( path )
puts "doing databases backup"
Utils::log "doing databases backup"

FileUtils.mkdir_p( File.dirname( path ) )
tmp_sql_file_path = File.join( Dir::tmpdir, "#{File.basename( path, '.tar.gz' )}" )
Expand All @@ -51,7 +50,7 @@ def do_databases_backup( path )
end

def clear
puts "cleaning"
Utils::log "cleaning"

[:yearly, :monthly, :weekly, :daily].each do |block|
Dir.glob( "#{@opts[:path]}/files/*.#{block.to_s}.*" ).sort[0..(-1*(@opts[block]+1))].each do |file_path|
Expand All @@ -64,13 +63,16 @@ def clear
end
end


def run
do_backup
clear
begin
do_backup
clear
rescue => e
Utils::log "ERROR: #{e}"
Utils::log "I should send and email at this moment"
end
end



end

Expand Down
6 changes: 4 additions & 2 deletions sweety_backy_execute.rb
Expand Up @@ -2,14 +2,16 @@
require File.dirname(__FILE__) + "/sweety_backy.rb"

if( ARGV[0].nil? )
puts "use: $ ruby sweety_backy_execute.rb <config_file_path>"
Utils::log "use: $ ruby sweety_backy_execute.rb <config_file_path>"
exit 1
end

lapsus_time =
Benchmark.realtime do
Utils::log "--------------------"
Utils::log "Starting SweetyBacky"
sb = SweetyBacky.new( ARGV[0] )
sb.run
end

puts "SweetyBacky on #{lapsus_time}"
Utils::log "SweetyBacky on #{lapsus_time} seconds"
10 changes: 10 additions & 0 deletions sweety_backy_test.rb
Expand Up @@ -7,6 +7,10 @@
class SweetyBackyTest < Test::Unit::TestCase

def setup
# Moching log
Utils.stubs(:log)

# tmp dir
@tmp_dir = File.join( Dir::tmpdir, "sweety_backy_#{Time.now.to_i}" )
Dir.mkdir( @tmp_dir )

Expand Down Expand Up @@ -161,6 +165,12 @@ def test_run
@sb.expects(:clear)
@sb.run
end

def test_error_on_command
assert_raise RuntimeError do
Utils::command( 'command_not_exists' )
end
end


end
Expand Down
19 changes: 17 additions & 2 deletions utils.rb
Expand Up @@ -13,11 +13,15 @@ def self.is_last_day_of_week?

def self.command( _command )
# puts "command: #{_command}"
%x( #{_command} )
result = %x( #{_command} 2>&1 )

raise "ERROR: on command: '#{_command}', result: '#{result}'" if $?.exitstatus != 0

return result
end

def self.read_opts( conf_path )
puts "conf_path: #{conf_path}"
Utils::log "conf_path: #{conf_path}"
raw_config = File.read( conf_path )
opts = YAML.load(raw_config)

Expand All @@ -27,8 +31,19 @@ def self.read_opts( conf_path )
end


Utils::log "configuration:"
Utils::log "------------"
opts.each_pair do |key, value|
Utils::log "#{key}: #{(value.instance_of? Array) ? value.join(' | ') : value}"
end


# TODO: test all options are ok

return opts
end

def self.log( msg )
puts "#{Time.now.strftime("%Y-%m-%d %H:%M")}: #{msg}"
end
end

0 comments on commit 3d89b9a

Please sign in to comment.