2021-08-31 09:40:28 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* (c) Copyright Ascensio System Limited 2010-2020
|
|
|
|
*
|
|
|
|
* This program is freeware. You can redistribute it and/or modify it under the terms of the GNU
|
|
|
|
* General Public License (GPL) version 3 as published by the Free Software Foundation (https://www.gnu.org/copyleft/gpl.html).
|
|
|
|
* In accordance with Section 7(a) of the GNU GPL its Section 15 shall be amended to the effect that
|
|
|
|
* Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
|
|
|
|
*
|
|
|
|
* THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. For more details, see GNU GPL at https://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* You can contact Ascensio System SIA by email at sales@onlyoffice.com
|
|
|
|
*
|
|
|
|
* The interactive user interfaces in modified source and object code versions of ONLYOFFICE must display
|
|
|
|
* Appropriate Legal Notices, as required under Section 5 of the GNU GPL version 3.
|
|
|
|
*
|
|
|
|
* Pursuant to Section 7 § 3(b) of the GNU GPL you must retain the original ONLYOFFICE logo which contains
|
|
|
|
* relevant author attributions when distributing the software. If the display of the logo in its graphic
|
|
|
|
* form is not reasonably feasible for technical reasons, you must include the words "Powered by ONLYOFFICE"
|
|
|
|
* in every copy of the program you distribute.
|
|
|
|
* Pursuant to Section 7 § 3(e) we decline to grant you any rights under trademark law for use of our trademarks.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
namespace ASC.Data.Backup.Services;
|
|
|
|
|
|
|
|
[Singletone(Additional = typeof(BackupWorkerExtension))]
|
|
|
|
public class BackupWorker
|
|
|
|
{
|
|
|
|
internal string TempFolder { get; set; }
|
|
|
|
|
|
|
|
private DistributedTaskQueue _progressQueue;
|
|
|
|
private string _currentRegion;
|
|
|
|
private Dictionary<string, string> _configPaths;
|
|
|
|
private int _limit;
|
|
|
|
private string _upgradesPath;
|
|
|
|
private readonly ILog _logger;
|
|
|
|
private readonly FactoryProgressItem _factoryProgressItem;
|
|
|
|
private readonly TempPath _tempPath;
|
|
|
|
private readonly object _synchRoot = new object();
|
|
|
|
|
|
|
|
public BackupWorker(
|
|
|
|
IOptionsMonitor<ILog> options,
|
|
|
|
DistributedTaskQueueOptionsManager progressQueue,
|
|
|
|
FactoryProgressItem factoryProgressItem,
|
|
|
|
TempPath tempPath)
|
|
|
|
{
|
|
|
|
_logger = options.CurrentValue;
|
|
|
|
_progressQueue = progressQueue.Get<BaseBackupProgressItem>();
|
|
|
|
_factoryProgressItem = factoryProgressItem;
|
|
|
|
_tempPath = tempPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Start(BackupSettings settings)
|
|
|
|
{
|
|
|
|
TempFolder = _tempPath.GetTempPath();
|
2022-03-02 15:25:28 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
if (!Directory.Exists(TempFolder))
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
Directory.CreateDirectory(TempFolder);
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
_limit = settings.Limit;
|
|
|
|
_upgradesPath = settings.UpgradesPath;
|
|
|
|
_currentRegion = settings.WebConfigs.CurrentRegion;
|
|
|
|
_configPaths = settings.WebConfigs.Elements.ToDictionary(el => el.Region, el => PathHelper.ToRootedConfigPath(el.Path));
|
|
|
|
_configPaths[_currentRegion] = PathHelper.ToRootedConfigPath(settings.WebConfigs.CurrentPath);
|
|
|
|
|
|
|
|
var invalidConfigPath = _configPaths.Values.FirstOrDefault(path => !File.Exists(path));
|
2022-03-02 15:25:28 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
if (invalidConfigPath != null)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
_logger.WarnFormat("Configuration file {0} not found", invalidConfigPath);
|
|
|
|
}
|
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public void Stop()
|
|
|
|
{
|
|
|
|
if (_progressQueue != null)
|
|
|
|
{
|
|
|
|
var tasks = _progressQueue.GetTasks();
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
foreach (var t in tasks)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
_progressQueue.CancelTask(t.Id);
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
|
|
|
|
_progressQueue = null;
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public BackupProgress StartBackup(StartBackupRequest request)
|
|
|
|
{
|
|
|
|
lock (_synchRoot)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
var item = _progressQueue.GetTasks<BackupProgressItem>().FirstOrDefault(t => t.TenantId == request.TenantId);
|
|
|
|
if (item != null && item.IsCompleted)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
_progressQueue.RemoveTask(item.Id);
|
|
|
|
item = null;
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
if (item == null)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
item = _factoryProgressItem.CreateBackupProgressItem(request, false, TempFolder, _limit, _currentRegion, _configPaths);
|
|
|
|
_progressQueue.QueueTask(item);
|
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
item.PublishChanges();
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
return ToBackupProgress(item);
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public void StartScheduledBackup(BackupSchedule schedule)
|
|
|
|
{
|
|
|
|
lock (_synchRoot)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
var item = _progressQueue.GetTasks<BackupProgressItem>().FirstOrDefault(t => t.TenantId == schedule.TenantId);
|
|
|
|
if (item != null && item.IsCompleted)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
_progressQueue.RemoveTask(item.Id);
|
|
|
|
item = null;
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
if (item == null)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
item = _factoryProgressItem.CreateBackupProgressItem(schedule, false, TempFolder, _limit, _currentRegion, _configPaths);
|
|
|
|
_progressQueue.QueueTask(item);
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public BackupProgress GetBackupProgress(int tenantId)
|
|
|
|
{
|
|
|
|
lock (_synchRoot)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
return ToBackupProgress(_progressQueue.GetTasks<BackupProgressItem>().FirstOrDefault(t => t.TenantId == tenantId));
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public BackupProgress GetTransferProgress(int tenantId)
|
|
|
|
{
|
|
|
|
lock (_synchRoot)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
return ToBackupProgress(_progressQueue.GetTasks<TransferProgressItem>().FirstOrDefault(t => t.TenantId == tenantId));
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public BackupProgress GetRestoreProgress(int tenantId)
|
|
|
|
{
|
|
|
|
lock (_synchRoot)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
return ToBackupProgress(_progressQueue.GetTasks<RestoreProgressItem>().FirstOrDefault(t => t.TenantId == tenantId));
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public void ResetBackupError(int tenantId)
|
|
|
|
{
|
|
|
|
lock (_synchRoot)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
var progress = _progressQueue.GetTasks<BackupProgressItem>().FirstOrDefault(t => t.TenantId == tenantId);
|
|
|
|
if (progress != null)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
progress.Exception = null;
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public void ResetRestoreError(int tenantId)
|
|
|
|
{
|
|
|
|
lock (_synchRoot)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
var progress = _progressQueue.GetTasks<RestoreProgressItem>().FirstOrDefault(t => t.TenantId == tenantId);
|
|
|
|
if (progress != null)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
progress.Exception = null;
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public BackupProgress StartRestore(StartRestoreRequest request)
|
|
|
|
{
|
|
|
|
lock (_synchRoot)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
var item = _progressQueue.GetTasks<RestoreProgressItem>().FirstOrDefault(t => t.TenantId == request.TenantId);
|
|
|
|
if (item != null && item.IsCompleted)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
_progressQueue.RemoveTask(item.Id);
|
|
|
|
item = null;
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
if (item == null)
|
2022-02-09 18:33:50 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
item = _factoryProgressItem.CreateRestoreProgressItem(request, TempFolder, _upgradesPath, _currentRegion, _configPaths);
|
|
|
|
_progressQueue.QueueTask(item);
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
return ToBackupProgress(item);
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public BackupProgress StartTransfer(int tenantId, string targetRegion, bool transferMail, bool notify)
|
|
|
|
{
|
|
|
|
lock (_synchRoot)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
var item = _progressQueue.GetTasks<TransferProgressItem>().FirstOrDefault(t => t.TenantId == tenantId);
|
|
|
|
if (item != null && item.IsCompleted)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
_progressQueue.RemoveTask(item.Id);
|
|
|
|
item = null;
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
if (item == null)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
item = _factoryProgressItem.CreateTransferProgressItem(targetRegion, transferMail, tenantId, TempFolder, _limit, notify, _currentRegion, _configPaths);
|
|
|
|
_progressQueue.QueueTask(item);
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
return ToBackupProgress(item);
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
internal static string GetBackupHash(string path)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
using (var sha256 = SHA256.Create())
|
|
|
|
using (var fileStream = File.OpenRead(path))
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
fileStream.Position = 0;
|
|
|
|
var hash = sha256.ComputeHash(fileStream);
|
|
|
|
return BitConverter.ToString(hash).Replace("-", string.Empty);
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
private BackupProgress ToBackupProgress(BaseBackupProgressItem progressItem)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
if (progressItem == null)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var progress = new BackupProgress
|
|
|
|
{
|
|
|
|
IsCompleted = progressItem.IsCompleted,
|
|
|
|
Progress = (int)progressItem.Percentage,
|
|
|
|
Error = progressItem.Exception != null ? progressItem.Exception.Message : "",
|
|
|
|
TenantId = progressItem.TenantId,
|
|
|
|
BackupProgressEnum = progressItem.BackupProgressItemEnum.Convert()
|
|
|
|
};
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
if (progressItem is BackupProgressItem backupProgressItem && backupProgressItem.Link != null)
|
|
|
|
{
|
|
|
|
progress.Link = backupProgressItem.Link;
|
|
|
|
}
|
|
|
|
else
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
if (progressItem is TransferProgressItem transferProgressItem && transferProgressItem.Link != null)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
progress.Link = transferProgressItem.Link;
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
return progress;
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 15:25:28 +00:00
|
|
|
public bool HaveBackupRestoreRequestWaitingTasks()
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-03-02 15:25:28 +00:00
|
|
|
var countActiveTask = _progressQueue.GetTasks<RestoreProgressItem>().Where(x => !x.IsCompleted).Count();
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-03-02 15:25:28 +00:00
|
|
|
if (_progressQueue.MaxThreadsCount >= countActiveTask)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-03-02 15:25:28 +00:00
|
|
|
return false;
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-03-02 15:25:28 +00:00
|
|
|
return true;
|
2022-02-09 18:49:52 +00:00
|
|
|
|
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-03-02 15:25:28 +00:00
|
|
|
public bool HaveBackupTransferRequestWaitingTasks()
|
2022-02-09 18:49:52 +00:00
|
|
|
{
|
2022-03-03 13:07:28 +00:00
|
|
|
if (_progressQueue.MaxThreadsCount == -1) return false;
|
|
|
|
|
2022-03-02 15:25:28 +00:00
|
|
|
var countActiveTask = _progressQueue.GetTasks<TransferProgressItem>().Where(x => !x.IsCompleted).Count();
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-03-02 15:25:28 +00:00
|
|
|
if (_progressQueue.MaxThreadsCount >= countActiveTask)
|
2022-02-09 18:49:52 +00:00
|
|
|
{
|
2022-03-02 15:25:28 +00:00
|
|
|
return false;
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-03-02 15:25:28 +00:00
|
|
|
return true;
|
2021-08-31 09:40:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-02 15:25:28 +00:00
|
|
|
public bool HaveBackupRequestWaitingTasks()
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-03-03 13:07:28 +00:00
|
|
|
if (_progressQueue.MaxThreadsCount == -1) return false;
|
|
|
|
|
2022-03-02 15:25:28 +00:00
|
|
|
var countActiveTask = _progressQueue.GetTasks<BackupProgressItem>().Where(x => !x.IsCompleted).Count();
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-03-02 15:25:28 +00:00
|
|
|
if (_progressQueue.MaxThreadsCount >= countActiveTask)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-03-02 15:25:28 +00:00
|
|
|
return false;
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 15:25:28 +00:00
|
|
|
return true;
|
2021-08-31 09:40:28 +00:00
|
|
|
|
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-02-15 16:56:43 +00:00
|
|
|
public static class BackupWorkerExtension
|
2022-02-09 18:49:52 +00:00
|
|
|
{
|
|
|
|
public static void Register(DIHelper services)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
services.AddDistributedTaskQueueService<BackupProgressItem>(5);
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|