rhevUP.SqlOperations.backupDatabases C# (CSharp) Method

backupDatabases() public method

public backupDatabases ( string pathBackupDB, string sqlServerName ) : int
pathBackupDB string
sqlServerName string
return int
        public int backupDatabases(string pathBackupDB, string sqlServerName)
        {
            SqlConnection thisConnection = new SqlConnection(@"Server=" + sqlServerName + ";Integrated Security=True");

            try
            {
                thisConnection.Open();
            }
            catch (Exception e)
            {
                Console.WriteLine("Unable to connect to SQL Server!!");
                Console.WriteLine("Are you using the correct SQL ServerName?");
                Console.WriteLine("{0} Exception caught.", e);
                Environment.Exit(-1);
            }

            string DB1 = pathBackupDB + @"\rhevm.bak";
            string DB2 = pathBackupDB + @"\rhevm_history.bak";

            SqlCommand thisCommand = thisConnection.CreateCommand();

            thisCommand.CommandText = @"backup database rhevm to disk='" + DB1 + @"'";
            try
            {
                thisCommand.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine("Cannot proceed with backup rhevm database, please check the following:");
                Console.WriteLine("* Are you using the correct SQL ServerName?");
                Console.WriteLine("* Any problem/timeout with SQL Server?");
                Console.WriteLine("* Make sure no application are using rhevm database at this moment");
                Console.WriteLine("Example: SQL Server Management software, RHEV Configurator, etc.");
                Console.WriteLine("{0} Exception caught.", e);
                Environment.Exit(-1);
            }

            /* if file exists, backup OK */
            if (!File.Exists(DB1))
            {
                Console.WriteLine("Unable to backup rhevm_history cannot continue!");
                return -1;
            }

            /* rhevm_history */
            thisCommand.CommandText = @"backup database rhevm_history to disk='" + DB2 + @"'";
            try
            {
                thisCommand.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                Console.WriteLine("Cannot proceed with backup rhevm database, please check the following:");
                Console.WriteLine("* Are you using the correct SQL ServerName?");
                Console.WriteLine("* Any problem/timeout with SQL Server?");
                Console.WriteLine("* Make sure no application are using rhevm database at this moment");
                Console.WriteLine("Example: SQL Server Management software, RHEV Configurator, etc.");
                Console.WriteLine("{0} Exception caught.", e);
                Environment.Exit(-1);
            }
            thisConnection.Close();

            /* if file exists, backup OK */
            if (!File.Exists(DB2))
            {
                Console.WriteLine("Unable to backup rhevm_history, cannot continue!");
                return -1;
            }
            return 0;
        }

Usage Example

Exemplo n.º 1
0
        public int backupRHEV(string sqlServerName, string destDir, bool quitFlag, string inetpubStr, string rhevpath)
        {
            fileOperations fop             = new fileOperations();
            string         PATH_BACKUP_DIR = destDir;


            DirectoryInfo DirRhevPath = new DirectoryInfo(rhevpath);

            if (DirRhevPath.Exists == false)
            {
                Console.WriteLine("Cannot proceed, RHEV Path " + rhevpath + " doesn't exist!");
                Environment.Exit(-1);
            }

            DirectoryInfo DirInetPub = new DirectoryInfo(inetpubStr);

            if (DirInetPub.Exists == false)
            {
                Console.WriteLine("Cannot proceed, inetpub path " + inetpubStr + " doesn't exist!");
                Environment.Exit(-1);
            }

            /* Check if the backup directory already exists, if yes add to the path Date */
            DirectoryInfo DirInfo = new DirectoryInfo(PATH_BACKUP_DIR);

            if (DirInfo.Exists == true)
            {
                DateTime datenow = DateTime.Now;
                PATH_BACKUP_DIR += datenow.ToString("yyyy-MM-dd_HHmmss");
            }

            string PATH_BACKUP_DB = PATH_BACKUP_DIR + @"\sqlBackup";

            // service
            string PATH_BACKUP_SERVICE    = PATH_BACKUP_DIR + @"\Service\";
            string PATH_BACKUP_SERVICE_CA = PATH_BACKUP_DIR + @"\Service\ca\";

            // inetpub
            string PATH_INETPUB        = inetpubStr;
            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\";

            string PATH_SERVICE_CA_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);
            }

            Console.WriteLine("========================================");
            Console.WriteLine("RHEVUP - " + VERSION_RHEV_UPDATE);
            Console.WriteLine("========================================\n");

            /* Creating initial dirs */
            fop.createDir(PATH_BACKUP_DIR);
            fop.createDir(PATH_BACKUP_DB);
            fop.createDir(PATH_BACKUP_SERVICE_CA);
            fop.createDir(PATH_BACKUP_CERTS_AUTH);
            fop.createDir(PATH_BACKUP_CERTS_PUB);
            fop.createDir(PATH_BACKUP_CERTS_PERSONAL);
            fop.createDir(PATH_BACKUP_INETPUB);

            ///////////////////////////////// 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 /////////////////////////////////////////////
            //////////////////////// BACKUP DATABASES ////////////////////////////////////////////
            //////////////////////////////////////////////////////////////////////////////////////
            /* backup rhevm databases */
            Console.WriteLine("Phase 2: backup rhevm databases");
            SqlOperations sql = new SqlOperations();

            ret = sql.backupDatabases(PATH_BACKUP_DB, sqlServerName);
            if (ret == -1)
            {
                Console.WriteLine("Phase 2: Failed\n");
                Environment.Exit(-1);
            }
            Console.WriteLine("Phase 2: Done\n");

            ///////////////////////////////// STEP 3 /////////////////////////////////////////////
            //////////////////////////////////////////////////////////////////////////////////////

            /* STEP 3 */
            /* backup service - CA - directory */
            Console.WriteLine("Phase 3: backup service CA directory");
            DirectoryInfo src  = new DirectoryInfo(PATH_SERVICE_CA_PROGRAM_FILES);
            DirectoryInfo dest = new DirectoryInfo(PATH_BACKUP_SERVICE_CA);

            ret = fop.backupServiceCA_DIR(src, dest);
            if (ret == -1)
            {
                Console.WriteLine("Phase 3: Failed\n");
                Environment.Exit(-1);
            }
            Console.WriteLine("Phase 3: Done\n");

            ///////////////////////////////// STEP 4 /////////////////////////////////////////////
            //////////////////////////////////////////////////////////////////////////////////////
            /* backup - rhevm.pfx and rhevm.ssh.key files*/
            Console.WriteLine("Phase 4: backup rhevm.pfx and rhevm.ssh.key");
            ret = fop.copyFile(PATH_SERVICE_PROGRAM_FILES + @"rhevm.pfx", PATH_BACKUP_SERVICE + @"rhevm.pfx");
            if (ret == -1)
            {
                Console.WriteLine("From:" + PATH_SERVICE_PROGRAM_FILES + @"rhevm.pfx");
                Console.WriteLine("To:" + PATH_BACKUP_SERVICE + @"rhevm.pfx");
                Console.WriteLine("Phase 4: Failed\n");
                Environment.Exit(-1);
            }
            Console.WriteLine("Copied rhevm.pfx");

            /* rhevm.ssh.key */
            ret = fop.copyFile(PATH_SERVICE_PROGRAM_FILES + @"rhevm.ssh.key", PATH_BACKUP_SERVICE + @"rhevm.ssh.key");
            if (ret == -1)
            {
                Console.WriteLine("From:" + PATH_SERVICE_PROGRAM_FILES + @"rhevm.ssh.key");
                Console.WriteLine("To:" + PATH_BACKUP_SERVICE + @"rhevm.ssh.key");
                Console.WriteLine("Phase 4: Failed\n");
                Environment.Exit(-1);
            }
            Console.WriteLine("Copied rhevm.ssh.key");
            Console.WriteLine("Phase 4: Done\n");

            ///////////////////////////////// STEP 5 /////////////////////////////////////////////
            //////////////////////////////////////////////////////////////////////////////////////
            /* backup - RHEVManager.exe.config RHEVMHistoryService.exe.config RHEVMNotificationService.exe.config*/

            Console.WriteLine("Phase 5: backup RHEVM*.config files");


            ret = fop.copyFile(PATH_SERVICE_PROGRAM_FILES + @"RHEVManager.exe.config", PATH_BACKUP_SERVICE + @"RHEVManager.exe.config");
            if (ret == -1)
            {
                Console.WriteLine("Phase 5: Failed - File: RHEVManager.exe.config\n");
                Environment.Exit(-1);
            }
            Console.WriteLine("Copied RHEVManager.exe.config");

            // Backup FieldsInVDCConfig.xml file
            ret = fop.copyFile(PATH_SERVICE_PROGRAM_FILES + @"FieldsInVDCConfig.xml", PATH_BACKUP_SERVICE + @"FieldsInVDCConfig.xml");
            if (ret == -1)
            {
                Console.WriteLine("Phase 5: Failed - File: FieldsInVDCConfig.xml\n");
                Environment.Exit(-1);
            }
            Console.WriteLine("Copied FieldsInVDCConfig.xml");

            ret = fop.copyFile(PATH_SERVICE_PROGRAM_FILES + @"RHEVMHistoryService.exe.config", PATH_BACKUP_SERVICE + @"RHEVMHistoryService.exe.config");
            if (ret == -1)
            {
                Console.WriteLine("Phase 5: Failed - File: RHEVMHistoryService.exe.config\n");
                Environment.Exit(-1);
            }
            Console.WriteLine("Copied RHEVMHistoryService.exe.config");

            ret = fop.copyFile(PATH_SERVICE_PROGRAM_FILES + @"RHEVMNotificationService.exe.config", PATH_BACKUP_SERVICE + @"RHEVMNotificationService.exe.config");
            if (ret == -1)
            {
                Console.WriteLine("Phase 5: Failed - File: RHEVMNotificationService.exe.config\n");
                Environment.Exit(-1);
            }
            Console.WriteLine("Copied RHEVMNotificationService.exe.config");
            Console.WriteLine("Phase 5: Done\n");

            ///////////////////////////////// STEP 6 /////////////////////////////////////////////
            //////////////////////////////////////////////////////////////////////////////////////

            /* backup inetpub files */
            Console.WriteLine("Phase 6: backup inetpub files");

            ret = fop.copyFile(PATH_INETPUB + @"\ca.crt", PATH_BACKUP_INETPUB + @"\ca.crt");
            if (ret == -1)
            {
                Console.WriteLine("Phase 6: Failed - cannot copy ca.crt from" + PATH_INETPUB + "\n");
                Environment.Exit(-1);
            }

            ret = fop.copyFile(PATH_INETPUB + @"\rhevm.ssh.key.txt", PATH_BACKUP_INETPUB + @"\rhevm.ssh.key.txt");
            if (ret == -1)
            {
                Console.WriteLine("Phase 6: Failed - cannot copy rhevm.ssh.key.txt from" + PATH_INETPUB + "\n");
                Environment.Exit(-1);
            }
            Console.WriteLine("Copied ca.crt rhevm.ssh.key.txt from inetpub");
            Console.WriteLine("Phase 6: Done\n");

            ///////////////////////////////// STEP 7 /////////////////////////////////////////////

            /* Copy .certs from Red Hat */
            Console.WriteLine("Phase 7: Getting certificates");
            certOperations cert = new certOperations();

            cert.get_Trusted_Root_Certificate_Authorities(PATH_BACKUP_CERTS_AUTH);
            cert.get_Trusted_Publishers(PATH_BACKUP_CERTS_PUB);
            cert.get_Personal_certs(PATH_BACKUP_CERTS_PERSONAL);
            Console.WriteLine("Phase 7: Done\n");



            ///////////////////////////////// STEP 7 /////////////////////////////////////////////
            //////////////////////////////////////////////////////////////////////////////////////
            /* shutdown the system */
            systemOperations  sys  = new systemOperations();
            serviceOperations serv = new serviceOperations();

            if (quitFlag == false)
            {
                Console.WriteLine("Please copy the path " + PATH_BACKUP_DIR + " to a CDROM/USB device to move to the new RHEV Server.");
                Console.WriteLine("\nAfter that, we need to *HALT* THIS COMPUTER to avoid any conflicts like DNS.");
                Console.WriteLine("Press y when you get ready to TURN OFF the entire system");
                char character = (char)Console.Read();

                if (character.Equals('y'))
                {
                    sys.Shutdown();
                }
                else
                {
                    Console.WriteLine("\nOk, I will *NOT* halt this machine now, aborting halt..");
                    Console.WriteLine("SUCCESS!");
                    return(0);
                }
            }
            serv.StartService("RHEV Manager");
            serv.StartService("RHEVM History Service");
            serv.StartService("RHEVM Net Console");
            serv.StartService("RHEVM Notification Service");
            Console.WriteLine("SUCCESS!");
            return(0);
        }
All Usage Examples Of rhevUP.SqlOperations::backupDatabases