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
|
class Program
{
static void Main()
{
// [...] Un certain nombre d'opérations sans lien avec mon fichier XML
InstanceCollection serverList = new InstanceCollection();
serverList.Backup();
serverList.Save();
// [...] Un certain nombre d'opérations sans lien avec mon fichier XML
}
}
public sealed class InstanceCollection
{
public InstanceCollection()
{
XmlDocument doc = new XmlDocument();
doc.Load("servers.xml");
try
{
Retention = int.Parse(doc.DocumentElement.SelectSingleNode("/backupsql/retention").InnerText, NumberStyles.Integer, CultureInfo.CurrentCulture);
}
catch
{
Retention = 5;
}
DateTime purgeDate = DateTime.Now.AddDays(-1 * (Retention + 1));
foreach (XmlNode nd in doc.DocumentElement.SelectNodes("/backupsql/servers/server"))
{
instances.Add(new Instance(purgeDate, nd));
}
}
public void Backup()
{
Task[] tasks = new Task[instances.Count];
for (int i = 0, cpt = instances.Count; i < cpt; i++)
{
var value = i;
tasks[value] = new Task(() => instances[value].Backup()); // Cette méthode n'utilise pas le fichier XML
tasks[value].Start();
}
Task.WaitAll(tasks);
}
public void Save()
{
XmlDocument doc = new XmlDocument();
doc.Load("servers.xml");
XmlNode nbRetention = doc.SelectSingleNode("/backupsql/retention");
if (nbRetention == null)
{
XmlNode ndRoot = doc.SelectSingleNode("/backupsql");
nbRetention = ndRoot.OwnerDocument.CreateElement("retention");
ndRoot.AppendChild(nbRetention);
}
nbRetention.InnerXml = this.Retention.ToString(CultureInfo.CurrentCulture);
XmlNode ndInstances = doc.SelectSingleNode("/backupsql/servers");
ndInstances.RemoveAll();
foreach (Instance instance in instances)
{
XmlNode ndInstance = ndInstances.OwnerDocument.CreateElement("server");
XmlAttribute attInstanceName = ndInstance.OwnerDocument.CreateAttribute("name");
attInstanceName.Value = instance.Name;
XmlAttribute attInstanceRestoreTo = ndInstance.OwnerDocument.CreateAttribute("restore");
attInstanceRestoreTo.Value = instance.RestoreTo;
foreach (Database database in instance.Databases)
{
XmlNode ndDatabase = ndInstance.OwnerDocument.CreateElement("database");
XmlAttribute attDatabaseName = ndDatabase.OwnerDocument.CreateAttribute("name");
XmlAttribute attDatabaseLastFull = ndDatabase.OwnerDocument.CreateAttribute("lastfull");
XmlAttribute attDatabaseLastLog = ndDatabase.OwnerDocument.CreateAttribute("lastlog");
attDatabaseName.Value = database.Name;
attDatabaseLastFull.Value = database.LastFull.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.CurrentCulture);
attDatabaseLastLog.Value = database.LastLog.ToString("yyyy-MM-dd HH:mm:ss.fff", CultureInfo.CurrentCulture);
ndDatabase.Attributes.Append(attDatabaseName);
ndDatabase.Attributes.Append(attDatabaseLastFull);
ndDatabase.Attributes.Append(attDatabaseLastLog);
ndInstance.AppendChild(ndDatabase);
}
ndInstance.Attributes.Append(attInstanceName);
ndInstance.Attributes.Append(attInstanceRestoreTo);
ndInstances.AppendChild(ndInstance);
}
doc.Save("servers.xml");
}
} |
Partager