-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseBackUpService.cs
More file actions
116 lines (91 loc) · 3.49 KB
/
DatabaseBackUpService.cs
File metadata and controls
116 lines (91 loc) · 3.49 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
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.ServiceProcess;
using System.Threading;
namespace DatabaseBackUpService
{
public partial class DatabaseBackUpService : ServiceBase
{
string ConnectionString;
string BackupFolder;
string LogFolder;
int BackupIntervalMinutes;
Timer BackupTimer;
public DatabaseBackUpService()
{
InitializeComponent();
ConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
BackupFolder = ConfigurationManager.AppSettings["BackupFolder"];
LogFolder = ConfigurationManager.AppSettings["LogFolder"];
BackupIntervalMinutes = int.TryParse(ConfigurationManager.AppSettings["BackupIntervalMinutes"], out int Interval) ? Interval : 60;
}
private void SetNewDatabaseBackup(object state)
{
try
{
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
using (SqlCommand command = new SqlCommand("SP_FullBackupDatabase", connection))
{
if (!Directory.Exists(BackupFolder))
Directory.CreateDirectory(BackupFolder);
command.Parameters.AddWithValue("@BackupLocation", BackupFolder);
command.CommandType = System.Data.CommandType.StoredProcedure;
SqlParameter outputParam = new SqlParameter("@SourceFilePath", SqlDbType.VarChar, 255)
{
Direction = ParameterDirection.Output
};
command.Parameters.Add(outputParam);
connection.Open();
command.ExecuteNonQuery();
string backupFilePath = outputParam.Value.ToString();
WriteLog($"Database Backup Created Successfully {backupFilePath}");
}
}
}
catch (Exception ex)
{
WriteLog("Error occurred during backup: " + ex.Message);
}
}
protected override void OnStart(string[] args)
{
WriteLog("Started Service.");
BackupTimer = new Timer(
state => SetNewDatabaseBackup(state),
null,
0,
BackupIntervalMinutes * 60 * 1000
);
WriteLog($"Backup schedule Initiated: every {BackupIntervalMinutes} minute(s).");
}
protected override void OnStop()
{
BackupTimer?.Dispose();
WriteLog("Stopped Service.");
}
private void WriteLog(string Message)
{
if (!Directory.Exists(LogFolder))
Directory.CreateDirectory(LogFolder);
string LogMessage = $"[{DateTime.Now:yyyy-MM-dd HH-mm-ss}] {Message}\n";
File.AppendAllText(Path.Combine(LogFolder, "Logs.txt"), LogMessage);
if (Environment.UserInteractive)
{
Console.WriteLine(LogMessage);
}
}
public void StartInConsole()
{
Console.WriteLine("Start-Pending...");
OnStart(null);
Console.ReadLine();
Console.WriteLine("Stop-Pending...");
OnStop();
Console.ReadKey();
}
}
}