From 809b2bf0a87702952ee18172d68f436799f1c7b6 Mon Sep 17 00:00:00 2001 From: TidusJar Date: Sun, 15 May 2016 23:39:40 -0400 Subject: [PATCH] Made the store backup clean up some of the older backups (> 7 days). --- PlexRequests.Services/Jobs/StoreBackup.cs | 57 ++++++++++++++++++++++- PlexRequests.Store/DbConfiguration.cs | 3 ++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/PlexRequests.Services/Jobs/StoreBackup.cs b/PlexRequests.Services/Jobs/StoreBackup.cs index 64a1fbc57..f64a7361e 100644 --- a/PlexRequests.Services/Jobs/StoreBackup.cs +++ b/PlexRequests.Services/Jobs/StoreBackup.cs @@ -27,6 +27,7 @@ using System; using System.IO; using System.Linq; +using System.Globalization; using NLog; @@ -37,7 +38,6 @@ using PlexRequests.Store.Repository; using Quartz; -using Directory = System.IO.Directory; namespace PlexRequests.Services.Jobs { @@ -57,6 +57,7 @@ namespace PlexRequests.Services.Jobs public void Execute(IJobExecutionContext context) { TakeBackup(); + Cleanup (); } private void TakeBackup() @@ -80,8 +81,11 @@ namespace PlexRequests.Services.Jobs try { + if(DoWeNeedToBackUp(backupDir.FullName)) + { File.Copy(dbPath, Path.Combine(backupDir.FullName, $"PlexRequests.sqlite_{DateTime.Now.ToString("yyyy-MM-dd hh.mm.ss")}.bak")); - } + } + } catch (Exception e) { Log.Warn(e); @@ -93,5 +97,54 @@ namespace PlexRequests.Services.Jobs } } + + private void Cleanup() + { + Log.Trace("Starting DB Cleanup"); + var dbPath = Sql.CurrentPath; + var dir = Path.GetDirectoryName(dbPath); + if (dir == null) + { + Log.Warn("We couldn't find the DB path. We cannot backup."); + return; + } + var backupDir = Directory.CreateDirectory(Path.Combine(dir, "Backup")); + + var files = backupDir.GetFiles(); + + foreach (var file in files) { + var dt = ParseName(file.Name); + if(dt < DateTime.Now.AddDays(-7)){ + try { + + File.Delete(file.FullName); + } catch (Exception ex) { + Log.Error(ex); + } + } + } + + } + + private bool DoWeNeedToBackup(string backupPath) + { + var files = Directory.GetFiles(backupPath); + //TODO Get the latest file and if it's within an hour of DateTime.Now then don't bother backing up. + return true; + } + + private DateTime ParseName(string fileName) + { + var names = fileName.Split(new []{'_','.',' '}, StringSplitOptions.RemoveEmptyEntries); + if(names.Count() > 1) + { + DateTime parsed; + //DateTime.TryParseExcat(names[1], "yyyy-MM-dd hh.mm.ss",CultureInfo.CurrentUICulture, DateTimeStyles.None, out parsed); + DateTime.TryParse(names[2], out parsed); + return parsed; + + } + return DateTime.MinValue; + } } } \ No newline at end of file diff --git a/PlexRequests.Store/DbConfiguration.cs b/PlexRequests.Store/DbConfiguration.cs index 27e7f20f9..eb8458d02 100644 --- a/PlexRequests.Store/DbConfiguration.cs +++ b/PlexRequests.Store/DbConfiguration.cs @@ -23,6 +23,9 @@ // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // *********************************************************************** +using System.Globalization; + + #endregion using System; using System.Data;