private static void ReadConfig(FileInfo confFileInfo) { GC.Collect(); using (FileStream fs = confFileInfo.OpenRead()) { XmlReader confReader = XmlReader.Create(fs); XmlSerializer ser = new XmlSerializer(typeof(AppConfigData)); ConfigData = (AppConfigData)ser.Deserialize(confReader); } }
internal static void CreateDirectories(AppConfigData conf) { foreach (string path in new string[] { conf.TargetDirectory, conf.TempDirectory }) { try { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } } catch { } } foreach (String s in new String[] { "Files", "Directories", "Databases", "Execs" }) { string fullPath = Path.Combine(conf.TempDirectory, s); try { Directory.CreateDirectory(fullPath); } catch { } } }
/// <summary> /// Create sample config file /// </summary> /// <param name="confFileInfo"></param> private static void CreateConfig(FileInfo confFileInfo) { Console.WriteLine("Creating sample config file..."); AppConfigData acd = new AppConfigData(); acd.TargetDirectory = @"\\192.168.0.10\Temp\kur4"; // Exec commands acd.Execs.Add(new Items.ItemExec() { CaptureOutput = true, Command = "dir", WorkingDirectory = "c:\\" }); acd.Execs.Add(new Items.ItemExec() { CaptureOutput = false, Command = "ping 127.0.0.1", WorkingDirectory = null }); acd.Execs.Add(new Items.ItemExec() { CaptureOutput = true, Command = "ping 8.8.8.8", WorkingDirectory = null, Stage = Items.ItemExec.RunStage.Before }); acd.Execs.Add(new Items.ItemExec() { CaptureOutput = true, Command = "ping 8.8.8.8", WorkingDirectory = null, Stage = Items.ItemExec.RunStage.After }); // Backup directories acd.Directories.Add(new Items.ItemDirectory(@"c:\Windows")); acd.Directories.Add(new Items.ItemDirectory() { FullPath = @"c:\Program Files (x86)", Compress = true }); // Backup files acd.Files.Add(new Items.ItemFile(@"c:\autoexec.bat")); // MS SQL acd.Database.Username = "******"; //acd.Database.Password = "******"; acd.Database.Databases.Add("hMailServer"); acd.Database.Databases.Add("tfs123"); XmlSerializer ser = new XmlSerializer(typeof(AppConfigData)); // Xml Settings XmlWriterSettings settings = new XmlWriterSettings(); settings.Encoding = Encoding.UTF8; settings.Indent = true; settings.NewLineOnAttributes = false; // Write to file using (FileStream confFileStream = confFileInfo.Create()) { using (XmlWriter confWriter = XmlWriter.Create(confFileStream, settings)) { ser.Serialize(confWriter, acd); confWriter.Flush(); } } // Exit Console.WriteLine("Press any key to continue, or any other key to quit."); Console.ReadKey(true); Console.WriteLine("Not that key. Press another one or this one again!"); Console.ReadKey(true); Environment.Exit(0); }
static void Main(string[] args) { AppConfigData conf = AppConfigWrapper.GetConfig(); if (conf.ThisIsSample) { Console.WriteLine("This is the example configuration. Nothing will be backed up now."); Console.ReadKey(true); Environment.Exit(0); } Tools.CreateDirectories(conf); // Copy directories conf.Directories.ForEach(d => { DirectoryInfo sourceDirInfo = new DirectoryInfo(d.FullPath); if (d.Compress) { ZipFile.CreateFromDirectory(d.FullPath, Path.Combine(conf.TempDirectory, "Directories", sourceDirInfo.Name + ".zip")); } else { Tools.CopyDirectory(d.FullPath, Path.Combine(conf.TempDirectory, "Directories", sourceDirInfo.Name)); } }); // Backup databases if (conf.Database.Databases.Any()) { // Allow anyone to write here or mssql will not be able to dump the database DirectoryInfo dbDir = new DirectoryInfo(Path.Combine(conf.TempDirectory, "Databases")); FileSystemAccessRule fsar = new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow); DirectorySecurity dsec = dbDir.GetAccessControl(); dsec.AddAccessRule(fsar); dbDir.SetAccessControl(dsec); conf.Database.Databases.ForEach(db => { StringBuilder sqlCmdText = new StringBuilder(); sqlCmdText.Append("BACKUP DATABASE ["); sqlCmdText.Append(db); sqlCmdText.Append("] TO DISK = N'"); sqlCmdText.Append(Path.Combine(conf.TempDirectory, "Databases", db + ".bak")); sqlCmdText.Append("' WITH NOFORMAT, NOINIT, NAME = N'"); sqlCmdText.AppendFormat("{0} - Backup'", db); sqlCmdText.Append(", SKIP, NOREWIND, NOUNLOAD, STATS = 10"); using (SqlConnection sql = new SqlConnection(Tools.CreateConnectionString(db, conf.Database.Username, conf.Database.Password))) { sql.Open(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = sqlCmdText.ToString(); cmd.CommandTimeout = 1800; cmd.Connection = sql; cmd.ExecuteNonQuery(); sql.Close(); } }); } // Copy to destination if (conf.Compress) { ZipFile.CreateFromDirectory(conf.TempDirectory, Path.Combine(conf.TargetDirectory, Tools.CreateTargetName() + ".zip")); } else { Tools.CopyDirectory(conf.TempDirectory, Path.Combine(conf.TargetDirectory, Tools.CreateTargetName())); } // Delete temp files GC.Collect(); System.Threading.Thread.Sleep(500); try { Directory.Delete(conf.TempDirectory, true); } catch (Exception any) { Console.WriteLine("Error: {0}", any.Message); } }