public Lfx.Types.OperationResult Backup(BackupInfo backupInfo) { string WorkFolder = backupInfo.Name + System.IO.Path.DirectorySeparatorChar; Lfx.Environment.Folders.EnsurePathExists(this.BackupPath); if (!System.IO.Directory.Exists(Lfx.Environment.Folders.TemporaryFolder + WorkFolder)) { System.IO.Directory.CreateDirectory(Lfx.Environment.Folders.TemporaryFolder + WorkFolder); } Lfx.Types.OperationProgress Progreso = new Lfx.Types.OperationProgress("Creando copia de seguridad", "Se está creando un volcado completo del almacén de datos en una carpeta, para resguardar."); Progreso.Modal = true; Progreso.Advertise = true; Progreso.Begin(); Progreso.Max = Lfx.Workspace.Master.Structure.Tables.Count + 1; Progreso.ChangeStatus("Exportando estructura"); Progreso.ChangeStatus(Progreso.Value + 1); System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.AppendChild(Lfx.Workspace.Master.Structure.ToXml(doc)); doc.Save(Lfx.Environment.Folders.TemporaryFolder + WorkFolder + "dbstruct.xml"); BackupWriter Writer = new BackupWriter(Lfx.Environment.Folders.TemporaryFolder + WorkFolder + "dbdata.lbd"); Writer.Write(":BKP"); IList <string> TableList = Lfx.Data.DatabaseCache.DefaultCache.GetTableNames(); foreach (string Tabla in TableList) { string NombreTabla = Tabla; if (Lfx.Workspace.Master.Structure.Tables.ContainsKey(Tabla)) { NombreTabla = Lfx.Workspace.Master.Structure.Tables[Tabla].Label; } Progreso.ChangeStatus("Volcando " + NombreTabla); Progreso.ChangeStatus(Progreso.Value + 1); ExportTableBin(Tabla, Writer); } Writer.Close(); System.IO.FileStream Archivo = new System.IO.FileStream(Lfx.Environment.Folders.TemporaryFolder + WorkFolder + "info.txt", System.IO.FileMode.Append, System.IO.FileAccess.Write); using (System.IO.StreamWriter Escribidor = new System.IO.StreamWriter(Archivo, System.Text.Encoding.Default)) { Escribidor.WriteLine("Copia de seguridad de Lázaro"); Escribidor.WriteLine(""); Escribidor.WriteLine("Empresa=" + backupInfo.CompanyName); Escribidor.WriteLine("EspacioTrabajo=" + Lfx.Workspace.Master.Name); Escribidor.WriteLine("FechaYHora=" + System.DateTime.Now.ToString("dd-MM-yyyy") + " a las " + System.DateTime.Now.ToString("HH:mm:ss")); Escribidor.WriteLine("Usuario=" + backupInfo.UserName); Escribidor.WriteLine("Estación=" + Lfx.Environment.SystemInformation.MachineName); Escribidor.WriteLine("VersiónLazaro=" + backupInfo.ProgramVersion); Escribidor.WriteLine(""); Escribidor.WriteLine("Por favor no modifique ni elimine este archivo."); Escribidor.Close(); Archivo.Close(); } if (Lfx.Workspace.Master.CurrentConfig.ReadGlobalSetting <int>("Sistema.ComprimirCopiasDeSeguridad", 0) != 0) { Progreso.ChangeStatus("Comprimiendo los datos"); Lfx.FileFormats.Compression.Archive ArchivoComprimido = new Lfx.FileFormats.Compression.Archive(Lfx.Environment.Folders.TemporaryFolder + WorkFolder + "backup.7z"); ArchivoComprimido.Add(Lfx.Environment.Folders.TemporaryFolder + WorkFolder + "*"); if (System.IO.File.Exists(Lfx.Environment.Folders.TemporaryFolder + WorkFolder + "backup.7z")) { Progreso.ChangeStatus("Eliminando archivos temporales"); // Borrar los archivos que acabo de comprimir System.IO.DirectoryInfo Dir = new System.IO.DirectoryInfo(Lfx.Environment.Folders.TemporaryFolder + WorkFolder); foreach (System.IO.FileInfo DirItem in Dir.GetFiles()) { if (DirItem.Name != "backup.7z" && DirItem.Name != "info.txt") { System.IO.File.Delete(Lfx.Environment.Folders.TemporaryFolder + WorkFolder + DirItem.Name); } } } } Progreso.ChangeStatus("Almacenando"); Progreso.ChangeStatus(Progreso.Value + 1); Lfx.Environment.Folders.MoveDirectory(Lfx.Environment.Folders.TemporaryFolder + WorkFolder, this.BackupPath + WorkFolder); int GuardarBackups = Lfx.Workspace.Master.CurrentConfig.ReadGlobalSetting <int>("Sisteam.Backup.CantMax", 14); if (GuardarBackups > 0) { List <BackupInfo> ListaDeBackups = this.GetBackups(); if (ListaDeBackups.Count > GuardarBackups) { Progreso.ChangeStatus("Eliminando copias de seguridad antiguas"); int BorrarBackups = ListaDeBackups.Count - GuardarBackups; if (BorrarBackups < ListaDeBackups.Count) { for (int i = 1; i <= BorrarBackups; i++) { this.Delete(this.GetOldestBackupName()); } } } } Progreso.End(); return(new Lfx.Types.SuccessOperationResult()); }
public Lfx.Types.OperationResult Backup(BackupInfo backupInfo) { string WorkFolder = backupInfo.Name + System.IO.Path.DirectorySeparatorChar; Lfx.Environment.Folders.EnsurePathExists(this.BackupPath); if (!System.IO.Directory.Exists(Lfx.Environment.Folders.TemporaryFolder + WorkFolder)) System.IO.Directory.CreateDirectory(Lfx.Environment.Folders.TemporaryFolder + WorkFolder); Lfx.Types.OperationProgress Progreso = new Lfx.Types.OperationProgress("Creando copia de seguridad", "Se está creando un volcado completo del almacén de datos en una carpeta, para resguardar."); Progreso.Modal = true; Progreso.Advertise = true; Progreso.Begin(); Progreso.Max = Lfx.Workspace.Master.Structure.Tables.Count + 1; Progreso.ChangeStatus("Exportando estructura"); Progreso.ChangeStatus(Progreso.Value + 1); System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.AppendChild(Lfx.Workspace.Master.Structure.ToXml(doc)); doc.Save(Lfx.Environment.Folders.TemporaryFolder + WorkFolder + "dbstruct.xml"); BackupWriter Writer = new BackupWriter(Lfx.Environment.Folders.TemporaryFolder + WorkFolder + "dbdata.lbd"); Writer.Write(":BKP"); IList<string> TableList = Lfx.Data.DataBaseCache.DefaultCache.GetTableNames(); foreach (string Tabla in TableList) { string NombreTabla = Tabla; if (Lfx.Workspace.Master.Structure.Tables.ContainsKey(Tabla)) NombreTabla = Lfx.Workspace.Master.Structure.Tables[Tabla].Label; Progreso.ChangeStatus("Volcando " + NombreTabla); Progreso.ChangeStatus(Progreso.Value + 1); ExportTableBin(Tabla, Writer); } Writer.Close(); System.IO.FileStream Archivo = new System.IO.FileStream(Lfx.Environment.Folders.TemporaryFolder + WorkFolder + "info.txt", System.IO.FileMode.Append, System.IO.FileAccess.Write); using (System.IO.StreamWriter Escribidor = new System.IO.StreamWriter(Archivo, System.Text.Encoding.Default)) { Escribidor.WriteLine("Copia de seguridad de Lázaro"); Escribidor.WriteLine(""); Escribidor.WriteLine("Empresa=" + backupInfo.CompanyName); Escribidor.WriteLine("EspacioTrabajo=" + Lfx.Workspace.Master.Name); Escribidor.WriteLine("FechaYHora=" + System.DateTime.Now.ToString("dd-MM-yyyy") + " a las " + System.DateTime.Now.ToString("HH:mm:ss")); Escribidor.WriteLine("Usuario=" + backupInfo.UserName); Escribidor.WriteLine("Estación=" + Lfx.Environment.SystemInformation.MachineName); Escribidor.WriteLine("VersiónLazaro=" + backupInfo.ProgramVersion); Escribidor.WriteLine(""); Escribidor.WriteLine("Por favor no modifique ni elimine este archivo."); Escribidor.Close(); Archivo.Close(); } if (Lfx.Workspace.Master.CurrentConfig.ReadGlobalSetting<int>("Sistema.ComprimirCopiasDeSeguridad", 0) != 0) { Progreso.ChangeStatus("Comprimiendo los datos"); Lfx.FileFormats.Compression.Archive ArchivoComprimido = new Lfx.FileFormats.Compression.Archive(Lfx.Environment.Folders.TemporaryFolder + WorkFolder + "backup.7z"); ArchivoComprimido.Add(Lfx.Environment.Folders.TemporaryFolder + WorkFolder + "*"); if (System.IO.File.Exists(Lfx.Environment.Folders.TemporaryFolder + WorkFolder + "backup.7z")) { Progreso.ChangeStatus("Eliminando archivos temporales"); // Borrar los archivos que acabo de comprimir System.IO.DirectoryInfo Dir = new System.IO.DirectoryInfo(Lfx.Environment.Folders.TemporaryFolder + WorkFolder); foreach (System.IO.FileInfo DirItem in Dir.GetFiles()) { if (DirItem.Name != "backup.7z" && DirItem.Name != "info.txt") { System.IO.File.Delete(Lfx.Environment.Folders.TemporaryFolder + WorkFolder + DirItem.Name); } } } } Progreso.ChangeStatus("Almacenando"); Progreso.ChangeStatus(Progreso.Value + 1); Lfx.Environment.Folders.MoveDirectory(Lfx.Environment.Folders.TemporaryFolder + WorkFolder, this.BackupPath + WorkFolder); int GuardarBackups = Lfx.Workspace.Master.CurrentConfig.ReadGlobalSetting<int>("Sisteam.Backup.CantMax", 14); if (GuardarBackups > 0) { List<BackupInfo> ListaDeBackups = this.GetBackups(); if (ListaDeBackups.Count > GuardarBackups) { Progreso.ChangeStatus("Eliminando copias de seguridad antiguas"); int BorrarBackups = ListaDeBackups.Count - GuardarBackups; if (BorrarBackups < ListaDeBackups.Count) { for (int i = 1; i <= BorrarBackups; i++) { this.Delete(this.GetOldestBackupName()); } } } } Progreso.End(); return new Lfx.Types.SuccessOperationResult(); }
/// <summary> /// Exporta una tabla en un formato binario propietario, incluyendo BLOBs. /// </summary> public void ExportTableBin(string nombreTabla, BackupWriter writer) { qGen.Select Comando = new qGen.Select(nombreTabla); System.Data.DataTable TablaBackup = Lfx.Workspace.Master.MasterConnection.Select(Comando); bool EmitiTabla = false; string[] Fields = null; foreach (System.Data.DataRow RegistroBackup in TablaBackup.Rows) { if (EmitiTabla == false) { Fields = new string[TablaBackup.Columns.Count]; for (int i = 0; i < TablaBackup.Columns.Count; i++) { Fields[i] = TablaBackup.Columns[i].ColumnName; } string FieldList = string.Join(",", Fields); writer.Write(":TBL" + Comando.Tables[0].Name.Length.ToString("0000") + Comando.Tables[0].Name); writer.Write(":FDL" + FieldList.Length.ToString("0000") + FieldList); EmitiTabla = true; } writer.Write(":ROW"); for (int i = 0; i < TablaBackup.Columns.Count; i++) { object ValorOrigen = RegistroBackup[Fields[i]]; string TipoCampoOrigen = ValorOrigen.GetType().ToString().Replace("System.", ""); string TipoCampoDestino; switch (TipoCampoOrigen) { case "Byte[]": TipoCampoDestino = "B"; break; case "SByte": case "Byte": case "Int16": case "Int32": case "Int64": ValorOrigen = ValorOrigen.ToString(); TipoCampoDestino = "I"; break; case "Single": case "Double": double ValDouble = System.Convert.ToDouble(ValorOrigen); ValorOrigen = ValDouble.ToString(System.Globalization.CultureInfo.InvariantCulture); TipoCampoDestino = "N"; break; case "Decimal": decimal ValDecimal = System.Convert.ToDecimal(ValorOrigen); ValorOrigen = ValDecimal.ToString(System.Globalization.CultureInfo.InvariantCulture); TipoCampoDestino = "N"; break; case "DateTime": ValorOrigen = ((DateTime)ValorOrigen).ToString(Lfx.Types.Formatting.DateTime.SqlDateTimeFormat); TipoCampoDestino = "D"; break; case "String": TipoCampoDestino = "S"; break; case "DBNull": TipoCampoDestino = "U"; ValorOrigen = ""; break; default: TipoCampoDestino = "S"; ValorOrigen = ValorOrigen.ToString(); break; } byte[] ValorDestino; if (ValorOrigen is byte[]) { ValorDestino = (byte[])ValorOrigen; } else if (ValorOrigen is string) { ValorDestino = System.Text.Encoding.UTF8.GetBytes((string)ValorOrigen); } else { throw new NotImplementedException(); } writer.Write(":FLD" + TipoCampoDestino + ValorDestino.Length.ToString("00000000")); if (ValorDestino.Length > 0) { writer.Write(ValorDestino); } } writer.Write(".ROW"); //writer.Write(":REM0002" + Lfx.Types.ControlChars.CrLf); } writer.Write(".TBL"); }
/// <summary> /// Exporta una tabla en un formato binario propietario, incluyendo BLOBs. /// </summary> public void ExportTableBin(string nombreTabla, BackupWriter writer) { qGen.Select Comando = new qGen.Select(nombreTabla); System.Data.DataTable TablaBackup = Lfx.Workspace.Master.MasterConnection.Select(Comando); bool EmitiTabla = false; string[] Fields = null; foreach (System.Data.DataRow RegistroBackup in TablaBackup.Rows) { if (EmitiTabla == false) { Fields = new string[TablaBackup.Columns.Count]; for (int i = 0; i < TablaBackup.Columns.Count; i++) { Fields[i] = TablaBackup.Columns[i].ColumnName; } string FieldList = string.Join(",", Fields); writer.Write(":TBL" + Comando.Tables.Length.ToString("0000") + Comando.Tables); writer.Write(":FDL" + FieldList.Length.ToString("0000") + FieldList); EmitiTabla = true; } writer.Write(":ROW"); for (int i = 0; i < TablaBackup.Columns.Count; i++) { object ValorOrigen = RegistroBackup[Fields[i]]; string TipoCampoOrigen = ValorOrigen.GetType().ToString().Replace("System.", ""); string TipoCampoDestino; switch (TipoCampoOrigen) { case "Byte[]": TipoCampoDestino = "B"; break; case "SByte": case "Byte": case "Int16": case "Int32": case "Int64": ValorOrigen = ValorOrigen.ToString(); TipoCampoDestino = "I"; break; case "Single": case "Double": double ValDouble = System.Convert.ToDouble(ValorOrigen); ValorOrigen = ValDouble.ToString(System.Globalization.CultureInfo.InvariantCulture); TipoCampoDestino = "N"; break; case "Decimal": decimal ValDecimal = System.Convert.ToDecimal(ValorOrigen); ValorOrigen = ValDecimal.ToString(System.Globalization.CultureInfo.InvariantCulture); TipoCampoDestino = "N"; break; case "DateTime": ValorOrigen = ((DateTime)ValorOrigen).ToString(Lfx.Types.Formatting.DateTime.SqlDateTimeFormat); TipoCampoDestino = "D"; break; case "String": TipoCampoDestino = "S"; break; case "DBNull": TipoCampoDestino = "U"; ValorOrigen = ""; break; default: TipoCampoDestino = "S"; ValorOrigen = ValorOrigen.ToString(); break; } byte[] ValorDestino; if (ValorOrigen is byte[]) ValorDestino = (byte[])ValorOrigen; else if (ValorOrigen is string) ValorDestino = System.Text.Encoding.UTF8.GetBytes((string)ValorOrigen); else throw new NotImplementedException(); writer.Write(":FLD" + TipoCampoDestino + ValorDestino.Length.ToString("00000000")); if (ValorDestino.Length > 0) writer.Write(ValorDestino); } writer.Write(".ROW"); //writer.Write(":REM0002" + Lfx.Types.ControlChars.CrLf); } writer.Write(".TBL"); }