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 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))
{
CreateDirectoryFromFilePath(newFilepath);
File.Copy(filepath, newFilepath, false);
}
string fileName = Path.GetFileName(filePath);
string destinationFilePath = Path.Combine(destinationFolder, fileName);
CreateDirectoryFromDirectoryPath(destinationFolder);
File.Copy(filePath, destinationFilePath, overwrite);
}
}
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;
string extension = Path.GetExtension(filepath);
string newFilename = string.Format("{0}-{1:yyyy-MM}-W{2:00}{3}", filename, dateTime, dateTime.WeekOfYear(), extension);
string newFilepath = Path.Combine(destinationFolder, newFilename);
string extension = Path.GetExtension(filePath);
string newFileName = string.Format("{0}-{1:yyyy-MM}-W{2:00}{3}", fileName, dateTime, dateTime.WeekOfYear(), extension);
string newFilePath = Path.Combine(destinationFolder, newFileName);
if (!File.Exists(newFilepath))
if (!File.Exists(newFilePath))
{
CreateDirectoryFromFilePath(newFilepath);
File.Copy(filepath, newFilepath, false);
CreateDirectoryFromDirectoryPath(destinationFolder);
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 @@ public abstract class SettingsBase<T> where T : SettingsBase<T>, new()
public delegate void SettingsSavedEventHandler(T settings, string filePath, bool result);
public event SettingsSavedEventHandler SettingsSaved;
[Browsable(false)]
[Browsable(false), JsonIgnore]
public string FilePath { get; private set; }
[Browsable(false)]
public string ApplicationVersion { get; set; }
[Browsable(false)]
[Browsable(false), JsonIgnore]
public bool IsFirstTimeRun { get; private set; }
[Browsable(false)]
[Browsable(false), JsonIgnore]
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)
{
return IsUpgrade && Helpers.CompareVersion(ApplicationVersion, version) <= 0;
@ -67,7 +76,7 @@ public bool Save(string filePath)
FilePath = filePath;
ApplicationVersion = Application.ProductVersion;
bool result = SaveInternal(this, FilePath, true);
bool result = SaveInternal(FilePath);
OnSettingsSaved(FilePath, result);
@ -89,23 +98,26 @@ public void SaveAsync()
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)
{
setting.FilePath = filePath;
setting.IsFirstTimeRun = string.IsNullOrEmpty(setting.ApplicationVersion);
setting.IsUpgrade = !setting.IsFirstTimeRun && Helpers.CompareApplicationVersion(setting.ApplicationVersion) < 0;
setting.BackupFolder = backupFolder;
setting.CreateBackup = createBackup;
setting.CreateWeeklyBackup = createWeeklyBackup;
}
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);
bool isSuccess = false;
@ -114,7 +126,7 @@ private static bool SaveInternal(object obj, string filePath, bool createBackup)
{
if (!string.IsNullOrEmpty(filePath))
{
lock (obj)
lock (this)
{
Helpers.CreateDirectoryFromFilePath(filePath);
@ -130,15 +142,15 @@ private static bool SaveInternal(object obj, string filePath, bool createBackup)
JsonSerializer serializer = new JsonSerializer();
serializer.ContractResolver = new WritablePropertiesOnlyResolver();
serializer.Converters.Add(new StringEnumConverter());
serializer.Serialize(jsonWriter, obj);
serializer.Serialize(jsonWriter, this);
jsonWriter.Flush();
}
if (File.Exists(filePath))
{
if (createBackup)
if (CreateBackup)
{
File.Copy(filePath, filePath + ".bak", true);
Helpers.CopyFile(filePath, BackupFolder);
}
File.Delete(filePath);
@ -146,6 +158,11 @@ private static bool SaveInternal(object obj, string filePath, bool createBackup)
File.Move(tempFilePath, filePath);
if (CreateWeeklyBackup && !string.IsNullOrEmpty(BackupFolder))
{
Helpers.BackupFileWeekly(filePath, BackupFolder);
}
isSuccess = true;
}
}
@ -162,7 +179,7 @@ private static bool SaveInternal(object obj, string filePath, bool createBackup)
return isSuccess;
}
private static T LoadInternal(string filePath, bool checkBackup)
private static T LoadInternal(string filePath, string backupFolder = null)
{
string typeName = typeof(T).Name;
@ -209,9 +226,11 @@ private static T LoadInternal(string filePath, bool checkBackup)
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>
</data>
<data name="lblFilenameFilter.Size" type="System.Drawing.Size, System.Drawing">
<value>74, 13</value>
<value>52, 13</value>
</data>
<data name="lblFilenameFilter.TabIndex" type="System.Int32, mscorlib">
<value>17</value>
</data>
<data name="lblFilenameFilter.Text" xml:space="preserve">
<value>Filename filter:</value>
<value>Filename:</value>
</data>
<data name="&gt;&gt;lblFilenameFilter.Name" xml:space="preserve">
<value>lblFilenameFilter</value>
@ -349,13 +349,13 @@
<value>16, 179</value>
</data>
<data name="cbHostFilter.Size" type="System.Drawing.Size, System.Drawing">
<value>73, 17</value>
<value>51, 17</value>
</data>
<data name="cbHostFilter.TabIndex" type="System.Int32, mscorlib">
<value>12</value>
</data>
<data name="cbHostFilter.Text" xml:space="preserve">
<value>Host filter:</value>
<value>Host:</value>
</data>
<data name="&gt;&gt;cbHostFilter.Name" xml:space="preserve">
<value>cbHostFilter</value>
@ -379,13 +379,13 @@
<value>16, 152</value>
</data>
<data name="cbTypeFilter.Size" type="System.Drawing.Size, System.Drawing">
<value>75, 17</value>
<value>53, 17</value>
</data>
<data name="cbTypeFilter.TabIndex" type="System.Int32, mscorlib">
<value>10</value>
</data>
<data name="cbTypeFilter.Text" xml:space="preserve">
<value>Type filter:</value>
<value>Type:</value>
</data>
<data name="&gt;&gt;cbTypeFilter.Name" xml:space="preserve">
<value>cbTypeFilter</value>
@ -490,13 +490,13 @@
<value>16, 72</value>
</data>
<data name="cbDateFilter.Size" type="System.Drawing.Size, System.Drawing">
<value>74, 17</value>
<value>52, 17</value>
</data>
<data name="cbDateFilter.TabIndex" type="System.Int32, mscorlib">
<value>0</value>
</data>
<data name="cbDateFilter.Text" xml:space="preserve">
<value>Date filter:</value>
<value>Date:</value>
</data>
<data name="&gt;&gt;cbDateFilter.Name" xml:space="preserve">
<value>cbDateFilter</value>

View file

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

View file

@ -141,18 +141,18 @@ public static void WaitHotkeysConfig()
public static void LoadApplicationConfig()
{
Settings = ApplicationConfig.Load(ApplicationConfigFilePath);
Settings = ApplicationConfig.Load(ApplicationConfigFilePath, BackupFolder, true, true);
DefaultTaskSettings = Settings.DefaultTaskSettings;
}
public static void LoadUploadersConfig()
{
UploadersConfig = UploadersConfig.Load(UploadersConfigFilePath);
UploadersConfig = UploadersConfig.Load(UploadersConfigFilePath, BackupFolder, true, true);
}
public static void LoadHotkeysConfig()
{
HotkeysConfig = HotkeysConfig.Load(HotkeysConfigFilePath);
HotkeysConfig = HotkeysConfig.Load(HotkeysConfigFilePath, BackupFolder, true, true);
}
public static void LoadAllSettings()
@ -242,20 +242,16 @@ public static void SaveAllSettingsAsync()
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()
{
Settings = new ApplicationConfig();
DefaultTaskSettings = Settings.DefaultTaskSettings;
UploadersConfig = new UploadersConfig();
HotkeysConfig = new HotkeysConfig();
if (File.Exists(ApplicationConfigFilePath)) File.Delete(ApplicationConfigFilePath);
LoadApplicationConfig();
if (File.Exists(UploadersConfigFilePath)) File.Delete(UploadersConfigFilePath);
LoadUploadersConfig();
if (File.Exists(HotkeysConfigFilePath)) File.Delete(HotkeysConfigFilePath);
LoadHotkeysConfig();
}
public static bool Export(string archivePath)