mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-07-09 03:04:19 -04:00
Added more edge case handling for Library backup
This commit is contained in:
parent
0d8c0651a9
commit
0258c43b37
@ -27,5 +27,7 @@ namespace API.Interfaces.Services
|
||||
/// <param name="directoryPath"></param>
|
||||
/// <returns></returns>
|
||||
bool ExistOrCreate(string directoryPath);
|
||||
|
||||
void ClearAndDeleteDirectory(string directoryPath);
|
||||
}
|
||||
}
|
@ -16,6 +16,7 @@ namespace API.Services
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly ILogger<BackupService> _logger;
|
||||
private readonly IDirectoryService _directoryService;
|
||||
private readonly string _tempDirectory = Path.Join(Directory.GetCurrentDirectory(), "temp");
|
||||
|
||||
private readonly IList<string> _backupFiles = new List<string>()
|
||||
{
|
||||
@ -39,6 +40,7 @@ namespace API.Services
|
||||
{
|
||||
_logger.LogInformation("Beginning backup of Database at {BackupTime}", DateTime.Now);
|
||||
var backupDirectory = Task.Run(() => _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.BackupDirectory)).Result.Value;
|
||||
|
||||
_logger.LogDebug("Backing up to {BackupDirectory}", backupDirectory);
|
||||
if (!_directoryService.ExistOrCreate(backupDirectory))
|
||||
{
|
||||
@ -46,19 +48,36 @@ namespace API.Services
|
||||
return;
|
||||
}
|
||||
|
||||
var fileInfos = _backupFiles.Select(file => new FileInfo(Path.Join(Directory.GetCurrentDirectory(), file))).ToList();
|
||||
var dateString = DateTime.Now.ToShortDateString().Replace("/", "_");
|
||||
var zipPath = Path.Join(backupDirectory, $"kavita_backup_{dateString}.zip");
|
||||
|
||||
var zipPath = Path.Join(backupDirectory, $"kavita_backup_{DateTime.Now}.zip");
|
||||
using (var zipArchive = ZipFile.Open(zipPath, ZipArchiveMode.Create))
|
||||
if (File.Exists(zipPath))
|
||||
{
|
||||
foreach (var fileInfo in fileInfos)
|
||||
{
|
||||
zipArchive.CreateEntryFromFile(fileInfo.FullName, fileInfo.Name);
|
||||
}
|
||||
_logger.LogInformation("{ZipFile} already exists, aborting", zipPath);
|
||||
return;
|
||||
}
|
||||
|
||||
var tempDirectory = Path.Join(_tempDirectory, dateString);
|
||||
_directoryService.ExistOrCreate(tempDirectory);
|
||||
|
||||
|
||||
foreach (var file in _backupFiles)
|
||||
{
|
||||
var originalFile = new FileInfo(Path.Join(Directory.GetCurrentDirectory(), file));
|
||||
originalFile.CopyTo(Path.Join(tempDirectory, originalFile.Name));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
ZipFile.CreateFromDirectory(tempDirectory, zipPath);
|
||||
}
|
||||
catch (AggregateException ex)
|
||||
{
|
||||
_logger.LogError(ex, "There was an issue when archiving library backup");
|
||||
}
|
||||
|
||||
_directoryService.ClearAndDeleteDirectory(tempDirectory);
|
||||
_logger.LogInformation("Database backup completed");
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@ -66,6 +66,22 @@ namespace API.Services
|
||||
return true;
|
||||
}
|
||||
|
||||
public void ClearAndDeleteDirectory(string directoryPath)
|
||||
{
|
||||
DirectoryInfo di = new DirectoryInfo(directoryPath);
|
||||
|
||||
foreach (var file in di.EnumerateFiles())
|
||||
{
|
||||
file.Delete();
|
||||
}
|
||||
foreach (var dir in di.EnumerateDirectories())
|
||||
{
|
||||
dir.Delete(true);
|
||||
}
|
||||
|
||||
di.Delete(true);
|
||||
}
|
||||
|
||||
public IEnumerable<string> ListDirectory(string rootPath)
|
||||
{
|
||||
if (!Directory.Exists(rootPath)) return ImmutableList<string>.Empty;
|
||||
|
Loading…
x
Reference in New Issue
Block a user