diff --git a/app/controllers/jobs_controller.rb b/app/controllers/jobs_controller.rb index b524827..c6af6b8 100644 --- a/app/controllers/jobs_controller.rb +++ b/app/controllers/jobs_controller.rb @@ -1,60 +1,67 @@ class JobsController < ApplicationController - before_action :fetch_host, only: [:new, :edit, :show, :create, :update, :toggle_enable] - before_action :fetch_job, only: [:show, :edit, :update, :destroy, :toggle_enable] + before_action :fetch_host, only: [:new, :edit, :show, :create, :update, + :toggle_enable, :backup_now] + before_action :fetch_job, only: [:show, :edit, :update, :destroy, :toggle_enable, :backup_now] # GET /jobs def new @job = @host.job_templates.new end # POST /jobs def create @job = @host.job_templates.new(fetch_params) @restore_location = params[:job_template][:restore_location] if @restore_location && @job.save_and_create_restore_job(@restore_location) redirect_to host_path(@host) else render :new end end # GET /jobs/1 def show; end # GET /jobs/1/edit def edit;end # PUT /jobs/1 def update if @job.update_attributes(fetch_params) redirect_to host_job_path(@host, @job) else render :edit end end # DELETE /jobs/1 def destroy end # PATCH /hosts/1/jobs/1/enable def toggle_enable @job.enabled = !@job.enabled @job.save redirect_to host_path(@host) end + # POST /hosts/1/jobs/1/backup_now + def backup_now + @job.backup_now + redirect_to client_path(@host.client) + end + private def fetch_job @job = @host.job_templates.find(params[:id]) end def fetch_host @host = current_user.hosts.find(params[:host_id]) end def fetch_params params.require(:job_template).permit(:name, :fileset_id, :schedule_id) end end diff --git a/app/models/job_template.rb b/app/models/job_template.rb index 60df951..b3460c4 100644 --- a/app/models/job_template.rb +++ b/app/models/job_template.rb @@ -1,88 +1,94 @@ class JobTemplate < ActiveRecord::Base establish_connection Baas::settings[:local_db] enum job_type: { backup: 0, restore: 1, verify: 2, admin: 3 } belongs_to :host belongs_to :fileset belongs_to :schedule validates :name, :fileset_id, presence: true validates :schedule_id, presence: true, unless: :restore? validates :name, uniqueness: { scope: :host } before_save :set_job_type after_save :notify_host scope :enabled, -> { where(enabled: true) } # configurable DEFAULT_OPTIONS = { storage: :File, pool: :Default, messages: :Standard, priority: 10, :'Write Bootstrap' => '"/var/lib/bacula/%c.bsr"' } def to_bacula_config_array ['Job {'] + options_array.map { |x| " #{x}" } + DEFAULT_OPTIONS.map { |k,v| " #{k.capitalize} = #{v}" } + ['}'] end def priority DEFAULT_OPTIONS[:priority] end def enabled_human enabled? ? 'yes' : 'no' end def schedule_human schedule.present? ? schedule.name : '-' end def name_for_config "#{host.name} #{name}" end + # Sends a hot backup request to Bacula via BaculaHandler + def backup_now + return false if not (enabled? && baculized? && backup?) + BaculaHandler.new(host).backup_now(name) + end + def save_and_create_restore_job(location) if save_status = save restore_job = JobTemplate.new( host: host, job_type: :restore, fileset: fileset, name: 'Restore ' + name, restore_location: location ) restore_job.save end save_status end private def notify_host host.recalculate end # Sets the default job_type as backup def set_job_type self.job_type = :backup if job_type.nil? end def options_array result = [ "Name = \"#{name_for_config}\"", "FileSet = \"#{fileset.name_for_config}\"", "Client = \"#{host.name}\"", "Type = \"#{job_type.capitalize}\"" ] if restore? result += ["Where = \"#{restore_location}\""] else result += ["Schedule = \"#{schedule.name_for_config}\""] end result end end diff --git a/app/views/clients/_client_details.html.erb b/app/views/clients/_client_details.html.erb index 7da5967..fe365ec 100644 --- a/app/views/clients/_client_details.html.erb +++ b/app/views/clients/_client_details.html.erb @@ -1,48 +1,47 @@
Name | <%= @client.name %> |
Uname | <%= @client.uname %> |
Active Jobs | <%= @client.running_jobs %> |
Last Backup | <%= @client.last_job_date_formatted %> |
File Retention | <%= @client.file_retention_days %> days |
Job Retention | <%= @client.job_retention_days %> days |
Total Space Used | <%= number_to_human_size @client.backup_jobs_size %> |
Files count | <%= number_by_magnitude(@client.files_count) %> |
Auto Prune | <%= @client.auto_prune_human %> |