summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorStefan Wintermeyer <stefan.wintermeyer@amooma.de>2013-02-06 11:07:04 +0100
committerStefan Wintermeyer <stefan.wintermeyer@amooma.de>2013-02-06 11:07:04 +0100
commit3edd6e58ea3f5d25068ea9d47f639ae89e5f2c0c (patch)
tree225aee0cc7a8d746dfa1e52ccb2fc8d633746197 /app/models
parentb1f9f3b2c39d31d0621da36420318406f33a1f92 (diff)
Improved the backup.
Diffstat (limited to 'app/models')
-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