From 93ee97123fb097f82768e13bbc7d71b9573f5312 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Wed, 25 Jan 2012 23:56:05 -0800 Subject: [PATCH] System/Backup will backup Config.xml and NzbDrone.sdf to a zip file for the client to download. --- .../ExtractArchiveFixture.cs | 5 +-- .../Providers/Core/ArchiveProvider.cs | 40 ++++++++++++++++++- NzbDrone.Web/Controllers/SystemController.cs | 14 +++++-- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/NzbDrone.Core.Test/ProviderTests/DiskProviderTests/ExtractArchiveFixture.cs b/NzbDrone.Core.Test/ProviderTests/DiskProviderTests/ExtractArchiveFixture.cs index 60256ab1d..0cb4071e6 100644 --- a/NzbDrone.Core.Test/ProviderTests/DiskProviderTests/ExtractArchiveFixture.cs +++ b/NzbDrone.Core.Test/ProviderTests/DiskProviderTests/ExtractArchiveFixture.cs @@ -12,11 +12,8 @@ public class ExtractArchiveFixture : CoreTest [Test] public void Should_extract_to_correct_folder() { - var archiveProvider = new ArchiveProvider(); - var destination = Path.Combine(TempFolder, "destination"); - archiveProvider.ExtractArchive(GetTestFilePath("TestArchive.zip"), destination); - + Mocker.Resolve().ExtractArchive(GetTestFilePath("TestArchive.zip"), destination); var destinationFolder = new DirectoryInfo(destination); diff --git a/NzbDrone.Core/Providers/Core/ArchiveProvider.cs b/NzbDrone.Core/Providers/Core/ArchiveProvider.cs index aa2de9abf..18d87729b 100644 --- a/NzbDrone.Core/Providers/Core/ArchiveProvider.cs +++ b/NzbDrone.Core/Providers/Core/ArchiveProvider.cs @@ -1,13 +1,28 @@ -using System.Linq; +using System; +using System.IO; +using System.Linq; using Ionic.Zip; using NLog; +using Ninject; +using NzbDrone.Common; namespace NzbDrone.Core.Providers.Core { public class ArchiveProvider { + private readonly EnviromentProvider _enviromentProvider; private static readonly Logger logger = LogManager.GetCurrentClassLogger(); + [Inject] + public ArchiveProvider(EnviromentProvider enviromentProvider) + { + _enviromentProvider = enviromentProvider; + } + + public ArchiveProvider() + { + + } public virtual void ExtractArchive(string compressedFile, string destination) { @@ -21,5 +36,28 @@ public virtual void ExtractArchive(string compressedFile, string destination) logger.Trace("Extraction complete."); } + public virtual FileInfo CreateBackupZip() + { + try + { + var dbFile = PathExtentions.GetNzbDronoeDbFile(_enviromentProvider); + var configFile = PathExtentions.GetConfigPath(_enviromentProvider); + var zipFile = Path.Combine(PathExtentions.GetAppDataPath(_enviromentProvider), "NzbDrone_Backup.zip"); + + using (var zip = new ZipFile()) + { + zip.AddFile(dbFile, String.Empty); + zip.AddFile(configFile, String.Empty); + zip.Save(zipFile); + } + + return new FileInfo(zipFile); + } + catch(Exception ex) + { + logger.WarnException("Failed to create backup zip", ex); + return null; + } + } } } \ No newline at end of file diff --git a/NzbDrone.Web/Controllers/SystemController.cs b/NzbDrone.Web/Controllers/SystemController.cs index f5d0c43df..f58d72938 100644 --- a/NzbDrone.Web/Controllers/SystemController.cs +++ b/NzbDrone.Web/Controllers/SystemController.cs @@ -19,13 +19,17 @@ public class SystemController : Controller private readonly IndexerProvider _indexerProvider; private readonly ConfigProvider _configProvider; private readonly DiskProvider _diskProvider; + private readonly ArchiveProvider _archiveProvider; - public SystemController(JobProvider jobProvider, IndexerProvider indexerProvider, ConfigProvider configProvider, DiskProvider diskProvider) + public SystemController(JobProvider jobProvider, IndexerProvider indexerProvider, + ConfigProvider configProvider, DiskProvider diskProvider, + ArchiveProvider archiveProvider) { _jobProvider = jobProvider; _indexerProvider = indexerProvider; _configProvider = configProvider; _diskProvider = diskProvider; + _archiveProvider = archiveProvider; } public ActionResult Jobs() @@ -48,13 +52,11 @@ public ActionResult Indexers() return View(_indexerProvider.All()); } - public ActionResult Config() { return View(_configProvider.All()); } - [GridAction] public ActionResult _SelectAjaxEditing() { @@ -144,5 +146,11 @@ public JsonResult RunJob(string typeName) return JsonNotificationResult.Info("Job Queued"); } + + public ActionResult Backup() + { + var file = _archiveProvider.CreateBackupZip(); + return File(file.FullName, "application/binary", file.Name); + } } }