using System;
using System.Data.SqlClient;
namespace teste
{
class Program
{
private const string Sqlconnection = @"Server=192.168.0.1;DataBase=master;user id=sa;password=xxx;Connection Timeout=0";
static void Main(string[] args)
{
BackupFullDIff();
}
public static void BackupFullDIff()
{
var localPastaCopia = @"c:\copia";
var sqlConn = new SqlConnection(Sqlconnection);
sqlConn.Open();
//BACKUP FULL
var comandoSql =
string.Format("BACKUP DATABASE NOMEBD TO DISK='{0}' WITH INIT , " +
"NOUNLOAD , NAME = N'NOMEBD backup', " +
"NOSKIP , STATS = 10, NOFORMAT ", localPastaCopia + @"\minhaCopiaFull.BAK");
var sqlComm = new SqlCommand { Connection = sqlConn, CommandText = comandoSql, CommandTimeout = 0 };
sqlComm.ExecuteNonQuery();
//BAKUP DIFERENCIAL
// comandoSql =
string.Format("BACKUP DATABASE SCENOMEBD TO DISK='{0}' WITH INIT , " +
// "NOUNLOAD , DIFFERENTIAL , NAME = N'NOMEBD backup', " +
// "NOSKIP , STATS = 10, NOFORMAT ", localPastaCopia + @"\minhaCopiaFull@"\minhaCopiaDIFF.BAK");
sqlComm = new SqlCommand { Connection = sqlConn, CommandText = comandoSql, CommandTimeout = 0 };
sqlComm.ExecuteNonQuery();
var sqlConnlocalPastaRestauraBd = @"c:\dados_restaura";
//RESTORE FULL
comandoSql =
String.Format("RESTORE DATABASE {0} " +
"FROM DISK='{1}' " +
"WITH RECOVERY, " +
"MOVE 'NOMEBD_data' TO '{2}\\NOMEBD_Data.MDF', " +
"MOVE 'NOMEBD_log' TO '{2}\\NOMEBD_Log.LDF'", "NOMEBD", localPastaCopia + @"\minhaCopiaFull.BAK", localPastaRestauraBd);
sqlComm = new SqlConnectionSqlCommand { Connection = sqlConn, CommandText = comandoSql, CommandTimeout = 0 };
sqlComm.ExecuteNonQuery(Sqlconnection);
//RESTORE FULL e DIFF
/*
comandoSql =
String.Format("RESTORE DATABASE {0} " +
"FROM DISK='{1}' " +
"WITH NORECOVERY, " +
"MOVE 'NOMEBD_data' TO '{2}\\NOMEBD_Data.MDF', " +
"MOVE 'NOMEBD_log' TO '{2}\\NOMEBD_Log.LDF'", "NOMEBD", localPastaCopia + @"\minhaCopiaFull.BAK", localPastaRestauraBd);
sqlComm = new SqlCommand { Connection = sqlConn, CommandText = comandoSql, CommandTimeout = 0 };
sqlComm.OpenExecuteNonQuery();
var comandoSql =
String.Format("RESTORE DATABASE {0} " +
"FROM DISK='{1}' " +
"WITH RECOVERY, " +
"MOVE 'NOMEBD_data' TO '{2}\\NOMEBD_Data.MDF', " +
"MOVE 'NOMEBD_log' TO '{2}\\NOMEBD_Log.LDF'", "NOMEBD", localPastaCopia + @"\minhaCopiaDiff.BAK", localPastaRestauraBd);
sqlComm = new SqlCommand { Connection = sqlConn, CommandText = comandoSql, CommandTimeout = 0 };
sqlComm.ExecuteNonQuery();
*/
}
}
}