public static string CreateBackupOfAllConfigurations(string directoryToSaveBackupTo) { string _itpipesDirectory = getITpipesInstallationDirectory(); if (_itpipesDirectory == null) { _itpipesDirectory = @"C:\Program Files\InspectIT"; } if (Directory.Exists(_itpipesDirectory) == false) { throw new FileNotFoundException(string.Format("Could not locate ITpipes installation directory: \"{0}\"", _itpipesDirectory)); } if (Directory.Exists(directoryToSaveBackupTo) == false) { Directory.CreateDirectory(directoryToSaveBackupTo); } //pre-backup processing: string setupMdb = Path.Combine(_itpipesDirectory, "setup.mdb"); if (File.Exists(setupMdb)) { UtilFunctions.copyFile(setupMdb, setupMdb + ".bak", true); } transferAddressBookLogosToLogosDirectory(_itpipesDirectory); transferTemplateLogosToLogosDirectory(Path.Combine(_itpipesDirectory, @"Templates")); string newBackupZipLocation = Path.Combine(directoryToSaveBackupTo, string.Format("ITpipes Config Backup - {0}.{1}", DateTime.Now.ToLongDateString(), BACKUP_FILE_EXTENSION)); int backupNum = 0; if (File.Exists(newBackupZipLocation)) { string tempFileName = new string(newBackupZipLocation.ToCharArray()); while (true) { backupNum++; tempFileName = appendNumberToExistingFileRecord(newBackupZipLocation, backupNum); if (File.Exists(tempFileName) == false) { newBackupZipLocation = tempFileName; break; } } } using (ZipFile backupZip = new ZipFile(newBackupZipLocation)) { foreach (string curDirectory in DIRECTORIES_TO_BACKUP) { if (Directory.Exists(curDirectory)) { addDirectoryToZipObject(backupZip, _itpipesDirectory, _itpipesDirectory + @"\" + curDirectory); } } foreach (string curFile in FILES_TO_BACKUP) { if (File.Exists(curFile)) { addFileToZipObjectIfPathExists(backupZip, _itpipesDirectory, _itpipesDirectory + @"\" + curFile); } } //dotnetzip doesn't handle its temp file's name already existing: if (File.Exists(newBackupZipLocation + ".tmp")) { File.Delete(newBackupZipLocation + ".tmp"); } if (Directory.Exists(Path.GetDirectoryName(newBackupZipLocation)) == false) { Directory.CreateDirectory(Path.GetDirectoryName(newBackupZipLocation)); } setBackupDirectoryAccessPermissions(directoryToSaveBackupTo); backupZip.Save(); } return(newBackupZipLocation); }
private static void transferTemplateLogosToLogosDirectory(string pathToItInstallDir) { string pathToTemplatesDir = Path.Combine(pathToItInstallDir, "Templates"); if (Directory.Exists(pathToTemplatesDir) == false) { return; } string tempTemplates = Path.Combine(Path.GetTempPath(), @"ITP_Temp_Templates"); if (Directory.Exists(tempTemplates)) { Directory.Delete(tempTemplates); } Directory.CreateDirectory(tempTemplates); foreach (string curTemplate in Directory.GetFiles(pathToTemplatesDir, "*.tpl", SearchOption.TopDirectoryOnly)) { string tempTplFilePath = Path.Combine(tempTemplates, Path.GetFileName(curTemplate)); UtilFunctions.copyFile(curTemplate, tempTplFilePath, true); using (ZipFile tplZip = new ZipFile(tempTplFilePath)) { tplZip.Encryption = EncryptionAlgorithm.PkzipWeak; tplZip.Password = "******"; tplZip.ExtractAll(tempTemplates, ExtractExistingFileAction.OverwriteSilently); string tempProjectDbPath = Path.Combine(tempTemplates, "project.mdb"); if (File.Exists(tempProjectDbPath) == false) { continue; } using (OleDbConnection curOleConn = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + tempProjectDbPath)) using (OleDbCommand curOleCommand = curOleConn.CreateCommand()) using (DataTable curAddressBook = new DataTable()) { curOleConn.Open(); curOleCommand.CommandText = "SELECT * FROM [Info]"; curAddressBook.Load(curOleCommand.ExecuteReader()); if (curAddressBook.Rows.Count == 0) { continue; } foreach (DataRow curRow in curAddressBook.Rows) { string relativePathToLogoFile = curRow["Logo"] == DBNull.Value ? null : (string)curRow["Logo"]; if (relativePathToLogoFile != null && File.Exists(Path.Combine(pathToItInstallDir, relativePathToLogoFile)) && curRow["Contact_Name"] != DBNull.Value) { string relativePathToNewLogoFile = copyTemplateLogoFromExistingLocation( relativePathToLogoFile, Path.Combine(pathToItInstallDir, "Logos"), Path.GetFileNameWithoutExtension(curTemplate), (string)curRow["Contact_Name"]); //Path.Combine(_itpipesInstallDir, // string.Format(@"Logos\{0}.{1}.bmp", // Path.GetFileNameWithoutExtension(curTemplate), // (string)curRow["Contact_Name"])); if (relativePathToNewLogoFile == relativePathToLogoFile) { continue; } curOleCommand.CommandText = "UPDATE [Info] SET [Logo] = ? WHERE [Info_ID] = ?"; curOleCommand.Parameters.Clear(); curOleCommand.Parameters.AddWithValue("Logo", relativePathToNewLogoFile); curOleCommand.Parameters.AddWithValue("Info_ID", (int)curRow["Info_ID"]); curOleCommand.ExecuteNonQuery(); } } ZipEntry projectZipEntry = null; foreach (ZipEntry curEntry in tplZip.Entries) { if (curEntry.FileName == "project.mdb") { projectZipEntry = curEntry; break; } } if (projectZipEntry != null) { tplZip.RemoveEntry(projectZipEntry); } else { throw new Exception(string.Format("Could not locate project.mdb in template: {0}", curTemplate)); } tplZip.AddFile(tempProjectDbPath, @"\"); foreach (ZipEntry curEntry in tplZip.Entries) { curEntry.Password = "******"; } try { if (File.Exists(curTemplate + ".bak")) { File.Delete(curTemplate + ".bak"); } File.Move(curTemplate, curTemplate + ".bak"); tplZip.Save(curTemplate); File.Delete(curTemplate + ".bak"); } catch { throw; } } } } try { Directory.Delete(tempTemplates, true); } catch { //Not gonna sweat it. } }
public static string CreateBackup() { //return value is the path to the backup file. return(ITP_Backup.CreateBackupOfAllConfigurations(UtilFunctions.getBackupDirectory())); }