-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseBackupService.cs
More file actions
121 lines (100 loc) · 3.77 KB
/
Copy pathDatabaseBackupService.cs
File metadata and controls
121 lines (100 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DatabaseBackupService
{
public partial class DatabaseBackupService : ServiceBase
{
private string connectionString;
private string backupFolder;
private string logFolder;
private string databaseName;
public DatabaseBackupService()
{
InitializeComponent();
//Read configuration values
connectionString = ConfigurationManager.AppSettings["ConnectionString"];
backupFolder = ConfigurationManager.AppSettings["BackupFolder"];
logFolder = ConfigurationManager.AppSettings["LogFolder"];
databaseName = ConfigurationManager.AppSettings["DatabaseName"];
databaseName = ConfigurationManager.AppSettings["DatabaseName"];
if (string.IsNullOrEmpty(databaseName))
{
databaseName = "Master";
Log("Database name is missing in App.config, using default: Master");
}
if (string.IsNullOrEmpty(backupFolder))
{
backupFolder = @"C:\DatabaseBackupService\backupFolder";
Log("Backup folder is missing in App.config, Using default: " + backupFolder);
}
if(string.IsNullOrEmpty(logFolder))
{
logFolder = @"C:\DatabaseBackupService\Logfolder";
Log("Log Folder is missing in App.config. Using default: " + logFolder);
}
//Ensure directores exist
Directory.CreateDirectory(backupFolder);
Directory.CreateDirectory(logFolder);
}
protected override void OnStart(string[] args)
{
Log("Service Sterted");
PerformBackup(null);
Log("Backup process completed.");
}
protected override void OnStop()
{
Log("Service Stoped.");
}
private void PerformBackup(object state)
{
try
{
string backupFileName = Path.Combine(backupFolder, $"{databaseName}_backup_{DateTime.Now:yyyyMMdd_HHmmss}.bak");
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string backupQuery = $@"BACKUP DATABASE [{databaseName}] TO DISK = '{backupFileName}' WITH INIT";
using (SqlCommand command = new SqlCommand(backupQuery, connection))
{
command.ExecuteNonQuery();
}
}
Log($"Database backup successful for [{databaseName}]: {backupFileName}");
}
catch (Exception ex)
{
Log($"Error during backup of [{databaseName}]: {ex.Message}");
}
}
public void Log(string message)
{
string logFilePath = Path.Combine(logFolder, "ServiceLog.txt");
string logMessage = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}{Environment.NewLine}";
File.AppendAllText(logFilePath, logMessage);
//output to console if running in debug mode
if(Environment.UserInteractive)
{
Console.Write(logMessage);
}
}
public void StarInConsole()
{
OnStart(null);
Console.WriteLine("Press Enter to stop the service...");
Console.ReadLine();
OnStop();
}
}
}