AdminController; DbBackup route
This commit is contained in:
BIN
WebAPI/.DS_Store
vendored
Normal file
BIN
WebAPI/.DS_Store
vendored
Normal file
Binary file not shown.
112
WebAPI/Controllers/AdminController.cs
Normal file
112
WebAPI/Controllers/AdminController.cs
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
using System.Data;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.Xml.Serialization;
|
||||||
|
using Google.Apis.Sheets.v4;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Data.SqlClient;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using WebAPI.dataParsers;
|
||||||
|
using WebAPI.Exports;
|
||||||
|
using WebAPI.Models;
|
||||||
|
using static Google.Apis.Drive.v3.FilesResource;
|
||||||
|
|
||||||
|
namespace WebAPI.Controllers
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
|
||||||
|
public class AdminController : Controller
|
||||||
|
{
|
||||||
|
private readonly AppDbContext db;
|
||||||
|
private GoogleDriveHelper googleDriveHelper;
|
||||||
|
private GoogleSheetsHelper googleSheetsHelper;
|
||||||
|
private readonly IConfiguration configuration;
|
||||||
|
private readonly LogsController logsController;
|
||||||
|
|
||||||
|
public AdminController(
|
||||||
|
AppDbContext _db,
|
||||||
|
GoogleSheetsHelper _googleSheetsHelper,
|
||||||
|
GoogleDriveHelper _googleDriveHelper,
|
||||||
|
IConfiguration _configuration)
|
||||||
|
{
|
||||||
|
db = _db;
|
||||||
|
googleSheetsHelper = _googleSheetsHelper;
|
||||||
|
googleDriveHelper = _googleDriveHelper;
|
||||||
|
configuration = _configuration;
|
||||||
|
logsController = new LogsController(googleSheetsHelper, googleDriveHelper, configuration);
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
[Route("BackupDatabase/{apiKey}")]
|
||||||
|
[AllowAnonymous]
|
||||||
|
public IActionResult BackupDatabase(string apiKey)
|
||||||
|
{
|
||||||
|
if (Request.Host.Value != configuration["apiLocalUrl"] || apiKey != configuration["apiKey"])
|
||||||
|
{
|
||||||
|
return Unauthorized();
|
||||||
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string databaseName = "diunabi-morska";
|
||||||
|
string localDatabasePath = $"{configuration["dbBackupFile"]}-{DateTime.UtcNow.Day}.bak";
|
||||||
|
var formatMediaName = $"DatabaseToolkitBackup_{databaseName}";
|
||||||
|
var formatName = $"Full Backup of {databaseName}";
|
||||||
|
|
||||||
|
var connection = new SqlConnection(configuration.GetConnectionString("SQLDatabase"));
|
||||||
|
|
||||||
|
var 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();
|
||||||
|
|
||||||
|
Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File();
|
||||||
|
body.Name = Path.GetFileName(localDatabasePath);
|
||||||
|
body.Parents = new List<string?> { configuration["GDriveBackupDirectory"] };
|
||||||
|
body.MimeType = "application/octet-stream";
|
||||||
|
|
||||||
|
var fsSource = new FileStream(localDatabasePath, FileMode.Open, FileAccess.Read);
|
||||||
|
|
||||||
|
CreateMediaUpload request = googleDriveHelper.Service.Files.Create(body, fsSource, body.MimeType);
|
||||||
|
request.Fields = "id";
|
||||||
|
|
||||||
|
var response = 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -165,6 +165,7 @@ namespace WebAPI.Controllers
|
|||||||
{
|
{
|
||||||
Title = "Import error",
|
Title = "Import error",
|
||||||
Type = LogEntryType.error,
|
Type = LogEntryType.error,
|
||||||
|
LogType = LogType.import,
|
||||||
Message = e.ToString(),
|
Message = e.ToString(),
|
||||||
CreatedAt = DateTime.UtcNow
|
CreatedAt = DateTime.UtcNow
|
||||||
});
|
});
|
||||||
@@ -294,9 +295,9 @@ namespace WebAPI.Controllers
|
|||||||
{
|
{
|
||||||
Title = $"Import Success, {importWorker.Name}",
|
Title = $"Import Success, {importWorker.Name}",
|
||||||
Type = LogEntryType.info,
|
Type = LogEntryType.info,
|
||||||
|
LogType = LogType.import,
|
||||||
CreatedAt = DateTime.UtcNow
|
CreatedAt = DateTime.UtcNow
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
private Layer AddLayer(Layer input, Guid currentUserId)
|
private Layer AddLayer(Layer input, Guid currentUserId)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -29,6 +29,9 @@ namespace WebAPI.Controllers
|
|||||||
case LogType.import:
|
case LogType.import:
|
||||||
type = "Import";
|
type = "Import";
|
||||||
break;
|
break;
|
||||||
|
case LogType.backup:
|
||||||
|
type = "Backup";
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
type = "Other"; // should never happen
|
type = "Other"; // should never happen
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ namespace WebAPI.Models
|
|||||||
error,
|
error,
|
||||||
}
|
}
|
||||||
public enum LogType {
|
public enum LogType {
|
||||||
import
|
import,
|
||||||
|
backup
|
||||||
}
|
}
|
||||||
public class LogEntry
|
public class LogEntry
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -16,6 +16,8 @@
|
|||||||
"exportDirectory": "1eTyCUzYbzVQB8f8sbNmvnebFXyW2-axt",
|
"exportDirectory": "1eTyCUzYbzVQB8f8sbNmvnebFXyW2-axt",
|
||||||
"appLogsFile": "13PuDvS3_HAYoSLOCgKexzlzIDLUilkApUF8QiJMTae0",
|
"appLogsFile": "13PuDvS3_HAYoSLOCgKexzlzIDLUilkApUF8QiJMTae0",
|
||||||
"apiLocalUrl": "localhost:5400",
|
"apiLocalUrl": "localhost:5400",
|
||||||
|
"dbBackupFile": "/home/mz/backups/diunabi-morska",
|
||||||
|
"GDriveBackupDirectory": "1FEcdtVcF2cuqCROdq-vlBNs__th1M8_P",
|
||||||
"Kestrel": {
|
"Kestrel": {
|
||||||
"Endpoints": {
|
"Endpoints": {
|
||||||
"Http": {
|
"Http": {
|
||||||
|
|||||||
@@ -13,9 +13,11 @@
|
|||||||
"GoogleClientId": "#{google-backend-login-client-id}#",
|
"GoogleClientId": "#{google-backend-login-client-id}#",
|
||||||
"Secret": "#{google-backend-login-secret}#",
|
"Secret": "#{google-backend-login-secret}#",
|
||||||
"apiKey": "#{api-key}#",
|
"apiKey": "#{api-key}#",
|
||||||
"exportDirectory": "#{export-directory}#",
|
"exportDirectory": "#{export-directory}#",
|
||||||
"appLogsFile": "#{app-logs-file}#",
|
"appLogsFile": "#{app-logs-file}#",
|
||||||
"apiLocalUrl": "#{api-local-url}#",
|
"apiLocalUrl": "#{api-local-url}#",
|
||||||
|
"dbBackupFile": "#{db-backup-file}",
|
||||||
|
"GDriveBackupDirectory": "#{gdrive-backup-directory}",
|
||||||
"Kestrel": {
|
"Kestrel": {
|
||||||
"Endpoints": {
|
"Endpoints": {
|
||||||
"Http": {
|
"Http": {
|
||||||
|
|||||||
Reference in New Issue
Block a user