2022-03-15 18:00:53 +00:00
|
|
|
// (c) Copyright Ascensio System SIA 2010-2022
|
|
|
|
//
|
|
|
|
// This program is a free software product.
|
|
|
|
// You can redistribute it and/or modify it under the terms
|
|
|
|
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
|
|
|
|
// Foundation. In accordance with Section 7(a) of the GNU AGPL 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 details, see
|
|
|
|
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
//
|
|
|
|
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
|
|
|
|
//
|
|
|
|
// The interactive user interfaces in modified source and object code versions of the Program must
|
|
|
|
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
|
|
|
|
//
|
|
|
|
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
|
|
|
|
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
|
|
|
|
// trademark law for use of our trademarks.
|
|
|
|
//
|
|
|
|
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
|
|
|
|
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
|
|
|
|
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
2020-05-20 15:14:44 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
namespace ASC.Data.Backup.Services;
|
|
|
|
|
|
|
|
[Scope]
|
|
|
|
public class BackupService : IBackupService
|
2020-10-14 14:26:18 +00:00
|
|
|
{
|
2022-05-26 09:01:54 +00:00
|
|
|
private readonly ILogger<BackupService> _logger;
|
2022-02-09 18:49:52 +00:00
|
|
|
private readonly BackupStorageFactory _backupStorageFactory;
|
|
|
|
private readonly BackupWorker _backupWorker;
|
|
|
|
private readonly BackupRepository _backupRepository;
|
|
|
|
private readonly ConfigurationExtension _configuration;
|
|
|
|
|
|
|
|
public BackupService(
|
2022-04-26 17:53:48 +00:00
|
|
|
ILogger<BackupService> logger,
|
2022-02-09 18:49:52 +00:00
|
|
|
BackupStorageFactory backupStorageFactory,
|
|
|
|
BackupWorker backupWorker,
|
|
|
|
BackupRepository backupRepository,
|
|
|
|
ConfigurationExtension configuration)
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-03-17 10:13:52 +00:00
|
|
|
_logger = logger;
|
2022-02-09 18:49:52 +00:00
|
|
|
_backupStorageFactory = backupStorageFactory;
|
|
|
|
_backupWorker = backupWorker;
|
|
|
|
_backupRepository = backupRepository;
|
|
|
|
_configuration = configuration;
|
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public void StartBackup(StartBackupRequest request)
|
|
|
|
{
|
|
|
|
var progress = _backupWorker.StartBackup(request);
|
|
|
|
if (!string.IsNullOrEmpty(progress.Error))
|
2021-05-13 08:57:40 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
throw new FaultException();
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public void DeleteBackup(Guid id)
|
|
|
|
{
|
|
|
|
var backupRecord = _backupRepository.GetBackupRecord(id);
|
|
|
|
_backupRepository.DeleteBackupRecord(backupRecord.Id);
|
2020-05-20 15:14:44 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
var storage = _backupStorageFactory.GetBackupStorage(backupRecord);
|
|
|
|
if (storage == null)
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
return;
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
storage.Delete(backupRecord.StoragePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void DeleteAllBackups(int tenantId)
|
|
|
|
{
|
|
|
|
foreach (var backupRecord in _backupRepository.GetBackupRecordsByTenantId(tenantId))
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
try
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
_backupRepository.DeleteBackupRecord(backupRecord.Id);
|
|
|
|
var storage = _backupStorageFactory.GetBackupStorage(backupRecord);
|
2022-02-09 16:46:09 +00:00
|
|
|
if (storage == null)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
storage.Delete(backupRecord.StoragePath);
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
catch (Exception error)
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-05-06 15:49:06 +00:00
|
|
|
_logger.WarningErrorWhileBackupRecord(error);
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public List<BackupHistoryRecord> GetBackupHistory(int tenantId)
|
|
|
|
{
|
|
|
|
var backupHistory = new List<BackupHistoryRecord>();
|
|
|
|
foreach (var record in _backupRepository.GetBackupRecordsByTenantId(tenantId))
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
var storage = _backupStorageFactory.GetBackupStorage(record);
|
|
|
|
if (storage == null)
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
continue;
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
if (storage.IsExists(record.StoragePath))
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
backupHistory.Add(new BackupHistoryRecord
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
Id = record.Id,
|
|
|
|
FileName = record.Name,
|
|
|
|
StorageType = record.StorageType,
|
|
|
|
CreatedOn = record.CreatedOn,
|
|
|
|
ExpiresOn = record.ExpiresOn
|
|
|
|
});
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
else
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
_backupRepository.DeleteBackupRecord(record.Id);
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
2021-05-13 08:57:40 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
return backupHistory;
|
|
|
|
}
|
2021-05-13 08:57:40 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public void StartTransfer(StartTransferRequest request)
|
|
|
|
{
|
|
|
|
var progress = _backupWorker.StartTransfer(request.TenantId, request.TargetRegion, request.BackupMail, request.NotifyUsers);
|
|
|
|
if (!string.IsNullOrEmpty(progress.Error))
|
2021-05-13 08:57:40 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
throw new FaultException();
|
2021-05-13 08:57:40 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
2020-06-17 07:49:17 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public void StartRestore(StartRestoreRequest request)
|
|
|
|
{
|
|
|
|
if (request.StorageType == BackupStorageType.Local)
|
2021-05-13 08:57:40 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
if (string.IsNullOrEmpty(request.FilePathOrId) || !File.Exists(request.FilePathOrId))
|
|
|
|
{
|
|
|
|
throw new FileNotFoundException();
|
|
|
|
}
|
2021-05-13 08:57:40 +00:00
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
if (!request.BackupId.Equals(Guid.Empty))
|
2021-05-13 08:57:40 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
var backupRecord = _backupRepository.GetBackupRecord(request.BackupId);
|
|
|
|
if (backupRecord == null)
|
|
|
|
{
|
|
|
|
throw new FileNotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
request.FilePathOrId = backupRecord.StoragePath;
|
|
|
|
request.StorageType = backupRecord.StorageType;
|
|
|
|
request.StorageParams = JsonConvert.DeserializeObject<Dictionary<string, string>>(backupRecord.StorageParams);
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
var progress = _backupWorker.StartRestore(request);
|
|
|
|
if (!string.IsNullOrEmpty(progress.Error))
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
throw new FaultException();
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public BackupProgress GetBackupProgress(int tenantId)
|
|
|
|
{
|
|
|
|
return _backupWorker.GetBackupProgress(tenantId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public BackupProgress GetTransferProgress(int tenantId)
|
|
|
|
{
|
|
|
|
return _backupWorker.GetTransferProgress(tenantId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public BackupProgress GetRestoreProgress(int tenantId)
|
|
|
|
{
|
|
|
|
return _backupWorker.GetRestoreProgress(tenantId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public string GetTmpFolder()
|
|
|
|
{
|
|
|
|
return _backupWorker.TempFolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<TransferRegion> GetTransferRegions()
|
|
|
|
{
|
|
|
|
var settings = _configuration.GetSetting<BackupSettings>("backup");
|
2020-05-20 15:14:44 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
return settings.WebConfigs.Elements.Select(configElement =>
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
var config = Utils.ConfigurationProvider.Open(PathHelper.ToRootedConfigPath(configElement.Path));
|
|
|
|
var baseDomain = config.AppSettings.Settings["core:base-domain"].Value;
|
2022-02-09 18:33:50 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
return new TransferRegion
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
Name = configElement.Region,
|
|
|
|
BaseDomain = baseDomain,
|
|
|
|
IsCurrentRegion = configElement.Region.Equals(settings.WebConfigs.CurrentRegion, StringComparison.InvariantCultureIgnoreCase)
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.ToList();
|
|
|
|
}
|
2022-02-09 18:33:50 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public void CreateSchedule(CreateScheduleRequest request)
|
|
|
|
{
|
|
|
|
_backupRepository.SaveBackupSchedule(
|
|
|
|
new BackupSchedule()
|
|
|
|
{
|
|
|
|
TenantId = request.TenantId,
|
|
|
|
Cron = request.Cron,
|
|
|
|
BackupMail = request.BackupMail,
|
|
|
|
BackupsStored = request.NumberOfBackupsStored,
|
|
|
|
StorageType = request.StorageType,
|
|
|
|
StorageBasePath = request.StorageBasePath,
|
|
|
|
StorageParams = JsonConvert.SerializeObject(request.StorageParams)
|
|
|
|
});
|
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public void DeleteSchedule(int tenantId)
|
|
|
|
{
|
|
|
|
_backupRepository.DeleteBackupSchedule(tenantId);
|
|
|
|
}
|
2020-05-20 15:14:44 +00:00
|
|
|
|
2022-02-09 18:49:52 +00:00
|
|
|
public ScheduleResponse GetSchedule(int tenantId)
|
|
|
|
{
|
|
|
|
var schedule = _backupRepository.GetBackupSchedule(tenantId);
|
|
|
|
if (schedule != null)
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
var tmp = new ScheduleResponse
|
|
|
|
{
|
|
|
|
StorageType = schedule.StorageType,
|
|
|
|
StorageBasePath = schedule.StorageBasePath,
|
|
|
|
BackupMail = schedule.BackupMail,
|
|
|
|
NumberOfBackupsStored = schedule.BackupsStored,
|
|
|
|
Cron = schedule.Cron,
|
|
|
|
LastBackupTime = schedule.LastBackupTime,
|
|
|
|
StorageParams = JsonConvert.DeserializeObject<Dictionary<string, string>>(schedule.StorageParams)
|
|
|
|
};
|
|
|
|
|
|
|
|
return tmp;
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
2022-02-09 18:49:52 +00:00
|
|
|
else
|
2020-05-20 15:14:44 +00:00
|
|
|
{
|
2022-02-09 18:49:52 +00:00
|
|
|
return null;
|
2020-05-20 15:14:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|