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.



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

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.



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

def opts
  @opts
end

#resultsObject (readonly)

Returns the value of attribute results.



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

def results
  @results
end

Instance Method Details

#cleanObject



103
104
105
# File 'lib/sweety_backy/runner.rb', line 103

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

#config(opts) ⇒ Object



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

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}" )
    @opts[:target_path]  = @opts[:s3_opts][:path]
  else
    @opts[:working_path] = @opts[:local_opts][:path]
    @opts[:target_path]  = @opts[:local_opts][:path]
  end
end

#do_backupObject



40
41
42
43
# File 'lib/sweety_backy/runner.rb', line 40

def do_backup
  do_files_backup
  do_databases_backup
end

#do_databases_backupObject



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

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



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

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 )
      SweetyBacky::Commander.do_slices( backup_path, opts[:slices_size] ) if opts[:slices_size]

      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


107
108
109
110
111
112
113
114
# File 'lib/sweety_backy/runner.rb', line 107

def print_results
  Utils.log( "RESULTS:" )
  Utils.log( "--------" )

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

#runObject



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/sweety_backy/runner.rb', line 116

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

#upload_databases_backup_to_s3(backup_path, md5_path) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/sweety_backy/runner.rb', line 128

def upload_databases_backup_to_s3( backup_path, md5_path )
  SweetyBacky::S3.upload(
    backup_path,
    "#{@opts[:target_path]}/databases/#{File.basename( backup_path )}",
    @opts[:s3_opts]
  )

  SweetyBacky::S3.upload(
    md5_path,
    "#{@opts[:target_path]}/databases/#{File.basename( md5_path )}",
    @opts[:s3_opts]
  )

  FileUtils.rm backup_path
  FileUtils.rm md5_path
end

#upload_files_backup_to_s3(backup_path, md5_path) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/sweety_backy/runner.rb', line 145

def upload_files_backup_to_s3( backup_path, md5_path )
  backup_paths = opts[:slices_size] ? Dir.glob( "#{backup_path}.part_*" ) : [backup_path]

  backup_paths.sort.each do |backup_path|
    SweetyBacky::S3.upload(
      backup_path,
      "#{@opts[:target_path]}/files/#{File.basename( backup_path )}",
      @opts[:s3_opts]
    )

    FileUtils.rm backup_path
  end

  SweetyBacky::S3.upload(
    md5_path,
    "#{@opts[:target_path]}/files/#{File.basename( md5_path )}",
    @opts[:s3_opts]
  )

  FileUtils.rm md5_path
end