Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
using System;
using System.Diagnostics;
using Microsoft.Extensions.Configuration;
using MySql.Data.MySqlClient;
public class DatabaseBackupRepository
{
private readonly string _connectionString;
public DatabaseBackupRepository(IConfiguration configuration)
{
_connectionString = configuration.GetConnectionString("DefaultConnection");
}
public void BackupDatabase()
{
try
{
// Parse connection details
var builder = new MySqlConnectionStringBuilder(_connectionString);
string databaseName = builder.Database;
string username = builder.UserID;
string password = builder.Password;
string host = builder.Server;
// Backup file path
string backupFolder = @"D:MySqlBackups";
string backupFileName = $"{databaseName}_{DateTime.Now:yyyyMMddHHmmss}.sql";
string backupFilePath = System.IO.Path.Combine(backupFolder, backupFileName);
// Ensure backup directory exists
if (!System.IO.Directory.Exists(backupFolder))
{
System.IO.Directory.CreateDirectory(backupFolder);
}
// Define the mysqldump command
string command = $"-u{username} -p{password} -h {host} {databaseName} > "{backupFilePath}"";
// Start the mysqldump process
ProcessStartInfo processInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
UseShellExecute = false
};
using (Process process = new Process { StartInfo = processInfo })
{
process.Start();
using (var writer = process.StandardInput)
{
if (writer.BaseStream.CanWrite)
{
writer.WriteLine($"mysqldump {command}");
}
}
process.WaitForExit();
}
Console.WriteLine("Database backup completed successfully.");
}
catch (Exception ex)
{
Console.WriteLine($"Error occurred while backing up the database: {ex.Message}");
}
}
}
////////////
// DatabaseBackupController.cs
using Microsoft.AspNetCore.Mvc;
[Route("api/[controller]")]
[ApiController]
public class DatabaseBackupController : ControllerBase
{
private readonly DatabaseBackupRepository _backupRepository;
public DatabaseBackupController(DatabaseBackupRepository backupRepository)
{
_backupRepository = backupRepository;
}
[HttpPost("backup")]
public IActionResult BackupDatabase()
{
_backupRepository.BackupDatabase();
return Ok("Database backup initiated.");
}
}
/////////builder.Services.AddScoped<DatabaseBackupRepository>();
/////
// BackupService.cs
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
public class BackupService
{
private readonly HttpClient _httpClient;
private readonly string _apiUrl;
public BackupService(HttpClient httpClient, IConfiguration configuration)
{
_httpClient = httpClient;
_apiUrl = configuration.GetSection("ApiSettings:BackupApiUrl").Value;
}
public async Task<string> BackupDatabaseAsync()
{
var response = await _httpClient.PostAsync($"{_apiUrl}/api/DatabaseBackup/backup", null);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
/////////
// WebBackupController.cs
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
public class WebBackupController : Controller
{
private readonly BackupService _backupService;
public WebBackupController(BackupService backupService)
{
_backupService = backupService;
}
[HttpPost("start-backup")]
public async Task<IActionResult> StartBackup()
{
var result = await _backupService.BackupDatabaseAsync();
return Ok(result);
}
}
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team