Skip to content

Commit

Permalink
Split Manual and pre-restore backup into separate folders (now they a…
Browse files Browse the repository at this point in the history
…re not counted towords size limit and are never deleted), fixed som typos
  • Loading branch information
sebeksd committed Mar 13, 2020
1 parent b615677 commit c6ee1ce
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 26 deletions.
2 changes: 1 addition & 1 deletion SaveGame Backup Tool/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private void Application_Startup(object sender, StartupEventArgs e)
BackupMaker lBackupMaker = new BackupMaker();
string lErrorMessage = "";

if (lBackupMaker.MakeBackup(lBackupTask, "", ref lErrorMessage))
if (lBackupMaker.MakeBackup(lBackupTask, BackupType.btNormal, ref lErrorMessage))
Console.WriteLine("SUCCEED: Task '" + lBackupTask.Settings.Name + "' backup created");
else
Console.WriteLine("FAILED: Task '" + lBackupTask.Settings.Name + "' backup failed: \r\n" + lErrorMessage);
Expand Down
55 changes: 49 additions & 6 deletions SaveGame Backup Tool/BackupMaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ You should have received a copy of the GNU Lesser General Public License

namespace SaveGameBackupTool
{
public enum BackupType
{
btNormal,
btManual,
btRestore
}

public class BackupMaker
{
public BackupMaker()
Expand All @@ -33,6 +40,41 @@ public BackupMaker()

}

private string BackupTypeToFilePostfix(BackupType lType)
{
switch (lType)
{
case BackupType.btManual:
return "-manual";
case BackupType.btRestore:
return "-pre_restore";
default:
return "";
}
}

private string DirectoryForBakupType(BackupType lType, string lDestinationPath)
{
string lSubFolder;
switch (lType)
{
case BackupType.btManual:
lSubFolder = "Manual";
break;
case BackupType.btRestore:
lSubFolder = "OnRestore";
break;
default:
lSubFolder = "";
break;
}

if (!String.IsNullOrEmpty(lSubFolder))
return Path.Combine(lDestinationPath, lSubFolder + "\\");
else
return lDestinationPath;
}

private bool CheckFilterForFile(string lFileFilterRegex, string lFileName)
{
// TODO Performance: make regex object persistent through whole Task scan
Expand Down Expand Up @@ -179,22 +221,23 @@ public void CleanUpOldBackups(BackupTask lBackupTask, ref string pErrorMessage)
}
}

public bool MakeBackup(BackupTask lBackupTask, string lPostfix, ref string pErrorMessage)
public bool MakeBackup(BackupTask lBackupTask, BackupType lType, ref string pErrorMessage)
{
string lBackupFileName = DateTime.Now.ToString(lBackupTask.Settings.BackupFileNamePattern);
lBackupFileName += lPostfix;
lBackupFileName += BackupTypeToFilePostfix(lType);
lBackupFileName += ".zip";
string lDestinationPath = DirectoryForBakupType(lType, lBackupTask.Settings.DestinationPath);

try
{
// to be sure that backup dir exists
Directory.CreateDirectory(lBackupTask.Settings.DestinationPath);
Directory.CreateDirectory(lDestinationPath);

// try to make a backup
if (lBackupTask.Settings.SourcePathHelper.IsFile)
ZipHelper.CreateFromFile(lBackupTask.Settings.SourcePathHelper.FilePath, lBackupTask.Settings.DestinationPath + lBackupFileName, CompressionLevel.Fastest, false, null, null);
ZipHelper.CreateFromFile(lBackupTask.Settings.SourcePathHelper.FilePath, lDestinationPath + lBackupFileName, CompressionLevel.Fastest, false, null, null);
else
ZipHelper.CreateFromDirectory(lBackupTask.Settings.SourcePathHelper.DirectoryPath, lBackupTask.Settings.DestinationPath + lBackupFileName, CompressionLevel.Fastest, false, null, lFilterFileName => !CheckFilterForFile(lBackupTask.Settings.FileFilterRegex, lFilterFileName));
ZipHelper.CreateFromDirectory(lBackupTask.Settings.SourcePathHelper.DirectoryPath, lDestinationPath + lBackupFileName, CompressionLevel.Fastest, false, null, lFilterFileName => !CheckFilterForFile(lBackupTask.Settings.FileFilterRegex, lFilterFileName));

pErrorMessage = "";

Expand All @@ -207,7 +250,7 @@ public bool MakeBackup(BackupTask lBackupTask, string lPostfix, ref string pErro
try
{
// probably file was created but content is corrupted or just not complete, remove it
File.Delete(lBackupTask.Settings.DestinationPath + lBackupFileName);
File.Delete(lDestinationPath + lBackupFileName);
}
catch
{
Expand Down
2 changes: 1 addition & 1 deletion SaveGame Backup Tool/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<Button x:Name="buttonAbout" Content="About" Margin="0,3,7,0" VerticalAlignment="Top" Height="22" Click="buttonAbout_Click" HorizontalAlignment="Right" Width="42"/>
<Label x:Name="labelDestinationDirSize" Content="Destination dir size:" HorizontalAlignment="Left" Margin="0,256,0,0" VerticalAlignment="Top"/>
<Label x:Name="labelDestinationDirSizeValue" Content="0" HorizontalAlignment="Left" Margin="113,256,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.634,0.445"/>
<CheckBox x:Name="checkBoxAutomaticDestinationDirSizeLimit" Content="Limit destination directory size [MB]" HorizontalAlignment="Left" Margin="9,208,0,0" VerticalAlignment="Top" Width="513" Checked="checkBoxAutomaticDestinationDirSizeLimit_CheckedChanged" Unchecked="checkBoxAutomaticDestinationDirSizeLimit_CheckedChanged"/>
<CheckBox x:Name="checkBoxAutomaticDestinationDirSizeLimit" Content="Limit automatic backup space [MB]" HorizontalAlignment="Left" Margin="9,208,0,0" VerticalAlignment="Top" Width="513" Checked="checkBoxAutomaticDestinationDirSizeLimit_CheckedChanged" Unchecked="checkBoxAutomaticDestinationDirSizeLimit_CheckedChanged"/>
<xctk:DecimalUpDown x:Name="decimalUpDownDestinationDirSizeLimit" Margin="0,208,8,0" VerticalAlignment="Top" Maximum="99999" Minimum="1" Value="1000" Text="Backup" HorizontalAlignment="Right" Width="57" ValueChanged="decimalUpDownDestinationDirSizeLimit_ValueChanged"/>
<Button x:Name="ButtonTypicalLocations" Content="Typical locations" Margin="0,58,74,0" Click="ButtonTypicalLocations_Click" HorizontalAlignment="Right" Width="101" Height="24" VerticalAlignment="Top"/>
<Button x:Name="buttonRestore" Content="Restore" Margin="0,258,128,0" VerticalAlignment="Top" HorizontalAlignment="Right" Width="55" Height="22" Click="ButtonRestore_Click" RenderTransformOrigin="0.462,1.411"/>
Expand Down
12 changes: 7 additions & 5 deletions SaveGame Backup Tool/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,17 +213,19 @@ private void SetToolTips()
textBoxBackupFileNamePattern.ToolTip = "Backup create new ZIP file, this pattern is used to name this result file.\r\n\r\n" +
"Remember to set pattern the way that file name will be unique (eg time),\r\n" +
"function to create name from the pattern is DateTime.ToString for more info about this pattern possibilities search Internet :)";
checkBoxAutomaticBackup.ToolTip = "Check if you want this tusk to be run every few minutes (backup will be made only if files were modified).";
checkBoxAutomaticBackup.ToolTip = "Check if you want this task to be run every few minutes (backup will be made only if files were modified).";
buttonForceBackup.ToolTip = "Perform a backup regardless of whether the files have been modified, if files are locked it will fail.";
decimalUpDownBackupEvery.ToolTip = "Task will launch every few minutes (value set in this field) and it will check if file was modified and also if file is not locked.\r\n" +
"If file was not modified from last backup or it is locked for reading then next check will be triggered every 30 seconds (till backup is made).";
rectangleStatusIcon.ToolTip = "Global tasks status (works only for automated tasks), blue - tasks not checked, green - all tasks succeed or nothing to do, red - some task failed.\r\n" +
"In tray icon mode this status is also presented by icon in tray.";

checkBoxAutomaticDestinationDirSizeLimit.ToolTip = "If this option is checked application will cleanup older files/backups from destination directory to maintain size limit set in by user (at least 4 last files remains).\r\n" +
"WARNING! Make sure destination directory is used only by one task/game because files are removed by creation date and file names are not used to chcek if file is from different task!";
"Manual and pre-restore backups are not counted towards this limit and they are not deleted (if needed you need to clean them manually).\r\n" +
"WARNING! Make sure destination directory is used only by one task/game because files are removed by creation date and file names are not used to check if file is from different task!";
decimalUpDownDestinationDirSizeLimit.ToolTip = "Limit value in [MB] for automated deletion of older files/backups from destination directory (at least 4 last files remains).\r\n" +
"WARNING! Make sure destination directory is used only by one task/game because files are removed by creation date and file names are not used to chcek if file is from different task!";
"Manual and pre-restore backups are not counted towards this limit and they are not deleted (if needed you need to clean them manually).\r\n" +
"WARNING! Make sure destination directory is used only by one task/game because files are removed by creation date and file names are not used to check if file is from different task!";
}

private void UpdateGlobalStatus(bool lSuccess)
Expand Down Expand Up @@ -430,7 +432,7 @@ private void CheckTasks()
else if (lFileWasModified)
{
// success and file was modified
lErrorOccurred = !fBackupMaker.MakeBackup(lBackupTask, "", ref lErrorMessage);
lErrorOccurred = !fBackupMaker.MakeBackup(lBackupTask, BackupType.btNormal, ref lErrorMessage);
lBackupTask.SetLastBackupStatus(!lErrorOccurred, lErrorMessage);
lRefreshComboBox = true;
}
Expand Down Expand Up @@ -581,7 +583,7 @@ private void buttonForceBackup_Click(object sender, RoutedEventArgs e)
BackupTask lBackupTask = fSettings.Settings.BackupTasks[fSelectedBackupTaskIndex];

string lErrorMessage = "";
lBackupTask.SetLastBackupStatus(fBackupMaker.MakeBackup(lBackupTask, "-manual", ref lErrorMessage), lErrorMessage);
lBackupTask.SetLastBackupStatus(fBackupMaker.MakeBackup(lBackupTask, BackupType.btManual, ref lErrorMessage), lErrorMessage);

// we made backup of currently selected item, refresh GUI to refresh last backup time
SelectTaskByIndex(-1, false); // reselect same item, refresh interface
Expand Down
4 changes: 2 additions & 2 deletions SaveGame Backup Tool/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@
// Możesz określić wszystkie wartości lub użyć domyślnych numerów kompilacji i poprawki
// przy użyciu symbolu „*”, tak jak pokazano poniżej:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.12")]
[assembly: AssemblyFileVersion("1.0.0.12")]
[assembly: AssemblyVersion("1.0.0.13")]
[assembly: AssemblyFileVersion("1.0.0.13")]
23 changes: 12 additions & 11 deletions SaveGame Backup Tool/Restore.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ You should have received a copy of the GNU Lesser General Public License
using System.Windows.Controls;
using System.Windows.Data;
using System.IO;
using System.Windows.Media;
using System.Threading;
using System.Windows.Threading;
using System;
Expand All @@ -37,6 +36,8 @@ public partial class Restore : Window
private BackupMaker fBackupMaker = null;
private BackupTask fBackupTask = null;

private string[] fFileList;

private static Action EmptyDelegate = delegate () { };

public Restore()
Expand All @@ -47,20 +48,20 @@ public Restore()
private bool ListFiles()
{
string lErrorMessage = "";
string[] lFileList = fBackupMaker.GetBackupsList(fBackupTask, ref lErrorMessage);
fFileList = fBackupMaker.GetBackupsList(fBackupTask, ref lErrorMessage);

if (lFileList != null)
if (fFileList != null)
{
ObservableCollection<string> lList = new ObservableCollection<string>();

// convert file paths to FileNames
for (int i = 0; i < lFileList.Length; i++)
for (int i = 0; i < fFileList.Length; i++)
{
lFileList[i] = System.IO.Path.GetFileName(lFileList[i]);
lList.Add(System.IO.Path.GetFileName(fFileList[i]));
}

ObservableCollection<string> lList = new ObservableCollection<string>(lFileList);
listBoxBackupFiles.DataContext = lList;


Binding lBinding = new Binding();
listBoxBackupFiles.DataContext = lList;
listBoxBackupFiles.SetBinding(ListBox.ItemsSourceProperty, lBinding);
listBoxBackupFiles.ScrollIntoView(listBoxBackupFiles.Items[listBoxBackupFiles.Items.Count - 1]);

Expand Down Expand Up @@ -90,7 +91,7 @@ private void buttonClose_Click(object sender, RoutedEventArgs e)
private bool BackupBeforeRestore()
{
string lErrorMessage = "";
bool lResult = fBackupMaker.MakeBackup(fBackupTask, "-pre_restore", ref lErrorMessage);
bool lResult = fBackupMaker.MakeBackup(fBackupTask, BackupType.btRestore, ref lErrorMessage);
fBackupTask.SetLastBackupStatus(lResult, lErrorMessage);
return lResult;
}
Expand Down Expand Up @@ -121,7 +122,7 @@ private void ButtonRestore_Click(object sender, RoutedEventArgs e)
labelStatus.Dispatcher.Invoke(DispatcherPriority.Render, EmptyDelegate);
Thread.Sleep(1);

if (fBackupMaker.RestoreBackup(fBackupTask, Path.GetFullPath(Path.Combine(fBackupTask.Settings.DestinationPathHelper.DirectoryPath, listBoxBackupFiles.SelectedItem.ToString()))))
if (fBackupMaker.RestoreBackup(fBackupTask, fFileList[listBoxBackupFiles.SelectedIndex]))
{
labelStatus.Content = "Status: restored";
}
Expand Down

0 comments on commit c6ee1ce

Please sign in to comment.