summaryrefslogtreecommitdiff
path: root/app/models/backup_job.rb
diff options
context:
space:
mode:
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