UPDATE: With .Net Core 2.0 you can use Microsoft.SqlServer.SqlManagementObjects (140.17265.0). SQL Server Management Objects (SMO) Framework
You can use SQL SMO under Windows and Linux.
Microsoft.SqlServer.SqlManagementObjects depends on System.Data.SqlClient (4.5.0)
Simple SMO backup example:
ServerConnection serverConnection = new ServerConnection("192.168.1.1", "user", "password");
Server server = new Server(serverConnection);
Database database = server.Databases["AdventureWorks"];
Backup backup = new Backup();
backup.Action = BackupActionType.Database;
backup.BackupSetDescription = "AdventureWorks - full backup";
backup.BackupSetName = "AdventureWorks backup";
backup.Database = "AdventureWorks";
BackupDeviceItem deviceItem = new BackupDeviceItem("AdventureWorks_Full_Backup.bak", DeviceType.File);
backup.Devices.Add(deviceItem);
backup.Incremental = false;
backup.LogTruncation = BackupTruncateLogType.Truncate;
backup.SqlBackup(server);
In .NetCore to backup/restore SQL Server database you can use common ADO.NET SqlConnection and SqlCommand objects. To customize backup/restore you need know the syntax of T-SQL BACKUP/RESTORE statements. Please consult with
RESTORE Statements (T-SQL)
BACKUP Statements (T-SQL)
using System;
using System.Data;
using System.Data.SqlClient;
namespace BackupRestore
{
class Program
{
static void Main(string[] args)
{
BackupDatabase("test", @"C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Backup\test.bak");
RestoreDatabase("test", @"C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Backup\test.bak");
}
private static void RestoreDatabase(string databaseName, string backupPath)
{
string commandText = $@"USE [master];
ALTER DATABASE [{databaseName}] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
RESTORE DATABASE [{databaseName}] FROM DISK = N'{backupPath}' WITH FILE = 1, NOUNLOAD, REPLACE, STATS = 5;
ALTER DATABASE [{databaseName}] SET MULTI_USER;";
SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder
{
DataSource = "localhost",
InitialCatalog = "master",
IntegratedSecurity = true
};
using (SqlConnection connection = new SqlConnection(connectionStringBuilder.ConnectionString))
{
connection.Open();
connection.InfoMessage += Connection_InfoMessage;
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = commandText;
command.CommandType = CommandType.Text;
command.ExecuteNonQuery();
}
}
}
private static void BackupDatabase(string databaseName, string backupPath)
{
string commandText = $@"BACKUP DATABASE [{databaseName}] TO DISK = N'{backupPath}' WITH NOFORMAT, INIT, NAME = N'{databaseName}-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10";
SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder
{
DataSource = "localhost",
InitialCatalog = "master",
IntegratedSecurity = true
};
using (SqlConnection connection = new SqlConnection(connectionStringBuilder.ConnectionString))
{
connection.Open();
connection.InfoMessage += Connection_InfoMessage;
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = commandText;
command.CommandType = CommandType.Text;
command.ExecuteNonQuery();
}
}
}
private static void Connection_InfoMessage(object sender, SqlInfoMessageEventArgs e)
{
Console.WriteLine(e.Message);
}
}
}
To RESTORE a database with new name for example newtest, you need execute next statement
RESTORE DATABASE [newtest]
FROM DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Backup\test.bak' WITH FILE = 1,
MOVE N'test' TO N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\newtest.mdf',
MOVE N'test_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\newtest_log.ldf', NOUNLOAD, STATS = 5