Skip to content

Commit

Permalink
update crontab_helper file, start/stop of backups done
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhbir-singh committed Jul 29, 2018
1 parent 9212f8c commit 1ff20b9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 46 deletions.
29 changes: 29 additions & 0 deletions app/assets/javascripts/rsnapshot_backups.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
});
17 changes: 6 additions & 11 deletions app/controllers/rsnapshot_backups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
8 changes: 6 additions & 2 deletions app/views/rsnapshot_backups/settings.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
54 changes: 21 additions & 33 deletions lib/rsnapshot_backups/crontab_helper.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 1ff20b9

Please sign in to comment.