summaryrefslogtreecommitdiff
path: root/app/models/backup_job.rb
diff options
context:
space:
mode:
authorspag <spag@golwen.net>2013-02-06 15:12:26 +0100
committerspag <spag@golwen.net>2013-02-06 15:12:26 +0100
commite27343e207be877c0977cc1794b754a3bdb9f258 (patch)
treec5fee76c060f9b7e8a1a82e1b3893a2902f538ef /app/models/backup_job.rb
parent98b822e2d139ad2c2779ce292f597d25ff6f936c (diff)
parentc26e4b9d27218378f34175ac3410eab5b12a5713 (diff)
Merge branch 'develop' of github.com:amooma/GS5 into develop
Diffstat (limited to 'app/models/backup_job.rb')
-rw-r--r--app/models/backup_job.rb23
1 files changed, 20 insertions, 3 deletions
diff --git a/app/models/backup_job.rb b/app/models/backup_job.rb
index a90726c..c9bde2b 100644
--- a/app/models/backup_job.rb
+++ b/app/models/backup_job.rb
@@ -1,26 +1,43 @@
class BackupJob < ActiveRecord::Base
attr_accessible :started_at, :finished_at, :state, :directory, :size_of_the_backup
+ before_create :set_state_to_queued
after_create :initiate_backup
+ after_destroy :delete_the_backup_directory
private
+ def set_state_to_queued
+ self.state = 'queued'
+ self.started_at = Time.now
+ end
+
def initiate_backup
self.delay.make_a_backup
end
def make_a_backup
- if self.finished_at.nil?
+ if self.finished_at.nil? && self.state == 'queued'
+ self.state = 'running'
+ self.save
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'
+ self.state = 'failed'
else
- self.size_of_the_backup = (`du -hs #{new_directory}`).split(/\t/).first
+ file = File::Stat.new(self.directory)
+ self.size_of_the_backup = file.size
self.finished_at = Time.now
self.state = 'successful'
end
self.save
end
end
+
+ def delete_the_backup_directory
+ if !self.directory.blank?
+ require 'fileutils'
+ FileUtils.rm_rf self.directory
+ end
+ end
end