89 lines
4.5 KiB
C#
89 lines
4.5 KiB
C#
using Api.Framework;
|
|
using Api.Framework.SDK;
|
|
using Api.Framework.Tools;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
|
|
namespace DataDocking
|
|
{
|
|
internal class AutoBakThread : TimerTask
|
|
{
|
|
public override void Run(object state, bool timedOut)
|
|
{
|
|
try
|
|
{
|
|
#region 定时备份sqlite数据库的数据
|
|
if (Class1.Config.Auto_Bak_OnOff)
|
|
{
|
|
if (ApiClient.GetSession().CurrentConnectionConfig.DbType == SqlSugar.DbType.MySql) return;
|
|
if (string.IsNullOrWhiteSpace(Class1.Config.Auto_Bak_Path))
|
|
{
|
|
Class1.Config.Auto_Bak_Path = Util.MapPath($"File\\备份");
|
|
Util.Save(Class1.Config);
|
|
}
|
|
if (!string.IsNullOrWhiteSpace(Class1.Config.Auto_Bak_Path) && Directory.Exists(Class1.Config.Auto_Bak_Path))
|
|
{
|
|
try
|
|
{
|
|
var now = DateTime.Now;
|
|
if (Class1.Config.Lately_Bak_Time.Date <= now.Date)
|
|
{
|
|
if (DateTime.Now.AddSeconds(-3).ToString("HHmm") == Class1.Config.Auto_Bak_Time.ToString("HHmm"))
|
|
{
|
|
Class1.Config.Lately_Bak_Time = Class1.Config.Lately_Bak_Time.Date.AddDays(Class1.Config.Auto_Bak_Day);
|
|
Api.Framework.Tools.Util.Save(Class1.Config);
|
|
|
|
#region 删除很久以前的备份
|
|
DirectoryInfo root = new DirectoryInfo(Class1.Config.Auto_Bak_Path.Trim());
|
|
List<FileInfo> files = root.GetFiles().ToList().Where(f => f.Name.StartsWith("数据库")).OrderBy(f => f.CreationTime).ToList();
|
|
if (files.Count >= 7)
|
|
{
|
|
int removeNum = files.Count - 7;
|
|
for (int i = 0; i < removeNum; i++)
|
|
{
|
|
try
|
|
{
|
|
File.Delete(files[i].FullName);
|
|
}
|
|
catch (Exception)
|
|
{ }
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
var path = Path.Combine(new string[] { Class1.Config.Auto_Bak_Path.Trim(), "数据库" + DateTime.Now.ToString("yyyyMMddHHmmssfff") });
|
|
File.Copy(Util.MapFile("数据库.db", "Config"), path);
|
|
if (File.Exists(path))
|
|
{
|
|
if (Class1.Config.Is_Bak_Send_Email)
|
|
{
|
|
Zip.Process(new string[] { path });
|
|
|
|
var zipPath = path + ".zip";
|
|
if (File.Exists(zipPath))
|
|
ApiClient.SendAdminEmail(ApiClient.Setting.SystemConfig.account_admin_email, $"数据库备份{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}", "数据库备份", zipPath, true);
|
|
}
|
|
EventClient.OnEvent(this, "定时备份成功 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:dd"));
|
|
}
|
|
else
|
|
EventClient.OnEvent(this, "定时备份失败 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:dd"));
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
EventClient.OnEvent(this, "定时备份异常" + ex.Message + "-" + ex.StackTrace);
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
EventClient.OnEvent(this, ex.Message + " - " + ex.StackTrace);
|
|
}
|
|
}
|
|
}
|
|
} |