Files
DiunaBI/WebAPI/Controllers/AdminController.cs

107 lines
3.8 KiB
C#
Raw Normal View History

2023-09-01 18:23:53 +02:00
using System.Data;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
2023-11-29 22:14:37 +01:00
using Microsoft.Data.SqlClient;
2023-09-01 18:23:53 +02:00
using WebAPI.Models;
2024-06-18 19:40:16 +02:00
namespace WebAPI.Controllers;
[ApiController]
[Route("api/[controller]")]
public class AdminController : Controller
2023-09-01 18:23:53 +02:00
{
2024-06-18 19:40:16 +02:00
private readonly GoogleDriveHelper _googleDriveHelper;
private readonly IConfiguration _configuration;
private readonly LogsController _logsController;
public AdminController(
GoogleDriveHelper googleDriveHelper,
IConfiguration configuration,
LogsController logsController)
2023-09-01 18:23:53 +02:00
{
2024-06-18 19:40:16 +02:00
_googleDriveHelper = googleDriveHelper;
_configuration = configuration;
_logsController = logsController;
}
2023-09-01 18:23:53 +02:00
2024-06-18 19:40:16 +02:00
[HttpGet]
[Route("BackupDatabase/{apiKey}")]
[AllowAnonymous]
public IActionResult BackupDatabase(string apiKey)
{
if (Request.Host.Value != _configuration["apiLocalUrl"] || apiKey != _configuration["apiKey"])
2023-09-01 18:23:53 +02:00
{
2024-06-18 19:40:16 +02:00
return Unauthorized();
2023-09-01 18:23:53 +02:00
}
2024-06-18 19:40:16 +02:00
try
2023-11-29 22:14:37 +01:00
{
2024-06-18 19:40:16 +02:00
const string databaseName = "diunabi-morska";
var localDatabasePath = $"{_configuration["dbBackupFile"]}-{DateTime.UtcNow.Day}.bak";
const string formatMediaName = $"DatabaseToolkitBackup_{databaseName}";
const string formatName = $"Full Backup of {databaseName}";
2023-11-29 22:14:37 +01:00
2024-06-18 19:40:16 +02:00
var connection = new SqlConnection(_configuration.GetConnectionString("SQLDatabase"));
2023-11-29 22:14:37 +01:00
2024-06-18 19:40:16 +02:00
const string sql = """
BACKUP DATABASE @databaseName
TO DISK = @localDatabasePath
WITH FORMAT,
MEDIANAME = @formatMediaName,
NAME = @formatName
""";
2023-11-29 22:14:37 +01:00
2024-06-18 19:40:16 +02:00
connection.Open();
var command = new SqlCommand(sql, connection);
2023-11-29 22:14:37 +01:00
2024-06-18 19:40:16 +02:00
command.CommandType = CommandType.Text;
command.CommandTimeout = 7200;
command.Parameters.AddWithValue("@databaseName", databaseName);
command.Parameters.AddWithValue("@localDatabasePath", localDatabasePath);
command.Parameters.AddWithValue("@formatMediaName", formatMediaName);
command.Parameters.AddWithValue("@formatName", formatName);
2023-11-29 22:14:37 +01:00
2024-06-18 19:40:16 +02:00
command.ExecuteNonQuery();
2023-11-29 22:14:37 +01:00
2024-06-18 19:40:16 +02:00
var body = new Google.Apis.Drive.v3.Data.File
{
Name = Path.GetFileName(localDatabasePath),
Parents = new List<string?> { _configuration["GDriveBackupDirectory"] },
MimeType = "application/octet-stream"
};
2023-09-01 18:23:53 +02:00
2024-06-18 19:40:16 +02:00
var fsSource = new FileStream(localDatabasePath, FileMode.Open, FileAccess.Read);
2023-09-01 18:23:53 +02:00
2024-06-18 19:40:16 +02:00
if (_googleDriveHelper.Service is null)
{
throw new Exception("Google Drive API not initialized");
}
2023-11-29 22:14:37 +01:00
2024-06-18 19:40:16 +02:00
var request = _googleDriveHelper.Service.Files.Create(body, fsSource, body.MimeType);
request.Fields = "id";
2023-11-29 22:14:37 +01:00
2024-06-18 19:40:16 +02:00
request.Upload();
2024-06-18 18:39:02 +02:00
2024-06-18 19:40:16 +02:00
_logsController.AddEntry(new LogEntry
2023-11-29 22:14:37 +01:00
{
2024-06-18 19:40:16 +02:00
Title = "Backup success",
Type = LogEntryType.info,
LogType = LogType.backup,
CreatedAt = DateTime.UtcNow
});
return Ok();
}
catch (Exception e)
{
_logsController.AddEntry(new LogEntry
{
Title = "Backup error",
Type = LogEntryType.error,
LogType = LogType.backup,
Message = e.ToString(),
CreatedAt = DateTime.UtcNow
});
return BadRequest(e.ToString());
2023-09-01 18:23:53 +02:00
}
}
}