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

323 lines
12 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-06-15 15:33:53 +00:00
using ASC.Common.Caching;
2020-05-20 15:14:44 +00:00
using ASC.Common.Logging;
using ASC.Common.Utils;
2020-06-15 15:33:53 +00:00
using ASC.Core.Common.Contracts;
2020-05-20 15:14:44 +00:00
using ASC.Data.Backup.EF.Model;
using ASC.Data.Backup.Storage;
2020-05-29 14:48:27 +00:00
using ASC.Data.Backup.Utils;
2020-06-15 15:33:53 +00:00
using Google.Protobuf.WellKnownTypes;
using Microsoft.Extensions.Configuration;
2020-06-15 15:33:53 +00:00
using Microsoft.Extensions.DependencyInjection;
2020-05-29 14:48:27 +00:00
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
2020-05-20 15:14:44 +00:00
namespace ASC.Data.Backup.Service
{
2020-06-15 15:33:53 +00:00
internal class BackupServiceNotifier
{
private IServiceProvider ServiceProvider { get; }
private ICacheNotify<StartBackupRequest> СacheStartBackupRequest { get; set; }
private ICacheNotify<StartRestoreRequest> СacheStartRestoreRequest { get; set; }
private ICacheNotify<StartTransferRequest> СacheStartTransferRequest { get; set; }
public BackupServiceNotifier(
IServiceProvider serviceProvider,
ICacheNotify<StartBackupRequest> cacheStartBackupRequest,
ICacheNotify<StartRestoreRequest> cacheStartRestoreRequest,
ICacheNotify<StartTransferRequest> cacheStartTransferRequest)
{
ServiceProvider = serviceProvider;
СacheStartBackupRequest = cacheStartBackupRequest;
СacheStartRestoreRequest = cacheStartRestoreRequest;
СacheStartTransferRequest = cacheStartTransferRequest;
}
public void Subscribe()
{
СacheStartBackupRequest.Subscribe((n) =>
{
using var scope = ServiceProvider.CreateScope();
var backupService = scope.ServiceProvider.GetService<BackupService>();
backupService.StartBackup(n);
}
, CacheNotifyAction.InsertOrUpdate);
СacheStartRestoreRequest.Subscribe((n) =>
{
using var scope = ServiceProvider.CreateScope();
var backupService = scope.ServiceProvider.GetService<BackupService>();
backupService.StartRestore(n);
}, CacheNotifyAction.InsertOrUpdate);
}
}
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 IConfiguration Configuration { get; }
2020-05-29 14:48:27 +00:00
public BackupService(
IOptionsMonitor<ILog> options,
BackupStorageFactory backupStorageFactory,
BackupWorker backupWorker,
BackupRepository backupRepository,
2020-06-15 15:33:53 +00:00
IConfiguration configuration)
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;
Configuration = configuration;
2020-05-20 15:14:44 +00:00
}
2020-06-15 15:33:53 +00:00
public void StartBackup(StartBackupRequest request)
2020-06-08 10:40:26 +00:00
{
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();
2020-06-15 15:33:53 +00:00
}
}
2020-05-20 15:14:44 +00:00
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
{
2020-06-08 10:40:26 +00:00
Id = record.Id.ToString(),
2020-05-29 14:48:27 +00:00
FileName = record.Name,
2020-06-08 10:40:26 +00:00
StorageType = (int)record.StorageType,
CreatedOn = Timestamp.FromDateTimeOffset(record.CreatedOn),
ExpiresOn = Timestamp.FromDateTimeOffset(record.ExpiresOn)
2020-05-29 14:48:27 +00:00
});
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;
}
2020-06-15 15:33:53 +00:00
public void StartTransfer(StartTransferRequest request)
2020-05-20 15:14:44 +00:00
{
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();
}
}
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;
}
2020-06-15 15:33:53 +00:00
public void StartRestore(StartRestoreRequest request)
2020-05-20 15:14:44 +00:00
{
2020-06-08 10:40:26 +00:00
if ((BackupStorageType)request.StorageType == BackupStorageType.Local)
2020-05-20 15:14:44 +00:00
{
if (string.IsNullOrEmpty(request.FilePathOrId) || !File.Exists(request.FilePathOrId))
{
throw new FileNotFoundException();
}
}
2020-06-15 15:33:53 +00:00
if (request.BackupId != "" && !request.BackupId.Equals(Guid.Empty))
2020-05-20 15:14:44 +00:00
{
2020-06-08 10:40:26 +00:00
var backupRecord = BackupRepository.GetBackupRecord(Guid.Parse(request.BackupId));
2020-05-20 15:14:44 +00:00
if (backupRecord == null)
{
throw new FileNotFoundException();
}
request.FilePathOrId = backupRecord.StoragePath;
2020-06-08 10:40:26 +00:00
request.StorageType = (int)backupRecord.StorageType;
request.StorageParams.Add(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();
}
}
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 settings = Configuration.GetSetting<BackupSettings>("backup");
return settings.WebConfigs.Elements.Select(configElement =>
{
var config = Utils.ConfigurationProvider.Open(PathHelper.ToRootedConfigPath(configElement.Path));
var baseDomain = config.AppSettings.Settings["core.base-domain"].Value;
return new TransferRegion
{
Name = configElement.Region,
BaseDomain = baseDomain,
IsCurrentRegion = configElement.Region.Equals(settings.WebConfigs.CurrentRegion, StringComparison.InvariantCultureIgnoreCase)
};
})
.ToList();
2020-05-20 15:14:44 +00:00
}
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,
2020-06-08 10:40:26 +00:00
StorageType = (BackupStorageType)request.StorageType,
2020-05-29 14:48:27 +00:00
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-06-15 15:33:53 +00:00
if (schedule != null)
2020-06-08 10:40:26 +00:00
{
var tmp = new ScheduleResponse
{
StorageType = (int)schedule.StorageType,
StorageBasePath = schedule.StorageBasePath,
BackupMail = schedule.BackupMail,
NumberOfBackupsStored = schedule.BackupsStored,
Cron = schedule.Cron,
LastBackupTime = Timestamp.FromDateTimeOffset(schedule.LastBackupTime),
};
tmp.StorageParams.Add(JsonConvert.DeserializeObject<Dictionary<string, string>>(schedule.StorageParams));
return tmp;
}
else
{
return null;
}
2020-05-20 15:14:44 +00:00
}
}
public static class BackupServiceExtension
{
public static DIHelper AddBackupService(this DIHelper services)
{
2020-06-15 15:33:53 +00:00
services.TryAddScoped<BackupService>();
services.TryAddSingleton<BackupServiceNotifier>();
2020-06-08 10:40:26 +00:00
return services
.AddKafkaService();
}
}
2020-05-20 15:14:44 +00:00
}