From 1ff20b9e46fc7b312cb93547b4f1cc80c98d75dc Mon Sep 17 00:00:00 2001 From: sukhbir-singh Date: Mon, 30 Jul 2018 00:09:11 +0530 Subject: [PATCH] update crontab_helper file, start/stop of backups done --- app/assets/javascripts/rsnapshot_backups.js | 29 ++++++++++ .../rsnapshot_backups_controller.rb | 17 +++--- .../rsnapshot_backups/settings.html.slim | 8 ++- lib/rsnapshot_backups/crontab_helper.rb | 54 ++++++++----------- 4 files changed, 62 insertions(+), 46 deletions(-) diff --git a/app/assets/javascripts/rsnapshot_backups.js b/app/assets/javascripts/rsnapshot_backups.js index b57b976..f9f9fec 100755 --- a/app/assets/javascripts/rsnapshot_backups.js +++ b/app/assets/javascripts/rsnapshot_backups.js @@ -209,6 +209,35 @@ $(document).ready(function() { }); $(document).on('click', '#start_backups_button', function(event) { + var current = event.target; + current.previousSibling.style.display = ""; var submit_btn=document.getElementById("start_backups"); submit_btn.click(); }); + +$(document).on('click', '#stop_backups_button', function(event) { + var current=event.target; + current.previousSibling.style.display = ""; + var form=document.getElementById("start_backups_form_id"); + var action=form.action; + action=action.replace("start_backups","stop_backups"); + form.action=action; + var submit_btn=document.getElementById("start_backups"); + submit_btn.click(); +}); + +$(document).on('ajax:success', '#start_backups_form_id', function(event, results) { + var messages_span = document.getElementById("start_backups_messages"); + messages_span.innerHTML = results["message"] + messages_span.style.display="inline-block"; + + setTimeout(function(){ + if(results["success"]){ + window.location.reload(); + } + messages_span.style.display="none"; + }, 4000); + + var spinner = document.getElementsByClassName("start_backups_spinner")[0]; + spinner.style.display="none"; +}); diff --git a/app/controllers/rsnapshot_backups_controller.rb b/app/controllers/rsnapshot_backups_controller.rb index bdf4286..2e37013 100755 --- a/app/controllers/rsnapshot_backups_controller.rb +++ b/app/controllers/rsnapshot_backups_controller.rb @@ -18,7 +18,7 @@ def settings @dest_path = RsnapshotHelper.get_fields("snapshot_root") @backup_paths = RsnapshotHelper.get_fields("backup") @cron_job_status = CronTabHelper.check_status - @current_intervals = @cron_job_status? [CronTabHelper.get_cron_interval] : nil + @current_intervals = CronTabHelper.get_cron_intervals end def update_backup_directory @@ -51,24 +51,19 @@ def update_backup_sources end end - def update_interval - CronTabHelper.add_cron(params[:interval]) - end - - def stop_automatic_backup - CronTabHelper.remove_cron - end - def start_backups intervals = params[:interval] - if intervals.size == 0 + if intervals.blank? or intervals.size == 0 render :json => {success: false, message: "Error: Select atleast one 'Repeat Duration' to start backups"} else - render :json => {success: success, message: "Backups Started Successfully !!"} + CronTabHelper.add_crons(intervals) + render :json => {success: true, message: "Backups Started Successfully !!"} end end def stop_backups + CronTabHelper.remove_all_crons + render :json => {success: true, message: "Backups Stopped Successfully !!"} end end diff --git a/app/views/rsnapshot_backups/settings.html.slim b/app/views/rsnapshot_backups/settings.html.slim index 7017af8..ca63c54 100755 --- a/app/views/rsnapshot_backups/settings.html.slim +++ b/app/views/rsnapshot_backups/settings.html.slim @@ -93,9 +93,13 @@ css: div style="margin-left:120px;" = spinner "start_backups_spinner" - input.p-2.pl-3.pr-3.mt-2#start_backups_button type="button" style="cursor:pointer;" value="Start Backups" + - if @cron_job_status + input.p-2.pl-3.pr-3.mt-2#stop_backups_button type="button" style="cursor:pointer;" value="Stop Backups" + - else + input.p-2.pl-3.pr-3.mt-2#start_backups_button type="button" style="cursor:pointer;" value="Start Backups" + br - span#start_backups_messages.messages style="display: none" + span#start_backups_messages.messages.mt-2.mb-2 style="display: none; width: auto;" br = javascript_include_tag 'rsnapshot_backups' diff --git a/lib/rsnapshot_backups/crontab_helper.rb b/lib/rsnapshot_backups/crontab_helper.rb index 98ae7ae..f54dd34 100644 --- a/lib/rsnapshot_backups/crontab_helper.rb +++ b/lib/rsnapshot_backups/crontab_helper.rb @@ -1,57 +1,45 @@ class CronTabHelper class << self def check_status - list = get_cron_list - list.include? "rsnapshot" + intervals = get_cron_intervals + intervals.size != 0 + end + + def add_crons(intervals) + intervals.each do |interval| + add_cron(interval) + end end def add_cron(interval=nil) - remove_cron if check_status - if interval == "hourly" - add_to_crontab("0 * * * * rsnapshot alpha") - elsif interval == "weekly" # 6 - saturday - add_to_crontab("0 0 * * 6 rsnapshot gamma") + if interval == "weekly" + add_to_crontab('weekly') + elsif interval == "monthly" + add_to_crontab('monthly') else # daily (as default) - add_to_crontab("0 0 * * * rsnapshot beta") + add_to_crontab('daily') end end - def remove_cron - list = get_cron_list - enteries = list.split("\n") - enteries.delete_if {|entry| entry.include? "rsnapshot"} - `sudo /var/hda/apps/03qjfjl1sh/elevated/crontab-util 'reset'` - enteries.each do |entry| - add_to_crontab(entry) - end + def remove_cron(interval) + `sudo /var/hda/apps/03qjfjl1sh/elevated/crontab-util 'remove' '#{interval}'` end def get_cron_list `sudo /var/hda/apps/03qjfjl1sh/elevated/crontab-util 'list'` end - def add_to_crontab(cron_string) - `sudo /var/hda/apps/03qjfjl1sh/elevated/crontab-util 'add' '#{cron_string}'` + def add_to_crontab(interval) + `sudo /var/hda/apps/03qjfjl1sh/elevated/crontab-util 'add' '#{interval}'` end - def get_cron_interval + def get_cron_intervals list = get_cron_list - enteries = list.split("\n") - enteries.each do |entry| - return convert_to_readable_format(entry) if entry.include? "rsnapshot" - end + intervals = list.split(" ") end - def convert_to_readable_format(cron_string) - if cron_string.include? "0 * * * *" - return "hourly" - elsif cron_string.include? "0 0 * * 6" - return "weekly" - elsif cron_string.include? "0 0 * * *" - return "daily" - else - return nil - end + def remove_all_crons + `sudo /var/hda/apps/03qjfjl1sh/elevated/crontab-util 'reset'` end end