DocSpace-buildtools/common/services/ASC.Data.Backup/Service/BackupService.cs

273 lines
10 KiB
C#
Raw Normal View History

2020-05-20 15:14:44 +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.
*
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
2020-05-29 14:48:27 +00:00
using System.ServiceModel;
using ASC.Common;
2020-05-20 15:14:44 +00:00
using ASC.Common.Logging;
using ASC.Core.Common.Contracts;
using ASC.Data.Backup.EF.Model;
using ASC.Data.Backup.Storage;
2020-05-29 14:48:27 +00:00
using ASC.Data.Backup.Utils;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
2020-05-20 15:14:44 +00:00
namespace ASC.Data.Backup.Service
{
2020-05-29 14:48:27 +00:00
internal class BackupService : IBackupService
2020-05-20 15:14:44 +00:00
{
2020-05-29 14:48:27 +00:00
private ILog Log { get; set; }
private BackupStorageFactory BackupStorageFactory { get; set; }
private BackupWorker BackupWorker { get; set; }
public BackupRepository BackupRepository { get; }
public BackupService(
IOptionsMonitor<ILog> options,
BackupStorageFactory backupStorageFactory,
BackupWorker backupWorker,
BackupRepository backupRepository)
2020-05-20 15:14:44 +00:00
{
2020-05-29 14:48:27 +00:00
Log = options.CurrentValue;
BackupStorageFactory = backupStorageFactory;
BackupWorker = backupWorker;
BackupRepository = backupRepository;
2020-05-20 15:14:44 +00:00
}
public BackupProgress StartBackup(StartBackupRequest request)
{
2020-05-29 14:48:27 +00:00
var progress = BackupWorker.StartBackup(request);
2020-05-20 15:14:44 +00:00
if (!string.IsNullOrEmpty(progress.Error))
{
throw new FaultException();
}
return progress;
}
public BackupProgress GetBackupProgress(int tenantId)
{
2020-05-29 14:48:27 +00:00
var progress = BackupWorker.GetBackupProgress(tenantId);
2020-05-20 15:14:44 +00:00
if (progress != null && !string.IsNullOrEmpty(progress.Error))
{
2020-05-29 14:48:27 +00:00
BackupWorker.ResetBackupError(tenantId);
throw new FaultException();
2020-05-20 15:14:44 +00:00
}
return progress;
}
public void DeleteBackup(Guid id)
{
2020-05-29 14:48:27 +00:00
var backupRecord = BackupRepository.GetBackupRecord(id);
BackupRepository.DeleteBackupRecord(backupRecord.Id);
2020-05-20 15:14:44 +00:00
2020-05-29 14:48:27 +00:00
var storage = BackupStorageFactory.GetBackupStorage(backupRecord);
2020-05-20 15:14:44 +00:00
if (storage == null) return;
storage.Delete(backupRecord.StoragePath);
}
public void DeleteAllBackups(int tenantId)
{
2020-05-29 14:48:27 +00:00
foreach (var backupRecord in BackupRepository.GetBackupRecordsByTenantId(tenantId))
2020-05-20 15:14:44 +00:00
{
try
{
2020-05-29 14:48:27 +00:00
BackupRepository.DeleteBackupRecord(backupRecord.Id);
var storage = BackupStorageFactory.GetBackupStorage(backupRecord);
2020-05-20 15:14:44 +00:00
if (storage == null) continue;
storage.Delete(backupRecord.StoragePath);
}
catch (Exception error)
{
2020-05-29 14:48:27 +00:00
Log.Warn("error while removing backup record: {0}", error);
2020-05-20 15:14:44 +00:00
}
}
}
public List<BackupHistoryRecord> GetBackupHistory(int tenantId)
{
var backupHistory = new List<BackupHistoryRecord>();
2020-05-29 14:48:27 +00:00
foreach (var record in BackupRepository.GetBackupRecordsByTenantId(tenantId))
2020-05-20 15:14:44 +00:00
{
2020-05-29 14:48:27 +00:00
var storage = BackupStorageFactory.GetBackupStorage(record);
2020-05-20 15:14:44 +00:00
if (storage == null) continue;
if (storage.IsExists(record.StoragePath))
{
2020-05-29 14:48:27 +00:00
backupHistory.Add(new BackupHistoryRecord
{
Id = record.Id,
FileName = record.Name,
StorageType = record.StorageType,
CreatedOn = record.CreatedOn,
ExpiresOn = record.ExpiresOn
});
2020-05-20 15:14:44 +00:00
}
else
{
2020-05-29 14:48:27 +00:00
BackupRepository.DeleteBackupRecord(record.Id);
2020-05-20 15:14:44 +00:00
}
}
return backupHistory;
}
public BackupProgress StartTransfer(StartTransferRequest request)
{
2020-05-29 14:48:27 +00:00
var progress = BackupWorker.StartTransfer(request.TenantId, request.TargetRegion, request.BackupMail, request.NotifyUsers);
2020-05-20 15:14:44 +00:00
if (!string.IsNullOrEmpty(progress.Error))
{
throw new FaultException();
}
return progress;
}
public BackupProgress GetTransferProgress(int tenantID)
{
2020-05-29 14:48:27 +00:00
var progress = BackupWorker.GetTransferProgress(tenantID);
2020-05-20 15:14:44 +00:00
if (!string.IsNullOrEmpty(progress.Error))
{
throw new FaultException();
}
return progress;
}
public BackupProgress StartRestore(StartRestoreRequest request)
{
if (request.StorageType == BackupStorageType.Local)
{
if (string.IsNullOrEmpty(request.FilePathOrId) || !File.Exists(request.FilePathOrId))
{
throw new FileNotFoundException();
}
}
if (!request.BackupId.Equals(Guid.Empty))
{
2020-05-29 14:48:27 +00:00
var backupRecord = BackupRepository.GetBackupRecord(request.BackupId);
2020-05-20 15:14:44 +00:00
if (backupRecord == null)
{
throw new FileNotFoundException();
}
request.FilePathOrId = backupRecord.StoragePath;
request.StorageType = backupRecord.StorageType;
2020-05-29 14:48:27 +00:00
request.StorageParams = JsonConvert.DeserializeObject<Dictionary<string, string>>(backupRecord.StorageParams);
2020-05-20 15:14:44 +00:00
}
2020-05-29 14:48:27 +00:00
var progress = BackupWorker.StartRestore(request);
2020-05-20 15:14:44 +00:00
if (!string.IsNullOrEmpty(progress.Error))
{
throw new FaultException();
}
return progress;
}
public BackupProgress GetRestoreProgress(int tenantId)
{
2020-05-29 14:48:27 +00:00
var progress = BackupWorker.GetRestoreProgress(tenantId);
2020-05-20 15:14:44 +00:00
if (progress != null && !string.IsNullOrEmpty(progress.Error))
{
2020-05-29 14:48:27 +00:00
BackupWorker.ResetRestoreError(tenantId);
2020-05-20 15:14:44 +00:00
throw new FaultException();
}
return progress;
}
public string GetTmpFolder()
{
2020-05-29 14:48:27 +00:00
return BackupWorker.TempFolder;
2020-05-20 15:14:44 +00:00
}
public List<TransferRegion> GetTransferRegions()
{
var webConfigs = BackupConfigurationSection.GetSection().WebConfigs;
return webConfigs
.Cast<WebConfigElement>()
.Select(configElement =>
{
var config = ConfigurationProvider.Open(PathHelper.ToRootedConfigPath(configElement.Path));
var baseDomain = config.AppSettings.Settings["core.base-domain"].Value;
2020-05-29 14:48:27 +00:00
return new TransferRegion
{
Name = configElement.Region,
BaseDomain = baseDomain,
IsCurrentRegion = configElement.Region.Equals(webConfigs.CurrentRegion, StringComparison.InvariantCultureIgnoreCase)
};
2020-05-20 15:14:44 +00:00
})
.ToList();
}
public void CreateSchedule(CreateScheduleRequest request)
{
2020-05-29 14:48:27 +00:00
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
}
public void DeleteSchedule(int tenantId)
{
2020-05-29 14:48:27 +00:00
BackupRepository.DeleteBackupSchedule(tenantId);
2020-05-20 15:14:44 +00:00
}
public ScheduleResponse GetSchedule(int tenantId)
{
2020-05-29 14:48:27 +00:00
var schedule = BackupRepository.GetBackupSchedule(tenantId);
2020-05-20 15:14:44 +00:00
return schedule != null
2020-05-29 14:48:27 +00:00
? 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)
}
2020-05-20 15:14:44 +00:00
: null;
}
}
public static class BackupServiceExtension
{
public static DIHelper AddBackupService(this DIHelper services)
{
services.TryAddScoped<BackupService>();
return services;
}
}
2020-05-20 15:14:44 +00:00
}