public void restoreRHEV(string sqlServerName, string sourceDir, string inetpubDir, string rhevpath) { string PATH_BACKUP_DIR = sourceDir; string PATH_BACKUP_DB = PATH_BACKUP_DIR + @"\sqlBackup"; string PATH_BACKUP_SERVICE = PATH_BACKUP_DIR + @"\Service\"; string PATH_BACKUP_SERVICE_CA = PATH_BACKUP_DIR + @"\Service\ca\"; string PATH_BACKUP_SERVICE_PRIVATE_CA = PATH_BACKUP_DIR + @"\Service\ca\private\"; // inetpub string PATH_INETPUB = inetpubDir; string PATH_BACKUP_INETPUB = PATH_BACKUP_DIR + @"\inetpub\"; // CERTS string PATH_BACKUP_CERTS_AUTH = PATH_BACKUP_DIR + @"\Certificates\TrustedRootCertificatesAuthorities\"; string PATH_BACKUP_CERTS_PUB = PATH_BACKUP_DIR + @"\Certificates\TrustedPublishers\"; string PATH_BACKUP_CERTS_PERSONAL = PATH_BACKUP_DIR + @"\Certificates\Personal\"; // Service string PATH_SERVICE_CA_PROGRAM_FILES = ""; string PATH_SERVICE_CA_PRIVATE_PROGRAM_FILES = ""; string PATH_SERVICE_PROGRAM_FILES = ""; int ret; PATH_SERVICE_CA_PROGRAM_FILES = rhevpath + @"\Service\ca\"; DirectoryInfo DirCA = new DirectoryInfo(rhevpath); if (DirCA.Exists == false) { Console.WriteLine("Cannot locate path: " + PATH_SERVICE_CA_PROGRAM_FILES + ", aborting.."); Environment.Exit(-1); } PATH_SERVICE_PROGRAM_FILES = rhevpath + @"\Service\"; DirectoryInfo DirPFILES = new DirectoryInfo(rhevpath); if (DirPFILES.Exists == false) { Console.WriteLine("Cannot locate path: " + PATH_SERVICE_PROGRAM_FILES + ", aborting.."); Environment.Exit(-1); } PATH_SERVICE_CA_PRIVATE_PROGRAM_FILES = PATH_SERVICE_CA_PROGRAM_FILES + @"\private\"; Console.WriteLine("========================================"); Console.WriteLine("RHEVUP - " + VERSION_RHEV_UPDATE); Console.WriteLine("========================================\n"); if (!Directory.Exists(sourceDir)) { Console.WriteLine("source dir " + sourceDir + " doesn't exit!"); Console.WriteLine("Aborting..."); Environment.Exit(-1); } ///////////////////////////////// STEP 1 ///////////////////////////////////////////// //////////////////////// SHUTDOWN WINDOWS SERVICES /////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////// /* Shutdown all RHEV Windows Services */ Console.WriteLine("Phase 1: Stopping services"); serviceOperations services = new serviceOperations(); services.StopService("RHEV Manager", 15000); services.StopService("RHEVM History Service", 15000); services.StopService("RHEVM Net Console", 15000); services.StopService("RHEVM Notification Service", 15000); Console.WriteLine("Phase 1: Done\n"); ///////////////////////////////// STEP 2 ///////////////////////////////////////////// //////////////////////// RESTORE DATABASES /////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////// Console.WriteLine("Phase 2: Restore rhev databases"); SqlOperations sql = new SqlOperations(); ret = sql.restoreDatabases(PATH_BACKUP_DB, sqlServerName); if (ret == -1) { Console.WriteLine("Phase 2: Failed\n"); Console.ReadLine(); Environment.Exit(-1); } sql.setUserPermissionLogin(sqlServerName); //Console.ReadLine(); Console.WriteLine("Phase 2: Done\n"); ///////////////////////////////// STEP 3 ///////////////////////////////////////////// ////////////////// Add FULL CONTROL PERMISSION TO Service\ca\rhevm.pfx ////////////// ////////////////////////////////////////////////////////////////////////////////////// //File.Copy((PATH_BACKUP_SERVICE + @"rhevm.pfx"), (PATH_SERVICE_PROGRAM_FILES + @"rhevm.pfx"), true); string currTime = DateTime.Now.ToString("MM-dd-yyyy-hh-mm-ss"); Console.WriteLine("Phase 3: backup current rhevm.pfx, ca.pem and restore the original"); // restore c:\Program Files (x86)\RHEVManager\Service\ca\ca.pem if (File.Exists(PATH_SERVICE_CA_PROGRAM_FILES + @"ca.pem")) { Console.WriteLine("Backuping " + (PATH_SERVICE_CA_PROGRAM_FILES + "ca.pem")); File.Copy((PATH_SERVICE_CA_PROGRAM_FILES + @"ca.pem"), (PATH_SERVICE_CA_PROGRAM_FILES + @"ca.pem" + currTime)); } if (!File.Exists(PATH_BACKUP_SERVICE_CA + @"ca.pem")) { Console.WriteLine("Unable to locate original " + PATH_BACKUP_SERVICE_CA + @"ca.pem"); Console.WriteLine("Phase 3: Failed\n"); Console.WriteLine("Aborting..."); Environment.Exit(-1); } File.Delete(PATH_SERVICE_CA_PROGRAM_FILES + @"ca.pem"); File.Copy((PATH_BACKUP_SERVICE_CA + @"ca.pem"), (PATH_SERVICE_CA_PROGRAM_FILES + @"ca.pem")); // restore c:\Program Files (x86)\RHEVManager\Service\ca\private\ca.pem if (File.Exists(PATH_SERVICE_CA_PRIVATE_PROGRAM_FILES + @"ca.pem")) { Console.WriteLine("Backuping " + (PATH_SERVICE_CA_PRIVATE_PROGRAM_FILES + "ca.pem")); File.Copy((PATH_SERVICE_CA_PRIVATE_PROGRAM_FILES + @"ca.pem"), (PATH_SERVICE_CA_PRIVATE_PROGRAM_FILES + @"ca.pem" + currTime)); } if (!File.Exists(PATH_BACKUP_SERVICE_PRIVATE_CA + @"ca.pem")) { Console.WriteLine("Unable to locate original " + PATH_BACKUP_SERVICE_PRIVATE_CA + @"ca.pem"); Console.WriteLine("Phase 3: Failed\n"); Console.WriteLine("Aborting..."); Environment.Exit(-1); } File.Delete(PATH_SERVICE_CA_PRIVATE_PROGRAM_FILES + @"ca.pem"); File.Copy((PATH_BACKUP_SERVICE_PRIVATE_CA + @"ca.pem"), (PATH_SERVICE_CA_PRIVATE_PROGRAM_FILES + @"ca.pem")); // restore inetpub - ca.crt // PATH_INETPUB = @"c:\inetpub\wwwroot\"; // PATH_BACKUP_INETPUB = PATH_BACKUP_DIR + @"\inetpub"; if (File.Exists(PATH_INETPUB + @"ca.crt")) { Console.WriteLine("Backuping " + (PATH_INETPUB + "ca.crt")); File.Copy((PATH_INETPUB + @"ca.crt"), (PATH_INETPUB + @"ca.crt" + currTime)); } if (!File.Exists(PATH_BACKUP_INETPUB + @"ca.crt")) { Console.WriteLine("Unable to locate original " + PATH_BACKUP_INETPUB + @"ca.crt"); Console.WriteLine("Phase 3: Failed\n"); Console.WriteLine("Aborting..."); Environment.Exit(-1); } File.Delete(PATH_INETPUB + @"ca.crt"); File.Copy((PATH_BACKUP_INETPUB + @"ca.crt"), (PATH_INETPUB + @"ca.crt")); // rhevm.ssh.key.txt if (File.Exists(PATH_INETPUB + @"rhevm.ssh.key.txt")) { Console.WriteLine("Backuping " + (PATH_INETPUB + "rhevm.ssh.key.txt")); File.Copy((PATH_INETPUB + @"rhevm.ssh.key.txt"), (PATH_INETPUB + @"rhevm.ssh.key.txt" + currTime)); } if (!File.Exists(PATH_BACKUP_INETPUB + @"rhevm.ssh.key.txt")) { Console.WriteLine("Unable to locate original " + PATH_BACKUP_INETPUB + @"rhevm.ssh.key.txt"); Console.WriteLine("Phase 3: Failed\n"); Console.WriteLine("Aborting..."); Environment.Exit(-1); } File.Delete(PATH_INETPUB + @"rhevm.ssh.key.txt"); File.Copy((PATH_BACKUP_INETPUB + @"rhevm.ssh.key.txt"), (PATH_INETPUB + @"rhevm.ssh.key.txt")); // backup current .pfx file if (File.Exists(PATH_SERVICE_PROGRAM_FILES + @"rhevm.pfx")) { Console.WriteLine("\nBackuping current rhevm.pfx"); Console.WriteLine("Path: " + (PATH_SERVICE_PROGRAM_FILES + @"rhevm.pfx")); File.Copy((PATH_SERVICE_PROGRAM_FILES + @"rhevm.pfx"), (PATH_SERVICE_PROGRAM_FILES + @"bkp.rhevm.pfx-" + currTime)); } // copying the "new (previous/old)" rhevm.pfx to Services directory // first, verify... if (!File.Exists(PATH_BACKUP_SERVICE + @"rhevm.pfx")) { Console.WriteLine("Unable to locate original rhevm.pfx"); Console.WriteLine("Phase 3: Failed\n"); Console.WriteLine("Aborting..."); Environment.Exit(-1); } File.Delete(PATH_SERVICE_PROGRAM_FILES + @"rhevm.pfx"); Console.WriteLine("\nCopying original rhevm.pfx to " + PATH_SERVICE_PROGRAM_FILES); File.Copy((PATH_BACKUP_SERVICE + @"rhevm.pfx"), (PATH_SERVICE_PROGRAM_FILES + @"rhevm.pfx"), true); // Setting perrmission to new .pfx Console.WriteLine("Setting permission (NETWORK SERVICE - FULL CONTROL) to rhevm.pfx"); string setPerFile = (PATH_SERVICE_PROGRAM_FILES + @"rhevm.pfx"); FileSecurity fileSecurity = File.GetAccessControl(setPerFile); fileSecurity.AddAccessRule(new FileSystemAccessRule("NETWORK SERVICE", FileSystemRights.FullControl, AccessControlType.Allow)); File.SetAccessControl(setPerFile, fileSecurity); //FileSecurity fileSecurity = File.GetAccessControl(setPerFile); fileSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow)); fileSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow)); File.SetAccessControl(setPerFile, fileSecurity); Console.WriteLine("\nPhase 3: Done\n"); ///////////////////////////////// STEP 4 //////////////////////////////////////////// ////////////////////////////// RESTORE CERTS ////////////////////////////////////// //////////;//////////////// CERTS Folders backup //////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////// ///////////////////// Personal - LocalMachine ///////////////////////// ///////////////////// Trusted Publishers ///////////////////////// ///////////////////// Trusted Root Certificates Authorities ///////////////////////// ///////////////////////////////////////////////////////////////////////////////////// Console.WriteLine("Phase 4: Restore rhev certificates"); certOperations cert = new certOperations(); /* restore Trusted Publishers */ DirectoryInfo dirPub = new DirectoryInfo(PATH_BACKUP_CERTS_PUB); FileInfo[] FilesPub = dirPub.GetFiles("*.cer"); Console.WriteLine("Trusted Publishers - LocalMachine:"); foreach (FileInfo fi in FilesPub) { Console.WriteLine("Adding cert " + fi.Name); cert.addCertificateTrustedPublishers(PATH_BACKUP_CERTS_PUB + fi.Name); } Console.WriteLine("done\n"); /* restore Trusted Root Certificates Authorities */ DirectoryInfo dirAuth = new DirectoryInfo(PATH_BACKUP_CERTS_AUTH); FileInfo[] FilesAuth = dirAuth.GetFiles("*.cer"); Console.WriteLine("Trusted Root Certificates Authorities - LocalMachine"); foreach (FileInfo fi in FilesAuth) { Console.WriteLine("Adding cert " + fi.Name); cert.addCertificateTrustedRootCertificateAuthorities(PATH_BACKUP_CERTS_AUTH + fi.Name); } Console.WriteLine("done\n"); /* Restore .pfx */ Console.WriteLine("\nPFX - Personal - LocalMachine:"); Console.WriteLine("Adding rhevm.pfx cert"); cert.addPfxCertificate(PATH_SERVICE_PROGRAM_FILES + @"rhevm.pfx", "mypass"); Console.WriteLine("done\n"); Console.WriteLine("Phase 4: Done\n"); ///////////////////////////////// STEP 5 ///////////////////////////////////////////// ////////////////// Start RHEVM Services ///////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////// serviceOperations sOp = new serviceOperations(); /* Start all RHEV Windows Services */ Console.WriteLine("Phase 5: Starting services"); serviceOperations servStart = new serviceOperations(); servStart.StartService("RHEV Manager"); servStart.StartService("RHEVM History Service"); servStart.StartService("RHEVM Net Console"); servStart.StartService("RHEVM Notification Service"); Console.WriteLine("Phase 5: Done\n"); Console.WriteLine("Finished!"); }