Я создал Windows сервис, который берет SQL Резервное копирование базы данных сервера за фиксированный интервал времени. Служба Windows отлично работает в режиме отладки, но после установки она продолжает работать, но не создает файл .bak
.
Я проверил файл журнала и обнаружил
Не удается открыть базу данных "dbTest" запрашивается логином. Не удалось войти в систему.
Я использовал Windows аутентификацию для соединения.
Вот мой фрагмент кода
public partial class Service1 : ServiceBase
{
Timer _timer = new Timer();
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
this.WriteToFile("Simple Service started {0}");
// _timer = new Timer(10 * 60 * 1000); // every 10 minutes
_timer = new Timer(60000); // every 10 minutes
_timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
_timer.Start(); // <- important
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.WriteToFile("ScheduleBackupService: Method Call");
this.ScheduleBackupService();
}
public void ScheduleBackupService()
{
string backupPath = ConfigurationManager.AppSettings["BackupFile"];
this.WriteToFile("Backup Path {0}" + backupPath);
string ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
this.WriteToFile("Connection String {0}" + ConnectionString);
string DbList = ConfigurationManager.AppSettings["dbList"];
this.WriteToFile("Connection String {0}" + DbList);
string[] words = DbList.Split(',');
foreach (string word in words)
{
//Console.WriteLine("WORD: " + word);
CreateBackFile(backupPath, word, ConnectionString);
}
}
internal static string CreateDirectory(string path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
public string GetDirectoryPath()
{
string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SqlDataBaseBackup");
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
return filePath;
}
public void CreateBackFile(string path, string dbName, string ConnectionString)
{
try
{
string name = dbName;
name = name + ".bak";
string backupPath = @"BACKUP DATABASE " + dbName + " TO DISK = N'" + path + @"\" + name + @"'";
this.WriteToFile("backup Path Query" + backupPath);
string svr = ConnectionString;
SqlConnection con = new SqlConnection(svr);
SqlCommand cmd = new SqlCommand(backupPath, con);
try
{
con.Open();
cmd.ExecuteNonQuery();
this.WriteToFile("BackUp Create Successfully {0}" + dbName);
}
catch (Exception ex)
{
WriteToFile("Service Error Exception CreateBackFile: {0} " + ex.Message);
this.WriteToFile("Service Error Exception CreateBackFile" + ex.StackTrace);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
catch (Exception ex)
{
this.WriteToFile("Service Error Exception : {0} " + ex.Message);
this.WriteToFile("Service Error Exception : {0}" + ex.StackTrace);
//Stop the Windows Service.
using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController("Service1"))
{
serviceController.Stop();
}
}
}
private void CreateZipFile(string path, string dbName)
{
//to store the value of folderapth
string FolderPathToZip = path;
//To create unique file name with date and time with nanoseconds.
string ZbackupPath = CreateDirectory(GetDirectoryPath() + "\\BackupZip\\");
string ZipFileName = ZbackupPath + dbName + ".zip";
try
{
if (System.IO.File.Exists(ZipFileName))
System.IO.File.Delete(ZipFileName);
ZipFile.CreateFromDirectory(FolderPathToZip, ZipFileName);
Array.ForEach(Directory.GetFiles(path), File.Delete);
this.WriteToFile("File Copy Successfully {0} this" + path);
}
catch (Exception ex)
{
//If system throw any exception message box will display "SOME ERROR"
// Notification(ex.Message);
}
}
public void WriteToFile(string text)
{
string path = ConfigurationManager.AppSettings["LogFile"];
using (StreamWriter writer = new StreamWriter(path, true))
{
writer.WriteLine(string.Format(text, DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt")));
writer.Close();
}
}
protected override void OnStop()
{
this.WriteToFile("Simple Service stopped {0}");
}
}
Вот моя строка соединения, сохраненная в app.config
:
<appSettings>
<add key ="Mode" value ="Interval"/>
<add key ="ConnectionString" value ="Data Source=DESKTOP-2TGRDDE;Initial Catalog=dbTest;Integrated Security=True"/>
<add key ="dbList" value ="dbBackUpFtp"/>
<add key ="LogFile" value ="D:\\Backup\\ServiceLog.txt"/>
<add key ="BackupFile" value ="D:\\Backup\\"/>
<add key ="IntervalMinutes" value ="1"/>
<add key ="ScheduledTime" value ="16:02"/>
</appSettings>
и содержимое установщика проекта при инициализации
private void InitializeComponent()
{
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
//
// serviceInstaller1
//
this.serviceInstaller1.Description = "BackUp Service";
this.serviceInstaller1.DisplayName = "BackUpService.Demo";
this.serviceInstaller1.ServiceName = "Service1";
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
}
Извините, я загрузил весь код, как и многие исследования, но не получилось.
Есть какие-либо необходимо внести изменения в секцию кода