-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask.cs
196 lines (172 loc) · 4.85 KB
/
Task.cs
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Xml.Serialization;
namespace ExpressBackup
{
public class Config
{
public string
LogFile = "ExpressBackup.log",
ZipPassword;
public int
LogLevel = 0;
public bool
StopAtError = false;
public Smtp
Smtp;
public string
OnFailureMail;
[
XmlArrayItem("BackupTask", typeof(BackupTask)),
XmlArrayItem("BackupCleanupTask", typeof(BackupCleanupTask)),
XmlArrayItem("IndexRebuildTask", typeof(IndexRebuildTask)),
XmlArrayItem("UpdateStatsTask", typeof(UpdateStatsTask)),
XmlArrayItem("BackupDirectoryTask", typeof(BackupDirectoryTask))
]
public List<Task>
Tasks;
}
public abstract class Task
{
[XmlAttribute]
public string
ID;
[XmlAttribute]
public bool
Disabled = false;
public abstract void Execute(Config config, DateTime t);
public virtual void Validate(Config config)
{
}
protected void Check<T>(string field, params T[] values)
{
var
fi = this.GetType().GetField(field, BindingFlags.Public | BindingFlags.Instance);
Debug.Assert(fi != null);
var
fieldValue = (T)fi.GetValue(this);
foreach (var value in values)
if (object.Equals(fieldValue, value))
throw new Exception("task [" + this.ID + "] > [" + field + "] is required");
}
}
public abstract class SqlTask : Task
{
// Sql connection
public string
SqlServer,
SqlDatabase,
SqlUser,
SqlPassword;
public bool
IntegratedSecurity;
public override void Validate(Config config)
{
Check("SqlServer", null, string.Empty);
Check("SqlDatabase", null, string.Empty);
}
}
public enum BackupType
{
Full,
Differential,
TransactionLog
}
public class BackupTask : SqlTask
{
public BackupType
SqlBackupType;
// Local
public string
LocalPath;
// Uploader
[
XmlElement("Ftp", typeof(FtpUploader)),
XmlElement("Sftp", typeof(SftpUploader))
]
public Uploader
Uploader;
public override void Execute(Config config, DateTime t)
{
Executor.Backup(config, this, t);
}
}
public class BackupDirectoryTask : SqlTask
{
// Local
public string
LocalPath,
BackupPath,
BackupPrefix;
// Uploader
[
XmlElement("Ftp", typeof(FtpUploader)),
XmlElement("Sftp", typeof(SftpUploader))
]
public Uploader
Uploader;
public override void Execute(Config config, DateTime t)
{
Executor.BackupDirectory(config, this, t);
}
public override void Validate(Config config)
{
Check("LocalPath", null, string.Empty);
Check("BackupPath", null, string.Empty);
Check("BackupPrefix", null, string.Empty);
}
}
public class BackupCleanupTask : Task
{
public string
CleanupFilePrefix,
CleanupFileExtention = ".7z",
CleanupFileMatch = "yyyyMMddHHmm";
public int
CleanupKeepDays = -1,
CleanupFtpKeepDays = -1;
public string
LocalPath;
// Uploader
[
XmlElement("Ftp", typeof(FtpUploader)),
XmlElement("Sftp", typeof(SftpUploader))
]
public Uploader
Uploader;
public override void Execute(Config config, DateTime t)
{
Executor.Cleanup(config, this, t);
}
public override void Validate(Config config)
{
Check("LocalPath", null, string.Empty);
Check("CleanupKeepDays", -1);
if (this.CleanupFtpKeepDays == -1)
this.CleanupFtpKeepDays = this.CleanupKeepDays;
}
}
public class IndexRebuildTask : SqlTask
{
public bool
IndexAllowRebuild = true,
IndexTryOnline = true;
public override void Execute(Config config, DateTime t)
{
Executor.Indexes(config, this);
}
}
public class UpdateStatsTask : SqlTask
{
public int
Percent = 100;
public bool
Recompute = false;
public override void Execute(Config config, DateTime t)
{
Executor.Statistics(config, this);
}
}
}