mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-11 15:56:05 -07:00
Made the store backup clean up some of the older backups (> 7 days).
This commit is contained in:
parent
15fae26397
commit
809b2bf0a8
2 changed files with 58 additions and 2 deletions
|
@ -27,6 +27,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
using NLog;
|
using NLog;
|
||||||
|
|
||||||
|
@ -37,7 +38,6 @@ using PlexRequests.Store.Repository;
|
||||||
|
|
||||||
using Quartz;
|
using Quartz;
|
||||||
|
|
||||||
using Directory = System.IO.Directory;
|
|
||||||
|
|
||||||
namespace PlexRequests.Services.Jobs
|
namespace PlexRequests.Services.Jobs
|
||||||
{
|
{
|
||||||
|
@ -57,6 +57,7 @@ namespace PlexRequests.Services.Jobs
|
||||||
public void Execute(IJobExecutionContext context)
|
public void Execute(IJobExecutionContext context)
|
||||||
{
|
{
|
||||||
TakeBackup();
|
TakeBackup();
|
||||||
|
Cleanup ();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void TakeBackup()
|
private void TakeBackup()
|
||||||
|
@ -79,9 +80,12 @@ namespace PlexRequests.Services.Jobs
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
|
{
|
||||||
|
if(DoWeNeedToBackUp(backupDir.FullName))
|
||||||
{
|
{
|
||||||
File.Copy(dbPath, Path.Combine(backupDir.FullName, $"PlexRequests.sqlite_{DateTime.Now.ToString("yyyy-MM-dd hh.mm.ss")}.bak"));
|
File.Copy(dbPath, Path.Combine(backupDir.FullName, $"PlexRequests.sqlite_{DateTime.Now.ToString("yyyy-MM-dd hh.mm.ss")}.bak"));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Log.Warn(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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -23,6 +23,9 @@
|
||||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
// ***********************************************************************
|
// ***********************************************************************
|
||||||
|
using System.Globalization;
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
using System;
|
using System;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue