Skip to content

Commit

Permalink
automatic daily data backups
Browse files Browse the repository at this point in the history
  • Loading branch information
epix37 committed Mar 12, 2015
1 parent dedf031 commit 56da870
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
9 changes: 7 additions & 2 deletions Hearthstone Deck Tracker/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ public class Config

[DefaultValue(new[] {0, 0, 0})]
//move this to some data file
public int[] GoldProgress = { 0, 0, 0 };
public int[] GoldProgress = {0, 0, 0};

//move this to some data file
public DateTime[] GoldProgressLastReset = { DateTime.MinValue, DateTime.MinValue, DateTime.MinValue };
public DateTime[] GoldProgressLastReset = {DateTime.MinValue, DateTime.MinValue, DateTime.MinValue};

[DefaultValue(new[] {0, 0, 0})]
//move this to some data file
Expand Down Expand Up @@ -681,6 +681,11 @@ public string HomeDir
get { return Instance.SaveInAppData ? AppDataPath + "/" : string.Empty; }
}

public string BackupDir
{
get { return Path.Combine(DataDir, "Backups"); }
}

public string ConfigPath
{
get { return Instance.ConfigDir + "config.xml"; }
Expand Down
1 change: 1 addition & 0 deletions Hearthstone Deck Tracker/Hearthstone Deck Tracker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@
<DependentUpon>OverlayPlayer.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\Annotations.cs" />
<Compile Include="Utility\BackupManager.cs" />
<Compile Include="Windows\AddGameDialog.xaml.cs">
<DependentUpon>AddGameDialog.xaml</DependentUpon>
</Compile>
Expand Down
1 change: 1 addition & 0 deletions Hearthstone Deck Tracker/MainWindow/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ public MainWindow()
UpdateOverlayAsync();
UpdateAsync();

BackupManager.Run();

_initialized = true;
}
Expand Down
66 changes: 66 additions & 0 deletions Hearthstone Deck Tracker/Utility/BackupManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#region

using System;
using System.IO;
using System.IO.Compression;
using System.Linq;

#endregion

namespace Hearthstone_Deck_Tracker.Utility
{
public class BackupManager
{
private const int MaxBackups = 7;
private static readonly string[] Files = {"PlayerDecks.xml", "DeckStats.xml", "DefaultDeckStats.xml"};

public static void Run()
{
Logger.WriteLine("Running BackupManager", "BackupManager");
if(!Directory.Exists(Config.Instance.BackupDir))
Directory.CreateDirectory(Config.Instance.BackupDir);
var dirInfo = new DirectoryInfo(Config.Instance.BackupDir);
var backupFileName = string.Format("Backup_{0}.zip", DateTime.Today.ToString("ddMMyyyy"));

try
{
var backups = dirInfo.GetFiles("Backup_*");
while(backups.Count() >= MaxBackups)
{
var oldest = backups.OrderBy(x => x.CreationTime).First();
Logger.WriteLine("Deleting old backup: " + oldest.Name, "BackupManager");
oldest.Delete();
backups = dirInfo.GetFiles("Backup_*");
}
}
catch(Exception ex)
{
Logger.WriteLine("Error deleting old backup: " + ex, "BackupManager");
}

if(dirInfo.GetFiles().Any(x => x.Name == backupFileName))
{
Logger.WriteLine("Backup for today already exists", "BackupManager");
return;
}

Logger.WriteLine("Creating backup for today", "BackupManager");
try
{
var backupFilePath = Path.Combine(Config.Instance.BackupDir, backupFileName);
using(var zip = ZipFile.Open(backupFilePath, ZipArchiveMode.Create))
{
foreach(var file in Files)
{
var path = Path.Combine(Config.Instance.DataDir, file);
zip.CreateEntryFromFile(path, file);
}
}
}
catch(Exception ex)
{
Logger.WriteLine("Error creating todays backup: " + ex, "BackupManager");
}
}
}
}

0 comments on commit 56da870

Please sign in to comment.