From 93ee97123fb097f82768e13bbc7d71b9573f5312 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Wed, 25 Jan 2012 23:56:05 -0800 Subject: [PATCH 1/3] 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 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskProviderTests [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 @@ namespace NzbDrone.Core.Providers.Core 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 @@ namespace NzbDrone.Web.Controllers 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 @@ namespace NzbDrone.Web.Controllers return View(_indexerProvider.All()); } - public ActionResult Config() { return View(_configProvider.All()); } - [GridAction] public ActionResult _SelectAjaxEditing() { @@ -144,5 +146,11 @@ namespace NzbDrone.Web.Controllers return JsonNotificationResult.Info("Job Queued"); } + + public ActionResult Backup() + { + var file = _archiveProvider.CreateBackupZip(); + return File(file.FullName, "application/binary", file.Name); + } } } From 928bc55839d5526be323102111c74a5223601b79 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 26 Jan 2012 19:59:30 -0800 Subject: [PATCH 2/3] New: Backup added to Settings -> System --- NzbDrone.Web/Views/Settings/System.cshtml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/NzbDrone.Web/Views/Settings/System.cshtml b/NzbDrone.Web/Views/Settings/System.cshtml index 319bd6ae3..e83a74e83 100644 --- a/NzbDrone.Web/Views/Settings/System.cshtml +++ b/NzbDrone.Web/Views/Settings/System.cshtml @@ -28,6 +28,11 @@ @Html.DescriptionFor(m => m.AuthenticationType) @Html.DropDownListFor(m => m.AuthenticationType, Model.AuthTypeSelectList, new { @class = "inputClass" }) + + + From 8ce6e8a08e30020ae4fca900b7794dad7aa5d317 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 26 Jan 2012 21:05:09 -0800 Subject: [PATCH 3/3] Cleaned up Backup. --- NzbDrone.Common/PathExtentions.cs | 8 +++ NzbDrone.Core/NzbDrone.Core.csproj | 1 + NzbDrone.Core/Providers/BackupProvider.cs | 53 +++++++++++++++++++ .../Providers/Core/ArchiveProvider.cs | 36 ------------- NzbDrone.Web/Controllers/SystemController.cs | 12 +++-- 5 files changed, 69 insertions(+), 41 deletions(-) create mode 100644 NzbDrone.Core/Providers/BackupProvider.cs diff --git a/NzbDrone.Common/PathExtentions.cs b/NzbDrone.Common/PathExtentions.cs index 58f997d34..d18f81304 100644 --- a/NzbDrone.Common/PathExtentions.cs +++ b/NzbDrone.Common/PathExtentions.cs @@ -17,6 +17,8 @@ namespace NzbDrone.Common public const string NZBDRONE_DB_FILE = "nzbdrone.sdf"; public const string LOG_DB_FILE = "log.sdf"; + private const string BACKUP_ZIP_FILE = "NzbDrone_Backup.zip"; + private const string UPDATE_SANDBOX_FOLDER_NAME = "nzbdrone_update\\"; private const string UPDATE_PACKAGE_FOLDER_NAME = "nzbdrone\\"; private const string UPDATE_BACKUP_FOLDER_NAME = "nzbdrone_backup\\"; @@ -144,9 +146,15 @@ namespace NzbDrone.Common { return Path.Combine(enviromentProvider.ApplicationPath, "nzbdrone.log.txt"); } + public static string GetArchivedLogFileName(this EnviromentProvider enviromentProvider) { return Path.Combine(enviromentProvider.ApplicationPath, "nzbdrone.log.0.txt"); } + + public static string GetConfigBackupFile(this EnviromentProvider enviromentProvider) + { + return Path.Combine(enviromentProvider.GetAppDataPath(), BACKUP_ZIP_FILE); + } } } \ No newline at end of file diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index a88c0925a..4d9492dd1 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -256,6 +256,7 @@ + diff --git a/NzbDrone.Core/Providers/BackupProvider.cs b/NzbDrone.Core/Providers/BackupProvider.cs new file mode 100644 index 000000000..734dd4d56 --- /dev/null +++ b/NzbDrone.Core/Providers/BackupProvider.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using Ionic.Zip; +using NLog; +using Ninject; +using NzbDrone.Common; + +namespace NzbDrone.Core.Providers +{ + public class BackupProvider + { + private readonly EnviromentProvider _enviromentProvider; + private static readonly Logger logger = LogManager.GetCurrentClassLogger(); + + [Inject] + public BackupProvider(EnviromentProvider enviromentProvider) + { + _enviromentProvider = enviromentProvider; + } + + public BackupProvider() + { + + } + + public virtual string CreateBackupZip() + { + try + { + var dbFile = _enviromentProvider.GetNzbDronoeDbFile(); + var configFile = _enviromentProvider.GetConfigPath(); + var zipFile = _enviromentProvider.GetConfigBackupFile(); + + using (var zip = new ZipFile()) + { + zip.AddFile(dbFile, String.Empty); + zip.AddFile(configFile, String.Empty); + zip.Save(zipFile); + } + + return zipFile; + } + catch (Exception ex) + { + logger.ErrorException("Failed to create backup zip", ex); + throw; + } + } + } +} diff --git a/NzbDrone.Core/Providers/Core/ArchiveProvider.cs b/NzbDrone.Core/Providers/Core/ArchiveProvider.cs index 18d87729b..48de6a983 100644 --- a/NzbDrone.Core/Providers/Core/ArchiveProvider.cs +++ b/NzbDrone.Core/Providers/Core/ArchiveProvider.cs @@ -10,19 +10,7 @@ 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) { @@ -35,29 +23,5 @@ namespace NzbDrone.Core.Providers.Core 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 f58d72938..87a3789c5 100644 --- a/NzbDrone.Web/Controllers/SystemController.cs +++ b/NzbDrone.Web/Controllers/SystemController.cs @@ -19,17 +19,17 @@ namespace NzbDrone.Web.Controllers private readonly IndexerProvider _indexerProvider; private readonly ConfigProvider _configProvider; private readonly DiskProvider _diskProvider; - private readonly ArchiveProvider _archiveProvider; + private readonly BackupProvider _backupProvider; public SystemController(JobProvider jobProvider, IndexerProvider indexerProvider, ConfigProvider configProvider, DiskProvider diskProvider, - ArchiveProvider archiveProvider) + BackupProvider backupProvider) { _jobProvider = jobProvider; _indexerProvider = indexerProvider; _configProvider = configProvider; _diskProvider = diskProvider; - _archiveProvider = archiveProvider; + _backupProvider = backupProvider; } public ActionResult Jobs() @@ -149,8 +149,10 @@ namespace NzbDrone.Web.Controllers public ActionResult Backup() { - var file = _archiveProvider.CreateBackupZip(); - return File(file.FullName, "application/binary", file.Name); + var file = _backupProvider.CreateBackupZip(); + var fileInfo = new FileInfo(file); + + return File(fileInfo.FullName, "application/binary", fileInfo.Name); } } }