summaryrefslogtreecommitdiff
path: root/app/models/backup_job.rb
blob: a90726c7d9542965ad7dc636a04d93218a875924 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class BackupJob < ActiveRecord::Base
  attr_accessible :started_at, :finished_at, :state, :directory, :size_of_the_backup

  after_create :initiate_backup

  private
  def initiate_backup
    self.delay.make_a_backup
  end

  def make_a_backup
    if self.finished_at.nil?
      original_directories = Dir.glob('/var/backups/GS5/*')
      system "backup perform --trigger GS5 --config_file #{Rails.root.join('config','backup.rb')}"
      self.directory = (Dir.glob('/var/backups/GS5/*') - original_directories).first
      if self.directory.blank?
        self.state = 'unsuccessful'
      else
        self.size_of_the_backup = (`du -hs #{new_directory}`).split(/\t/).first
        self.finished_at = Time.now
        self.state = 'successful'
      end
      self.save
    end
  end
end