Improvements to backup system which allows creating weekly backups right after saving

This commit is contained in:
Jaex 2018-08-03 13:40:00 +03:00
parent d4a1a69ee4
commit 3d56ae2f62
5 changed files with 88 additions and 60 deletions

View file

@ -885,37 +885,51 @@ public static void CreateDirectoryFromFilePath(string path)
} }
} }
public static void BackupFileMonthly(string filepath, string destinationFolder) public static void CopyFile(string filePath, string destinationFolder, bool overwrite = true)
{ {
if (!string.IsNullOrEmpty(filepath) && File.Exists(filepath)) if (!string.IsNullOrEmpty(filePath) && !string.IsNullOrEmpty(destinationFolder))
{ {
string filename = Path.GetFileNameWithoutExtension(filepath); string fileName = Path.GetFileName(filePath);
string extension = Path.GetExtension(filepath); string destinationFilePath = Path.Combine(destinationFolder, fileName);
string newFilename = string.Format("{0}-{1:yyyy-MM}{2}", filename, DateTime.Now, extension); CreateDirectoryFromDirectoryPath(destinationFolder);
string newFilepath = Path.Combine(destinationFolder, newFilename); File.Copy(filePath, destinationFilePath, overwrite);
if (!File.Exists(newFilepath))
{
CreateDirectoryFromFilePath(newFilepath);
File.Copy(filepath, newFilepath, false);
}
} }
} }
public static void BackupFileWeekly(string filepath, string destinationFolder) public static string BackupFileWeekly(string filePath, string destinationFolder)
{ {
if (!string.IsNullOrEmpty(filepath) && File.Exists(filepath)) if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
{ {
string filename = Path.GetFileNameWithoutExtension(filepath); string fileName = Path.GetFileNameWithoutExtension(filePath);
DateTime dateTime = DateTime.Now; DateTime dateTime = DateTime.Now;
string extension = Path.GetExtension(filepath); string extension = Path.GetExtension(filePath);
string newFilename = string.Format("{0}-{1:yyyy-MM}-W{2:00}{3}", filename, dateTime, dateTime.WeekOfYear(), extension); string newFileName = string.Format("{0}-{1:yyyy-MM}-W{2:00}{3}", fileName, dateTime, dateTime.WeekOfYear(), extension);
string newFilepath = Path.Combine(destinationFolder, newFilename); string newFilePath = Path.Combine(destinationFolder, newFileName);
if (!File.Exists(newFilepath)) if (!File.Exists(newFilePath))
{ {
CreateDirectoryFromFilePath(newFilepath); CreateDirectoryFromDirectoryPath(destinationFolder);
File.Copy(filepath, newFilepath, false); File.Copy(filePath, newFilePath, false);
return newFilePath;
}
}
return null;
}
public static void BackupFileMonthly(string filePath, string destinationFolder)
{
if (!string.IsNullOrEmpty(filePath) && File.Exists(filePath))
{
string fileName = Path.GetFileNameWithoutExtension(filePath);
string extension = Path.GetExtension(filePath);
string newFileName = string.Format("{0}-{1:yyyy-MM}{2}", fileName, DateTime.Now, extension);
string newFilePath = Path.Combine(destinationFolder, newFileName);
if (!File.Exists(newFilePath))
{
CreateDirectoryFromDirectoryPath(destinationFolder);
File.Copy(filePath, newFilePath, false);
} }
} }
} }

View file

@ -37,18 +37,27 @@ namespace ShareX.HelpersLib
public delegate void SettingsSavedEventHandler(T settings, string filePath, bool result); public delegate void SettingsSavedEventHandler(T settings, string filePath, bool result);
public event SettingsSavedEventHandler SettingsSaved; public event SettingsSavedEventHandler SettingsSaved;
[Browsable(false)] [Browsable(false), JsonIgnore]
public string FilePath { get; private set; } public string FilePath { get; private set; }
[Browsable(false)] [Browsable(false)]
public string ApplicationVersion { get; set; } public string ApplicationVersion { get; set; }
[Browsable(false)] [Browsable(false), JsonIgnore]
public bool IsFirstTimeRun { get; private set; } public bool IsFirstTimeRun { get; private set; }
[Browsable(false)] [Browsable(false), JsonIgnore]
public bool IsUpgrade { get; private set; } public bool IsUpgrade { get; private set; }
[Browsable(false), JsonIgnore]
public string BackupFolder { get; set; }
[Browsable(false), JsonIgnore]
public bool CreateBackup { get; set; }
[Browsable(false), JsonIgnore]
public bool CreateWeeklyBackup { get; set; }
public bool IsUpgradeFrom(string version) public bool IsUpgradeFrom(string version)
{ {
return IsUpgrade && Helpers.CompareVersion(ApplicationVersion, version) <= 0; return IsUpgrade && Helpers.CompareVersion(ApplicationVersion, version) <= 0;
@ -67,7 +76,7 @@ public bool Save(string filePath)
FilePath = filePath; FilePath = filePath;
ApplicationVersion = Application.ProductVersion; ApplicationVersion = Application.ProductVersion;
bool result = SaveInternal(this, FilePath, true); bool result = SaveInternal(FilePath);
OnSettingsSaved(FilePath, result); OnSettingsSaved(FilePath, result);
@ -89,23 +98,26 @@ public void SaveAsync()
SaveAsync(FilePath); SaveAsync(FilePath);
} }
public static T Load(string filePath) public static T Load(string filePath, string backupFolder = null, bool createBackup = false, bool createWeeklyBackup = false)
{ {
T setting = LoadInternal(filePath, true); T setting = LoadInternal(filePath, backupFolder);
if (setting != null) if (setting != null)
{ {
setting.FilePath = filePath; setting.FilePath = filePath;
setting.IsFirstTimeRun = string.IsNullOrEmpty(setting.ApplicationVersion); setting.IsFirstTimeRun = string.IsNullOrEmpty(setting.ApplicationVersion);
setting.IsUpgrade = !setting.IsFirstTimeRun && Helpers.CompareApplicationVersion(setting.ApplicationVersion) < 0; setting.IsUpgrade = !setting.IsFirstTimeRun && Helpers.CompareApplicationVersion(setting.ApplicationVersion) < 0;
setting.BackupFolder = backupFolder;
setting.CreateBackup = createBackup;
setting.CreateWeeklyBackup = createWeeklyBackup;
} }
return setting; return setting;
} }
private static bool SaveInternal(object obj, string filePath, bool createBackup) private bool SaveInternal(string filePath)
{ {
string typeName = obj.GetType().Name; string typeName = GetType().Name;
DebugHelper.WriteLine("{0} save started: {1}", typeName, filePath); DebugHelper.WriteLine("{0} save started: {1}", typeName, filePath);
bool isSuccess = false; bool isSuccess = false;
@ -114,7 +126,7 @@ private static bool SaveInternal(object obj, string filePath, bool createBackup)
{ {
if (!string.IsNullOrEmpty(filePath)) if (!string.IsNullOrEmpty(filePath))
{ {
lock (obj) lock (this)
{ {
Helpers.CreateDirectoryFromFilePath(filePath); Helpers.CreateDirectoryFromFilePath(filePath);
@ -130,15 +142,15 @@ private static bool SaveInternal(object obj, string filePath, bool createBackup)
JsonSerializer serializer = new JsonSerializer(); JsonSerializer serializer = new JsonSerializer();
serializer.ContractResolver = new WritablePropertiesOnlyResolver(); serializer.ContractResolver = new WritablePropertiesOnlyResolver();
serializer.Converters.Add(new StringEnumConverter()); serializer.Converters.Add(new StringEnumConverter());
serializer.Serialize(jsonWriter, obj); serializer.Serialize(jsonWriter, this);
jsonWriter.Flush(); jsonWriter.Flush();
} }
if (File.Exists(filePath)) if (File.Exists(filePath))
{ {
if (createBackup) if (CreateBackup)
{ {
File.Copy(filePath, filePath + ".bak", true); Helpers.CopyFile(filePath, BackupFolder);
} }
File.Delete(filePath); File.Delete(filePath);
@ -146,6 +158,11 @@ private static bool SaveInternal(object obj, string filePath, bool createBackup)
File.Move(tempFilePath, filePath); File.Move(tempFilePath, filePath);
if (CreateWeeklyBackup && !string.IsNullOrEmpty(BackupFolder))
{
Helpers.BackupFileWeekly(filePath, BackupFolder);
}
isSuccess = true; isSuccess = true;
} }
} }
@ -162,7 +179,7 @@ private static bool SaveInternal(object obj, string filePath, bool createBackup)
return isSuccess; return isSuccess;
} }
private static T LoadInternal(string filePath, bool checkBackup) private static T LoadInternal(string filePath, string backupFolder = null)
{ {
string typeName = typeof(T).Name; string typeName = typeof(T).Name;
@ -209,9 +226,11 @@ private static T LoadInternal(string filePath, bool checkBackup)
DebugHelper.WriteException(e, typeName + " load failed: " + filePath); DebugHelper.WriteException(e, typeName + " load failed: " + filePath);
} }
if (checkBackup) if (!string.IsNullOrEmpty(backupFolder))
{ {
return LoadInternal(filePath + ".bak", false); string fileName = Path.GetFileName(filePath);
string backupFilePath = Path.Combine(backupFolder, fileName);
return LoadInternal(backupFilePath);
} }
} }

View file

@ -223,13 +223,13 @@
<value>13, 24</value> <value>13, 24</value>
</data> </data>
<data name="lblFilenameFilter.Size" type="System.Drawing.Size, System.Drawing"> <data name="lblFilenameFilter.Size" type="System.Drawing.Size, System.Drawing">
<value>74, 13</value> <value>52, 13</value>
</data> </data>
<data name="lblFilenameFilter.TabIndex" type="System.Int32, mscorlib"> <data name="lblFilenameFilter.TabIndex" type="System.Int32, mscorlib">
<value>17</value> <value>17</value>
</data> </data>
<data name="lblFilenameFilter.Text" xml:space="preserve"> <data name="lblFilenameFilter.Text" xml:space="preserve">
<value>Filename filter:</value> <value>Filename:</value>
</data> </data>
<data name="&gt;&gt;lblFilenameFilter.Name" xml:space="preserve"> <data name="&gt;&gt;lblFilenameFilter.Name" xml:space="preserve">
<value>lblFilenameFilter</value> <value>lblFilenameFilter</value>
@ -349,13 +349,13 @@
<value>16, 179</value> <value>16, 179</value>
</data> </data>
<data name="cbHostFilter.Size" type="System.Drawing.Size, System.Drawing"> <data name="cbHostFilter.Size" type="System.Drawing.Size, System.Drawing">
<value>73, 17</value> <value>51, 17</value>
</data> </data>
<data name="cbHostFilter.TabIndex" type="System.Int32, mscorlib"> <data name="cbHostFilter.TabIndex" type="System.Int32, mscorlib">
<value>12</value> <value>12</value>
</data> </data>
<data name="cbHostFilter.Text" xml:space="preserve"> <data name="cbHostFilter.Text" xml:space="preserve">
<value>Host filter:</value> <value>Host:</value>
</data> </data>
<data name="&gt;&gt;cbHostFilter.Name" xml:space="preserve"> <data name="&gt;&gt;cbHostFilter.Name" xml:space="preserve">
<value>cbHostFilter</value> <value>cbHostFilter</value>
@ -379,13 +379,13 @@
<value>16, 152</value> <value>16, 152</value>
</data> </data>
<data name="cbTypeFilter.Size" type="System.Drawing.Size, System.Drawing"> <data name="cbTypeFilter.Size" type="System.Drawing.Size, System.Drawing">
<value>75, 17</value> <value>53, 17</value>
</data> </data>
<data name="cbTypeFilter.TabIndex" type="System.Int32, mscorlib"> <data name="cbTypeFilter.TabIndex" type="System.Int32, mscorlib">
<value>10</value> <value>10</value>
</data> </data>
<data name="cbTypeFilter.Text" xml:space="preserve"> <data name="cbTypeFilter.Text" xml:space="preserve">
<value>Type filter:</value> <value>Type:</value>
</data> </data>
<data name="&gt;&gt;cbTypeFilter.Name" xml:space="preserve"> <data name="&gt;&gt;cbTypeFilter.Name" xml:space="preserve">
<value>cbTypeFilter</value> <value>cbTypeFilter</value>
@ -490,13 +490,13 @@
<value>16, 72</value> <value>16, 72</value>
</data> </data>
<data name="cbDateFilter.Size" type="System.Drawing.Size, System.Drawing"> <data name="cbDateFilter.Size" type="System.Drawing.Size, System.Drawing">
<value>74, 17</value> <value>52, 17</value>
</data> </data>
<data name="cbDateFilter.TabIndex" type="System.Int32, mscorlib"> <data name="cbDateFilter.TabIndex" type="System.Int32, mscorlib">
<value>0</value> <value>0</value>
</data> </data>
<data name="cbDateFilter.Text" xml:space="preserve"> <data name="cbDateFilter.Text" xml:space="preserve">
<value>Date filter:</value> <value>Date:</value>
</data> </data>
<data name="&gt;&gt;cbDateFilter.Name" xml:space="preserve"> <data name="&gt;&gt;cbDateFilter.Name" xml:space="preserve">
<value>cbDateFilter</value> <value>cbDateFilter</value>

View file

@ -300,7 +300,6 @@ private static void Run()
if (WatchFolderManager != null) WatchFolderManager.Dispose(); if (WatchFolderManager != null) WatchFolderManager.Dispose();
SettingManager.SaveAllSettings(); SettingManager.SaveAllSettings();
SettingManager.BackupSettings();
DebugHelper.Logger.AsyncWrite = false; DebugHelper.Logger.AsyncWrite = false;
DebugHelper.WriteLine("ShareX closing."); DebugHelper.WriteLine("ShareX closing.");

View file

@ -141,18 +141,18 @@ public static void WaitHotkeysConfig()
public static void LoadApplicationConfig() public static void LoadApplicationConfig()
{ {
Settings = ApplicationConfig.Load(ApplicationConfigFilePath); Settings = ApplicationConfig.Load(ApplicationConfigFilePath, BackupFolder, true, true);
DefaultTaskSettings = Settings.DefaultTaskSettings; DefaultTaskSettings = Settings.DefaultTaskSettings;
} }
public static void LoadUploadersConfig() public static void LoadUploadersConfig()
{ {
UploadersConfig = UploadersConfig.Load(UploadersConfigFilePath); UploadersConfig = UploadersConfig.Load(UploadersConfigFilePath, BackupFolder, true, true);
} }
public static void LoadHotkeysConfig() public static void LoadHotkeysConfig()
{ {
HotkeysConfig = HotkeysConfig.Load(HotkeysConfigFilePath); HotkeysConfig = HotkeysConfig.Load(HotkeysConfigFilePath, BackupFolder, true, true);
} }
public static void LoadAllSettings() public static void LoadAllSettings()
@ -242,20 +242,16 @@ public static void SaveAllSettingsAsync()
SaveHotkeysConfigAsync(); SaveHotkeysConfigAsync();
} }
public static void BackupSettings()
{
Helpers.BackupFileWeekly(ApplicationConfigFilePath, BackupFolder);
Helpers.BackupFileWeekly(UploadersConfigFilePath, BackupFolder);
Helpers.BackupFileWeekly(HotkeysConfigFilePath, BackupFolder);
Helpers.BackupFileWeekly(Program.HistoryFilePath, BackupFolder);
}
public static void ResetSettings() public static void ResetSettings()
{ {
Settings = new ApplicationConfig(); if (File.Exists(ApplicationConfigFilePath)) File.Delete(ApplicationConfigFilePath);
DefaultTaskSettings = Settings.DefaultTaskSettings; LoadApplicationConfig();
UploadersConfig = new UploadersConfig();
HotkeysConfig = new HotkeysConfig(); if (File.Exists(UploadersConfigFilePath)) File.Delete(UploadersConfigFilePath);
LoadUploadersConfig();
if (File.Exists(HotkeysConfigFilePath)) File.Delete(HotkeysConfigFilePath);
LoadHotkeysConfig();
} }
public static bool Export(string archivePath) public static bool Export(string archivePath)