From 78cc203d188593ee4680a19a01e5ef80059aff41 Mon Sep 17 00:00:00 2001 From: Kenneth Skovhede Date: Fri, 25 Jul 2014 22:17:48 +0200 Subject: [PATCH] Made a check to pair up an existing local database (e.g. from a commandline job) in the GUI --- Duplicati/Library/Main/DatabaseLocator.cs | 3 ++ Duplicati/Server/WebServer/ControlHandler.cs | 23 ++++++++++-- .../ControlMethods/AddOrUpdateBackup.cs | 9 +++++ Duplicati/Server/webroot/scripts/app.js | 15 ++++++-- .../Server/webroot/scripts/editdialog.js | 36 ++++++++++++++----- 5 files changed, 73 insertions(+), 13 deletions(-) diff --git a/Duplicati/Library/Main/DatabaseLocator.cs b/Duplicati/Library/Main/DatabaseLocator.cs index 7bbd4f8cd..8fd758826 100644 --- a/Duplicati/Library/Main/DatabaseLocator.cs +++ b/Duplicati/Library/Main/DatabaseLocator.cs @@ -39,6 +39,9 @@ namespace Duplicati.Library.Main public static string GetDatabasePath(string backend, Options options, bool autoCreate = true, bool anyUsername = false) { + if (options == null) + options = new Options(new Dictionary()); + if (!string.IsNullOrEmpty(options.Dbpath)) return options.Dbpath; diff --git a/Duplicati/Server/WebServer/ControlHandler.cs b/Duplicati/Server/WebServer/ControlHandler.cs index 53bda03d7..ee0731bb2 100644 --- a/Duplicati/Server/WebServer/ControlHandler.cs +++ b/Duplicati/Server/WebServer/ControlHandler.cs @@ -59,6 +59,7 @@ namespace Duplicati.Server.WebServer SUPPORTED_METHODS.Add("read-log", ReadLogData); SUPPORTED_METHODS.Add("get-license-data", GetLicenseData); SUPPORTED_METHODS.Add("get-changelog", GetChangelog); + SUPPORTED_METHODS.Add("locate-uri-db", LocateUriDb); } public override bool Process (HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session) @@ -180,6 +181,25 @@ namespace Duplicati.Server.WebServer return result; } + private void LocateUriDb(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw) + { + HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString; + var uri = input["uri"].Value; + + if (string.IsNullOrWhiteSpace(uri)) + { + ReportError(response, bw, "Invalid request, missing uri"); + } + else + { + var path = Library.Main.DatabaseLocator.GetDatabasePath(uri, null, false, false); + + bw.SetOK(); + bw.WriteJsonObject(new {Exists = !string.IsNullOrWhiteSpace(path)}); + } + + } + private void GetChangelog(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw) { HttpServer.HttpInput input = request.Method.ToUpper() == "POST" ? request.Form : request.QueryString; @@ -212,9 +232,6 @@ namespace Duplicati.Server.WebServer }); } } - - - } private void GetLicenseData(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw) diff --git a/Duplicati/Server/WebServer/ControlMethods/AddOrUpdateBackup.cs b/Duplicati/Server/WebServer/ControlMethods/AddOrUpdateBackup.cs index 07fbd8d0e..ada2c5fb6 100644 --- a/Duplicati/Server/WebServer/ControlMethods/AddOrUpdateBackup.cs +++ b/Duplicati/Server/WebServer/ControlMethods/AddOrUpdateBackup.cs @@ -102,6 +102,7 @@ namespace Duplicati.Server.WebServer private void AddBackup(HttpServer.IHttpRequest request, HttpServer.IHttpResponse response, HttpServer.Sessions.IHttpSession session, BodyWriter bw) { var str = request.Form["data"].Value; + if (string.IsNullOrWhiteSpace(str)) { ReportError(response, bw, "Missing backup object"); @@ -128,6 +129,14 @@ namespace Duplicati.Server.WebServer } else { + if (Library.Utility.Utility.ParseBool(request.Form["existing_db"].Value, false)) + { + data.Backup.DBPath = Library.Main.DatabaseLocator.GetDatabasePath(data.Backup.TargetURL, null, false, false); + if (string.IsNullOrWhiteSpace(data.Backup.DBPath)) + throw new Exception("Unable to find remote db path?"); + } + + lock(Program.DataConnection.m_lock) { if (Program.DataConnection.Backups.Where(x => x.Name.Equals(data.Backup.Name, StringComparison.InvariantCultureIgnoreCase)).Any()) diff --git a/Duplicati/Server/webroot/scripts/app.js b/Duplicati/Server/webroot/scripts/app.js index 1a47bcb87..e6d439ec7 100644 --- a/Duplicati/Server/webroot/scripts/app.js +++ b/Duplicati/Server/webroot/scripts/app.js @@ -736,9 +736,13 @@ $(document).ready(function() { serverWithCallback({ action: 'get-backup', id: id}, callback, errorhandler); }; - APP_DATA.addBackup = function(cfg, callback, errorhandler) { + APP_DATA.addBackup = function(cfg, callback, errorhandler, extra_options) { serverWithCallback( - { action: 'add-backup', HTTP_METHOD: 'POST', data: JSON.stringify(cfg)}, + $.extend( + {}, + extra_options, + { action: 'add-backup', HTTP_METHOD: 'POST', data: JSON.stringify(cfg)} + ), callback, errorhandler ); @@ -752,6 +756,13 @@ $(document).ready(function() { ); }; + APP_DATA.locateUriDb = function(targeturl, callback, errorhandler) { + serverWithCallback( + { action: 'locate-uri-db', HTTP_METHOD: 'POST', uri: targeturl}, + callback, + errorhandler + ); + }; APP_DATA.editNewBackup = function() { APP_DATA.editBackup(); diff --git a/Duplicati/Server/webroot/scripts/editdialog.js b/Duplicati/Server/webroot/scripts/editdialog.js index ae7922fa3..0b2f698be 100644 --- a/Duplicati/Server/webroot/scripts/editdialog.js +++ b/Duplicati/Server/webroot/scripts/editdialog.js @@ -688,15 +688,35 @@ $(document).ready(function() { for(var k in set) obj.Backup.Settings.push({Name: k, Value: set[k]}); - var method = EDIT_STATE.newBackup ? APP_DATA.addBackup : APP_DATA.updateBackup; - method(obj, function() { - EDIT_STATE.dataModified = false; - $('#edit-dialog').dialog('close'); - }, - function(data, succes, status) { - alert('Could not save: ' + status); - }); + if (EDIT_STATE.newBackup) { + APP_DATA.locateUriDb(obj.Backup.TargetURL, function(res) { + var existing_db = (res.Exists && confirm("An existing local database for the storage has been found.\nRe-using the database will allow the commandline and server instances to work on the same remote storage.\n\n Do you wish to use the existing database?")) ? true : false; + + APP_DATA.addBackup(obj, function() { + EDIT_STATE.dataModified = false; + $('#edit-dialog').dialog('close'); + }, + function(data, succes, status) { + alert('Could not save: ' + status); + }, + { + existing_db: existing_db + }); + }, + function(data, succes, status) { + alert('Could not save: ' + status); + }); + + } else { + APP_DATA.updateBackup(obj, function() { + EDIT_STATE.dataModified = false; + $('#edit-dialog').dialog('close'); + }, + function(data, succes, status) { + alert('Could not save: ' + status); + }); + } } });