restore backup

pull/1/head
Suresh Alse 2015-06-13 08:23:13 +05:30
parent 9c48e80748
commit 848bf44f3a
8 changed files with 129 additions and 8 deletions

26
app.js
View File

@ -1,6 +1,7 @@
var express = require('express');
var app = express();
var crontab = require("./crontab");
var restore = require("./restore");
// include the routes
var routes = require("./routes").routes;
@ -24,8 +25,6 @@ app.set('port', (process.env.PORT || 8000));
app.get(routes.root, function(req, res) {
// get all the crontabs
//crontab.create_new("/usr/bin/find", "0 2 12 * *");
//crontab.create_new("/sbin/ping -c 1 192.168.0.1 > /dev/null", "* * * * *");
crontab.crontabs( function(docs){
res.render('index', {
routes : JSON.stringify(routes),
@ -70,6 +69,29 @@ app.get(routes.backup, function(req, res) {
crontab.backup();
res.end();
})
app.get(routes.restore, function(req, res) {
// get all the crontabs
restore.crontabs(req.query.db, function(docs){
res.render('restore', {
routes : JSON.stringify(routes),
crontabs : JSON.stringify(docs),
backups : crontab.get_backup_names(),
db: req.query.db
});
});
})
app.get(routes.delete_backup, function(req, res) {
restore.delete(req.query.db);
res.end();
})
app.get(routes.restore_backup, function(req, res) {
crontab.restore(req.query.db);
res.end();
})
app.listen(app.get('port'), function() {
console.log("Crontab UI is running at localhost:" + app.get('port'))
})

View File

@ -66,5 +66,10 @@ exports.get_backup_names = function(){
exports.backup = function(){
//TODO check if it failed
fs.createReadStream( __dirname + '/crontabs/crontab.db').pipe(fs.createWriteStream( __dirname + '/crontabs/backup ' + (new Date()).toString() + '.db'));
fs.createReadStream( __dirname + '/crontabs/crontab.db').pipe(fs.createWriteStream( __dirname + '/crontabs/backup ' + (new Date()).toString().replace("+", " ") + '.db'));
}
exports.restore = function(db_name){
fs.createReadStream( __dirname + '/crontabs/' + db_name).pipe(fs.createWriteStream( __dirname + '/crontabs/crontab.db'));
db.loadDatabase();
}

View File

@ -115,6 +115,23 @@ function doBackup(){
});
}
function delete_backup(db_name){
messageBox("<p> Do you want to delete this backup? </p>", "Confirm delete", null, null, function(){
$.get(routes.delete_backup, {db: db_name}, function(){
location = routes.root;
});
});
}
function restore_backup(db_name){
messageBox("<p> Do you want to restore this backup? </p>", "Confirm restore", null, null, function(){
$.get(routes.restore_backup, {db: db_name}, function(){
location = routes.root;
});
});
}
// script corresponding to job popup management
var schedule = "";
var job_command = "";

19
restore.js Normal file
View File

@ -0,0 +1,19 @@
//load database
var Datastore = require('nedb');
var exec = require('child_process').exec;
var fs = require('fs');
exports.crontabs = function(db_name, callback){
var db = new Datastore({ filename: __dirname + '/crontabs/' + db_name });
db.loadDatabase(function (err) {
});
db.find({}, function(err, docs){
callback(docs);
});
}
exports.delete = function(db_name){
fs.unlink(__dirname + '/crontabs/' + db_name);
}

View File

@ -1,10 +1,12 @@
exports.routes = {
"root" : "/",
"downloads" : "/downloads",
"save" : "/save",
"crontab" : "/crontab",
"stop" : "/stop",
"start" : "/start",
"remove": "/remove",
"backup": "/backup",
"restore": "/restore",
"delete_backup": "/delete",
"restore_backup": "/restore_backup"
}

View File

@ -69,8 +69,6 @@
<a class="btn btn-info"><span class="glyphicon glyphicon-import" aria-hidden="true"></span> Import from 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>
<% include popup.ejs %>
</body>
</html>

View File

@ -8,7 +8,7 @@
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="<%= routes.root %>">Crontab UI</a>
<a class="navbar-brand" href="<%= JSON.parse(routes).root %>">Crontab UI</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
@ -22,7 +22,7 @@
<li><a href="#">None</a></li>
<% } else { %>
<% backups.forEach(function(file){ %>
<li><a href="#"><%= file %></a></li>
<li><a href="<%= JSON.parse(routes).restore %>?db=<%= file %>"><%= file %></a></li>
<% }) %>
<% } %>
</ul>

58
views/restore.ejs Normal file
View File

@ -0,0 +1,58 @@
<!doctype html>
<head>
<title>Crontab UI</title>
<script src="jquery.js"></script>
<script src="script.js"></script>
<script src="bootstrap.min.js"></script>
<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>
<body>
<% include navbar %>
<div class="container">
<h2><%= db %></h2>
<table class="table">
<tr>
<th></th>
<th>Job</th>
<th>Time</th>
<th>Last Modified</th>
</tr>
<% JSON.parse(crontabs).forEach(function(crontab){ %>
<!-- color based on crontab state -->
<% if (!crontab.stopped) { %>
<tr>
<% } else { %>
<tr style="background:#3A6DA6;color:#fff">
<% } %>
<td>
<% if (crontab.name) { %>
<a class="btn" data-toggle="tooltip" data-placement="right" title="<%= crontab.name %>"><span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span> </a>
<% } %>
</td>
<td><%= crontab.command %></td>
<td><%= crontab.schedule %></td>
<td><%= crontab.timestamp %></td>
</tr>
<% }); %>
</table>
<a class="btn btn-primary" onclick="restore_backup('<%= db %>')"><span class="glyphicon glyphicon-floppy-save" aria-hidden="true"></span> Restore</a>
<a class="btn btn-danger" onclick="delete_backup('<%= db %>')"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span> Delete</a>
</div>
<% include popup.ejs %>
</body>
</html>