127 lines
4.5 KiB
C#
127 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using iPackage.Core.Web.Models.Entity;
|
|
using iPackage.Core.Web.Models.Settings;
|
|
using iPackage.Core.Web.Services.Contracts;
|
|
using iPackage.Extensions;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.Data.SqlClient;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace iPackage.Core.Web.Services
|
|
{
|
|
|
|
public class DbService : IDbService
|
|
{
|
|
private readonly ApplicationIdentityContext _context;
|
|
private readonly IOptionsSnapshot<SiteSettings> _adminUserSeedOptions;
|
|
|
|
public DbService(
|
|
ApplicationIdentityContext context,
|
|
IOptionsSnapshot<SiteSettings> adminUserSeedOptions)
|
|
{
|
|
_context = context;
|
|
_adminUserSeedOptions = adminUserSeedOptions;
|
|
}
|
|
public void Initialize()
|
|
{
|
|
_context.Database.EnsureCreated();
|
|
_context.Database.Migrate();
|
|
}
|
|
|
|
|
|
|
|
public string CreateBackUp()
|
|
{
|
|
var backUpName = $"{StringExtensions.GetId()}_{DateTime.Now.ToString(" yyyy-MM-dd HH-mm-ss")}.bak";
|
|
var path = $"{Directory.GetCurrentDirectory()}/wwwroot/BackUps/{backUpName}";
|
|
string commandText = $@"BACKUP DATABASE [iGarsonDB] TO DISK = N'{path}' WITH NOFORMAT, INIT, NAME = N'iGarsonDB-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10";
|
|
|
|
SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder
|
|
{
|
|
DataSource = ".",
|
|
InitialCatalog = "iGarsonDB",
|
|
IntegratedSecurity = true
|
|
};
|
|
using (SqlConnection connection = new SqlConnection(connectionStringBuilder.ConnectionString))
|
|
{
|
|
connection.Open();
|
|
using (SqlCommand command = connection.CreateCommand())
|
|
{
|
|
command.CommandText = commandText;
|
|
command.CommandType = CommandType.Text;
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
|
|
return backUpName;
|
|
}
|
|
|
|
public List<string> GetBackUps()
|
|
{
|
|
var files = Directory.GetFiles($"{Directory.GetCurrentDirectory()}/wwwroot/BackUps");
|
|
return files.Select(f => f.Split('\\').Last()).ToList();
|
|
}
|
|
|
|
public bool RestoreBackUp(string backUpId)
|
|
{
|
|
var files = Directory.GetFiles($"{Directory.GetCurrentDirectory()}/wwwroot/BackUps");
|
|
string backUpName = string.Empty;
|
|
foreach (var file in files)
|
|
{
|
|
if (file.Contains(backUpId))
|
|
backUpName = file;
|
|
}
|
|
if (string.IsNullOrEmpty(backUpName))
|
|
return false;
|
|
|
|
var databaseName = "iGarsonDB";
|
|
var path = backUpName;
|
|
string commandText = $@"USE [master];
|
|
ALTER DATABASE [{databaseName}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
|
|
RESTORE DATABASE [{databaseName}] FROM DISK = N'{path}' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5;
|
|
ALTER DATABASE [{databaseName}] SET MULTI_USER;";
|
|
|
|
SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder
|
|
{
|
|
DataSource = ".",
|
|
InitialCatalog = "iGarsonDB",
|
|
IntegratedSecurity = true
|
|
};
|
|
using (SqlConnection connection = new SqlConnection(connectionStringBuilder.ConnectionString))
|
|
{
|
|
connection.Open();
|
|
using (SqlCommand command = connection.CreateCommand())
|
|
{
|
|
command.CommandText = commandText;
|
|
command.CommandType = CommandType.Text;
|
|
command.ExecuteNonQuery();
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public bool RemoveBackUp(string backUpId)
|
|
{
|
|
var files = Directory.GetFiles($"{Directory.GetCurrentDirectory()}/wwwroot/BackUps");
|
|
string backUpName = string.Empty;
|
|
foreach (var file in files)
|
|
{
|
|
if (file.Contains(backUpId))
|
|
backUpName = file;
|
|
}
|
|
if (string.IsNullOrEmpty(backUpName))
|
|
return false;
|
|
var path = backUpName;
|
|
File.Delete(path);
|
|
return true;
|
|
}
|
|
}
|
|
}
|