take backup

pull/1/head
Suresh Alse 2015-06-13 00:49:46 +05:30
parent b73c16c8c4
commit 9c48e80748
7 changed files with 117 additions and 18 deletions

28
app.js
View File

@ -28,8 +28,9 @@ app.get(routes.root, function(req, res) {
//crontab.create_new("/sbin/ping -c 1 192.168.0.1 > /dev/null", "* * * * *");
crontab.crontabs( function(docs){
res.render('index', {
routes : routes,
crontabs : JSON.stringify(docs)
routes : JSON.stringify(routes),
crontabs : JSON.stringify(docs),
backups : crontab.get_backup_names()
});
});
})
@ -46,6 +47,29 @@ app.post(routes.save, function(req, res) {
res.end();
})
app.post(routes.stop, function(req, res) {
crontab.status(req.body._id, true);
res.end();
})
app.post(routes.start, function(req, res) {
crontab.status(req.body._id, false);
res.end();
})
app.post(routes.remove, function(req, res) {
crontab.remove(req.body._id);
res.end();
})
app.get(routes.crontab, function(req, res) {
crontab.set_crontab();
res.end();
})
app.get(routes.backup, function(req, res) {
crontab.backup();
res.end();
})
app.listen(app.get('port'), function() {
console.log("Crontab UI is running at localhost:" + app.get('port'))
})

View File

@ -24,6 +24,14 @@ exports.create_new = function(name, command, schedule){
exports.update = function(data){
db.update({_id: data._id}, crontab(data.name, data.command, data.schedule, null));
}
exports.status = function(_id, stopped){
db.update({_id: _id},{$set: {stopped: stopped}});
}
exports.remove = function(_id){
db.remove({_id: _id}, {});
}
exports.crontabs = function(callback){
db.find({}, function(err, docs){
callback(docs);
@ -43,3 +51,20 @@ exports.set_crontab = function(){
});
}
exports.get_backup_names = function(){
var backups = []
fs.readdirSync(__dirname + '/crontabs').forEach(function(file){
// file name begins with backup
if(file.indexOf("backup") == 0){
backups.push(file);
}
});
return backups;
}
exports.backup = function(){
//TODO check if it failed
fs.createReadStream( __dirname + '/crontabs/crontab.db').pipe(fs.createWriteStream( __dirname + '/crontabs/backup ' + (new Date()).toString() + '.db'));
}

View File

@ -1,10 +1,9 @@
/*********** MessageBox ****************/
// simply show info. Only close button
function infoMessageBox(message, title){
$("#modal-body").html(message);
$("#modal-title").html(title);
$("#modal-button").hide();
$("#popup").modal('show');
$("#info-body").html(message);
$("#info-title").html(title);
$("#info-popup").modal('show');
}
// modal with full control
function messageBox(body, title, ok_text, close_text, callback){
@ -23,23 +22,39 @@ function messageBox(body, title, ok_text, close_text, callback){
function deleteJob(_id){
// TODO fix this. pass callback properly
messageBox("<p> Do you want to delete this Job? </p>", "Confirm delete", null, null, function(){
console.log("delete job");
$.post(routes.remove, {_id: _id}, function(){
location.reload();
});
});
}
function stopJob(_id){
messageBox("<p> Do you want to stop this Job? </p>", "Confirm stop job", null, null, function(){
console.log("stop job");
$.post(routes.stop, {_id: _id}, function(){
location.reload();
});
});
}
function startJob(_id){
messageBox("<p> Do you want to start this Job? </p>", "Confirm start job", null, null, function(){
console.log("start job");
$.post(routes.start, {_id: _id}, function(){
location.reload();
});
});
}
function setCrontab(){
messageBox("<p> Do you want to set the crontab file? </p>", "Confirm crontab setup", null, null, function(){
$.get(routes.crontab, {}, function(){
// TODO show only if success
infoMessageBox("Successfuly set crontab file!","Information");
});
});
}
function editJob(_id){
console.log(_id);
var job = null;
crontabs.forEach(function(crontab){
if(crontab._id == _id)
@ -48,7 +63,7 @@ function editJob(_id){
if(job){
$("#job").modal("show");
$("#job-name").val(job.name);
$("#job-command").val(job.command);
$("#job-command").val(job.command.replace("\"","\\\""));
// if macro not used
if(job.schedule.indexOf("@") != 0){
var components = job.schedule.split(" ");
@ -65,7 +80,7 @@ function editJob(_id){
$("#job-save").click(function(){
// TODO good old boring validations
$.post("/save", {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: _id}, function(){
$.post(routes.save, {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: _id}, function(){
location.reload();
})
});
@ -86,12 +101,19 @@ function newJob(){
job_string();
$("#job-save").click(function(){
// TODO good old boring validations
$.post("/save", {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: -1}, function(){
$.post(routes.save, {name: $("#job-name").val(), command: job_command , schedule: schedule, _id: -1}, function(){
location.reload();
})
});
}
function doBackup(){
messageBox("<p> Do you want to take backup? </p>", "Confirm backup", null, null, function(){
$.get(routes.backup, {}, function(){
location.reload();
});
});
}
// script corresponding to job popup management
var schedule = "";
@ -105,4 +127,5 @@ function set_schedule(){
schedule = $("#job-minute").val() + " " +$("#job-hour").val() + " " +$("#job-day").val() + " " +$("#job-month").val() + " " +$("#job-week").val();
job_string();
}
// popup management ends

View File

@ -2,5 +2,9 @@ exports.routes = {
"root" : "/",
"downloads" : "/downloads",
"save" : "/save",
"crontab" : "/crontab"
"crontab" : "/crontab",
"stop" : "/stop",
"start" : "/start",
"remove": "/remove",
"backup": "/backup",
}

View File

@ -7,10 +7,13 @@
<link rel="stylesheet" href="bootstrap.min.css" />
<script type="text/javascript">
var crontabs = [];
var routes = [];
$(function () {
// initialize tooltips
$('[data-toggle="tooltip"]').tooltip();
// TODO handle the commands with quotes
crontabs = JSON.parse('<%- crontabs %>');
routes = JSON.parse('<%- routes %>');
})
</script>
</head>
@ -61,10 +64,10 @@
</table>
<a class="btn btn-primary" onclick="newJob();"><span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span> New</a>
<a class="btn btn-info"><span class="glyphicon glyphicon-floppy-save" aria-hidden="true"></span> Backup</a>
<a class="btn btn-info" onclick="doBackup();"><span class="glyphicon glyphicon-floppy-save" aria-hidden="true"></span> Backup</a>
<a class="btn btn-info"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> Export</a>
<a class="btn btn-info"><span class="glyphicon glyphicon-import" aria-hidden="true"></span> Import from crontab</a>
<a class="btn btn-success"><span class="glyphicon glyphicon-save" aria-hidden="true"></span> Save to crontab</a>
<a class="btn btn-success" onclick="setCrontab();"><span class="glyphicon glyphicon-save" aria-hidden="true"></span> Save to crontab</a>
</div>
<div>
</div>

View File

@ -15,12 +15,16 @@
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="#">Import</a></li>
<li><a href="<%= routes.downloads %>">Downloads<span class="sr-only">(current)</span></a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Backups <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<!-- TODO -->
<li><a href="#">None</a></li>
<% if (backups.length == 0){ %>
<li><a href="#">None</a></li>
<% } else { %>
<% backups.forEach(function(file){ %>
<li><a href="#"><%= file %></a></li>
<% }) %>
<% } %>
</ul>
</li>
</ul>

View File

@ -15,6 +15,22 @@
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<div class="modal fade" id="info-popup">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="info-title">Message</h4>
</div>
<div class="modal-body" id="info-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal" id="info-button">Ok</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<!-- Job -->
<div class="modal fade" id="job">
<div class="modal-dialog">