Remove unused backup enpoint
This commit is contained in:
@@ -9,103 +9,14 @@ namespace WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class AdminController : Controller
|
||||
{
|
||||
private readonly GoogleDriveHelper _googleDriveHelper;
|
||||
public class AdminController : Controller {
|
||||
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly LogsController _logsController;
|
||||
|
||||
public AdminController(
|
||||
GoogleDriveHelper googleDriveHelper,
|
||||
GoogleSheetsHelper googleSheetsHelper,
|
||||
IConfiguration configuration,
|
||||
FirestoreDb firestoreDb)
|
||||
IConfiguration configuration)
|
||||
{
|
||||
_googleDriveHelper = googleDriveHelper;
|
||||
_configuration = configuration;
|
||||
_logsController = new LogsController(firestoreDb);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("BackupDatabase/{apiKey}")]
|
||||
[AllowAnonymous]
|
||||
public IActionResult BackupDatabase(string apiKey)
|
||||
{
|
||||
if (Request.Host.Value != _configuration["apiLocalUrl"] || apiKey != _configuration["apiKey"])
|
||||
{
|
||||
return Unauthorized();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
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}";
|
||||
|
||||
|
||||
var connection = new SqlConnection(_configuration.GetConnectionString("SQLDatabase"));
|
||||
|
||||
const string sql = """
|
||||
BACKUP DATABASE @databaseName
|
||||
TO DISK = @localDatabasePath
|
||||
WITH FORMAT,
|
||||
MEDIANAME = @formatMediaName,
|
||||
NAME = @formatName
|
||||
""";
|
||||
|
||||
connection.Open();
|
||||
var command = new SqlCommand(sql, connection);
|
||||
|
||||
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);
|
||||
|
||||
command.ExecuteNonQuery();
|
||||
|
||||
var body = new Google.Apis.Drive.v3.Data.File
|
||||
{
|
||||
Name = Path.GetFileName(localDatabasePath),
|
||||
Parents = new List<string?> { "1ANm0pokPFgBw7vIaxLgDWXqznl3UjVvJ" },
|
||||
MimeType = "application/octet-stream"
|
||||
};
|
||||
|
||||
var fsSource = new FileStream(localDatabasePath, FileMode.Open, FileAccess.Read);
|
||||
|
||||
if (_googleDriveHelper.Service is null)
|
||||
{
|
||||
throw new Exception("Google Drive API not initialized");
|
||||
}
|
||||
|
||||
var request = _googleDriveHelper.Service.Files.Create(body, fsSource, body.MimeType);
|
||||
request.Fields = "id";
|
||||
|
||||
request.Upload();
|
||||
|
||||
_logsController.AddEntry(new LogEntry
|
||||
{
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
|
||||
Reference in New Issue
Block a user