Class: SweetyBacky::Runner

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Runner

Returns a new instance of Runner.



11
12
13
14
15
16
17
# File 'lib/sweety_backy/runner.rb', line 11

def initialize( path = nil )
  if( !path.nil? )
    config( SweetyBacky::OptsReader.read_opts( path ) )
  end
  
  @results = []
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



9
10
11
# File 'lib/sweety_backy/runner.rb', line 9

def opts
  @opts
end

#resultsObject (readonly)

Returns the value of attribute results.



9
10
11
# File 'lib/sweety_backy/runner.rb', line 9

def results
  @results
end

Instance Method Details

#cleanObject



99
100
101
# File 'lib/sweety_backy/runner.rb', line 99

def clean
  SweetyBacky::Commander.clean( @opts )
end

#config(opts) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sweety_backy/runner.rb', line 19

def config( opts )
  @opts = {
    :paths          => [],
    :databases      => [],
    :yearly         => 1,
    :monthly        => 1,
    :weekly         => 2,
    :daily          => 4,
    :storage_system => :local
  }.merge( opts )
  
  if( @opts[:storage_system].to_sym == :s3 )
    @opts[:working_path] = File.join( Dir::tmpdir, "sweety_backy_#{Time.now.to_i}" )
  else
    @opts[:working_path] = @opts[:local_opts][:path]
  end
end

#do_backupObject



37
38
39
40
# File 'lib/sweety_backy/runner.rb', line 37

def do_backup
  do_files_backup
  do_databases_backup
end

#do_databases_backupObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sweety_backy/runner.rb', line 69

def do_databases_backup
  @opts[:databases].each do |database_name|
    
    success     = nil
    backup_path = "#{@opts[:working_path]}/databases/#{database_name}.#{Date.today.strftime('%Y%m%d')}.#{SweetyBacky::Utils.period}.sql.tar.gz"
    md5_path    = "#{backup_path}.md5"
    
    begin
      SweetyBacky::Commander.do_database_backup( database_name, backup_path, @opts)
      SweetyBacky::Commander.do_md5( backup_path, md5_path )
    
      if( @opts[:storage_system].to_sym == :s3 )
        upload_databases_backup_to_s3( backup_path, md5_path )
      end
      
      success = true
      
    rescue Exception => e
      Utils.log( "ERROR: backing up database: '#{database_name}', e: #{e.message}" )
      Utils.log( e.backtrace.join("\n") )
      
      success = false
    end
    
    @results << { :name => "database: #{database_name}", :success => success }
  end
end

#do_files_backupObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sweety_backy/runner.rb', line 42

def do_files_backup
  @opts[:paths].each do |path|
    success     = nil
    backup_path = "#{@opts[:working_path]}/files/#{SweetyBacky::Utils.namerize( path )}.#{Date.today.strftime('%Y%m%d')}.#{SweetyBacky::Utils.period}.tar.gz"
    md5_path    = "#{backup_path}.md5"
    
    begin
      SweetyBacky::Commander.do_files_backup( path, backup_path )
      SweetyBacky::Commander.do_md5( backup_path, md5_path )
    
      if( @opts[:storage_system].to_sym == :s3 )
        upload_files_backup_to_s3( backup_path, md5_path )
      end
      
      success = true
      
    rescue Exception => e
      Utils.log( "ERROR: backing up file: '#{path}', e: #{e.message}" )
      Utils.log( e.backtrace.join("\n") )
      
      success = false
    end
    
    @results << { :name => "file: #{path}", :success => success }
  end
end


103
104
105
106
107
108
109
110
# File 'lib/sweety_backy/runner.rb', line 103

def print_results
  Utils.log( "RESULTS:" )
  Utils.log( "--------" )
  
  @results.each do |result|
    Utils.log( "#{result[:name]} -> #{result[:success] ? 'OK' : 'ERROR'}" )
  end
end

#runObject



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/sweety_backy/runner.rb', line 112

def run
  begin
    do_backup
    clean
    print_results
  rescue => e
    SweetyBacky::Utils.log "ERROR: #{e}"
    SweetyBacky::Utils.log "BACKTRACE: #{e.backtrace.join("\n")}"
    SweetyBacky::Utils.log "I should send and email at this moment"
  end
end