Пример #1
0
        /// <summary>
        /// main mysql dump executor task
        /// start/running in ASYNC mode.
        /// internal its calling other task/jobs and waits for completion.
        /// with every task completed its firing notification events(listener)
        /// --BASIC FLOW--
        /// </summary>
        async void DumpMysqlTaskExecutor()
        {
            Task <List <string> > testConnectionTask = testCon();
            List <string>         tables             = await testConnectionTask;

            if (tables != null)
            {
                onProgress("connected");
                onInitDumpTables(tables);


                mydump = new MysqlDump();
                mydump.credentialsConfigInstance = credentialsConfigInstance;
                mydump.CompressProgress         += onCompressProgressHandler;
                mydump.CompressStart            += onCompressStartHandler;
                mydump.TableRowCount            += onTableRowCountHandler;
                mydump.TableStartDump           += onTableStartDumpHandler;

                Task <DumpResultSet> result     = dumptask(mydump);
                DumpResultSet        dumpresult = null;
                try
                {
                    dumpresult = await result;
                }
                catch (Exception ex)
                {
                    //xtipaei mysqlException alla edo den mpenei pote
                    Console.WriteLine("MYSQLEXCEPTION" + ex.Message);
                    if (!(ex is NullReferenceException))
                    {
                        dumpresult = new DumpResultSet();
                        dumpresult.wasSuccessful             = false;
                        dumpresult.errorNumber               = 2;
                        dumpresult.mysqldumpexeStandardError = "Error in MySQLDumpAdapter:\n" + ex.Message;
                        onCompleted(dumpresult);
                    }
                }

                //me mysqlexception kanonika les kai den trexei tipota sinexizei edo xoris na mpei sto catch
                Console.WriteLine("NOTMYSQLEXCEPTION");
                onCompleted(dumpresult);

                mydump = null;
            }
            else
            {
                //we need enumaration classes for all kind of different erros
                //..We still need enumaration class for all kind of dif erros
                onError(-1);

                mydump = null;
            }
        }
Пример #2
0
        /// <summary>
        /// main mysql dump executor task
        /// start/running in ASYNC mode.
        /// internal its calling other task/jobs and waits for completion.
        /// with every task completed its firing notification events(listener)
        /// --BASIC FLOW--
        /// </summary>
        async void DumpMysqlTaskExecutor()
        {
            Task <List <string> > testConnectionTask = testCon();
            List <string>         tables             = await testConnectionTask;

            if (tables != null)
            {
                onProgress("connected");
                onInitDumpTables(tables);

                mydump = new MysqlDump();

                mydump.credentialsConfigInstance = credentialsConfigInstance;
                mydump.CompressProgress         += onCompressProgressHandler;
                mydump.CompressStart            += onCompressStartHandler;
                mydump.TableRowCount            += onTableRowCountHandler;
                mydump.TableStartDump           += onTableStartDumpHandler;

                Task <DumpResultSet> result     = dumptask(mydump);
                DumpResultSet        dumpresult = null;
                try
                {
                    dumpresult = await result;
                }
                catch (Exception ex)
                {
                    if (!(ex is NullReferenceException))
                    {
                        dumpresult = new DumpResultSet();
                        dumpresult.wasSuccessful             = false;
                        dumpresult.errorNumber               = 2;
                        dumpresult.mysqldumpexeStandardError = "Error in MySQLDumpAdapter:\n" + ex.Message;
                    }
                }

                onCompleted(dumpresult);
                mydump = null;
            }
            else
            {
                //we need enumaration classes for all kind of different erros
                //..We still need enumaration class for all kind of dif erros
                onError(-1);

                mydump = null;
            }
        }
Пример #3
0
 private void onCompleted(DumpResultSet status)
 {
     Completed?.Invoke(status);
 }
Пример #4
0
        private StringBuilder calculateArguments()
        {
            StringBuilder arguments = new StringBuilder();

            resultObj = new DumpResultSet();
            //<ConfigurationSection>

            arguments.Append("--protocol=tcp ");

            //Credentials

            //host
            if (!String.IsNullOrEmpty(credentialsConfigInstance.host))
            {
                arguments.Append("--host " + credentialsConfigInstance.host + " ");
            }
            else
            {
                resultObj.wasSuccessful = false;
                resultObj.errorNumber   = -1;
                resultObj.errorMessage  = "Host not set";
                return(arguments);
            }

            //port
            if (credentialsConfigInstance.port < 1 || credentialsConfigInstance.port > 65535)
            {
                resultObj.wasSuccessful = false;
                resultObj.errorNumber   = -1;
                resultObj.errorMessage  = "Invalid port number: " + credentialsConfigInstance.port;
                return(arguments);
            }
            else
            {
                arguments.Append("--port=" + credentialsConfigInstance.port + " ");
            }

            //username
            if (!String.IsNullOrEmpty(credentialsConfigInstance.username))
            {
                arguments.Append("--user " + credentialsConfigInstance.username + " ");
            }
            else
            {
                resultObj.wasSuccessful = false;
                resultObj.errorNumber   = -1;
                resultObj.errorMessage  = "Username not set";
                return(arguments);
            }

            //pasword
            if (!String.IsNullOrEmpty(credentialsConfigInstance.password))
            {
                arguments.Append("--password="******" ");
            }

            //MySqlDumpConfiguration

            //includeCreateSchema
            if (!configurationManagerInstance.mysqlDumpConfigInstance.includeCreateSchema)
            {
                arguments.Append("--no-create-info ");
            }

            //includeData
            if (!configurationManagerInstance.mysqlDumpConfigInstance.includeData)
            {
                arguments.Append("--no-data ");
            }

            //includeComments
            if (!configurationManagerInstance.mysqlDumpConfigInstance.includeComments)
            {
                arguments.Append("--skip-comments ");
            }

            //singleTransaction
            if (configurationManagerInstance.mysqlDumpConfigInstance.singleTransaction)
            {
                arguments.Append("--single-transaction ");
            }

            //disableForeignKeyChecks
            if (configurationManagerInstance.mysqlDumpConfigInstance.disableForeignKeyChecks)
            {
                arguments.Append("--disable-keys ");
            }

            //addDropDatabase
            if (configurationManagerInstance.mysqlDumpConfigInstance.addDropDatabase)
            {
                arguments.Append("--add-drop-database ");
            }

            //createDatabase
            if (!configurationManagerInstance.mysqlDumpConfigInstance.createDatabase)
            {
                arguments.Append("--no-create-db ");
            }

            //moreCompatible
            if (configurationManagerInstance.mysqlDumpConfigInstance.moreCompatible)
            {
                arguments.Append("--compatible ");
            }

            //characterSet
            if (configurationManagerInstance.mysqlDumpConfigInstance.characterSet != "utf8")
            {
                string charSetPath = "\"" + AppDomain.CurrentDomain.BaseDirectory + "resources\\mysqldump\\charsets\"";
                arguments.Append("--character-sets-dir=" + charSetPath + " ");
                arguments.Append("--default-character-set=" + configurationManagerInstance.mysqlDumpConfigInstance.characterSet + " ");
            }

            //addDropTable
            if (configurationManagerInstance.mysqlDumpConfigInstance.addDropTable)
            {
                arguments.Append("--add-drop-table ");
            }
            else
            {
                arguments.Append("--skip-add-drop-table ");
            }

            //addLocks
            if (configurationManagerInstance.mysqlDumpConfigInstance.addLocks)
            {
                arguments.Append("--add-locks ");
            }

            //noAutocommit
            if (configurationManagerInstance.mysqlDumpConfigInstance.noAutocommit)
            {
                arguments.Append("--no-autocommit ");
            }

            //encloseWithBackquotes
            if (!configurationManagerInstance.mysqlDumpConfigInstance.encloseWithBackquotes)
            {
                arguments.Append("--skip-quote-names ");
            }

            //addCreateProcedureFunction
            if (configurationManagerInstance.mysqlDumpConfigInstance.addCreateProcedureFunction)
            {
                arguments.Append(" --routines ");
            }

            //addInfoComments
            if (configurationManagerInstance.mysqlDumpConfigInstance.addInfoComments)
            {
                arguments.Append("--dump-date ");
            }

            //completeInsertStatements
            if (configurationManagerInstance.mysqlDumpConfigInstance.completeInsertStatements)
            {
                arguments.Append("--complete-insert ");
            }

            //extendedInsertStatements
            if (configurationManagerInstance.mysqlDumpConfigInstance.completeInsertStatements)
            {
                arguments.Append("--extended-insert ");
            }

            //maximumLengthOfQuery
            arguments.Append("--net-buffer-length " + configurationManagerInstance.mysqlDumpConfigInstance.maximumLengthOfQuery + " ");

            //maximumPacketLength
            arguments.Append("--max_allowed_packet=" + configurationManagerInstance.mysqlDumpConfigInstance.maximumPacketLength + "M ");

            //useIgnoreInserts
            if (configurationManagerInstance.mysqlDumpConfigInstance.useIgnoreInserts)
            {
                arguments.Append("--insert-ignore ");
            }

            //useHexadecimal
            if (configurationManagerInstance.mysqlDumpConfigInstance.useHexadecimal)
            {
                arguments.Append("--hex-blob ");
            }

            //dumpTriggers
            if (!configurationManagerInstance.mysqlDumpConfigInstance.dumpTriggers)
            {
                arguments.Append("--skip-triggers ");
            }

            //dumpEvents
            if (configurationManagerInstance.mysqlDumpConfigInstance.dumpEvents)
            {
                arguments.Append("--events ");
            }

            //xml
            if (configurationManagerInstance.mysqlDumpConfigInstance.xml)
            {
                arguments.Append("--xml ");
            }

            //exportType
            switch (configurationManagerInstance.mysqlDumpConfigInstance.exportType)
            {
            case 0:
                break;

            case 1:
                arguments.Append("--replace ");
                break;

            default:
                break;
            }

            //database choice
            if (credentialsConfigInstance.databases == null)
            {
                if (string.IsNullOrEmpty(credentialsConfigInstance.database))
                {
                    arguments.Append("--all-databases ");
                }
                else
                {
                    arguments.Append("--databases " + credentialsConfigInstance.database);
                    if (credentialsConfigInstance.excludeTablesSingleDatabase != null)
                    {
                        arguments.Append(" ");
                        string[] tables = credentialsConfigInstance.excludeTablesSingleDatabase.Split(',');
                        foreach (string table in tables)
                        {
                            arguments.Append("--ignore-table=" + credentialsConfigInstance.database + "." + table + " ");
                        }
                    }
                }
            }
            else
            {
                arguments.Append("--databases ");
                foreach (string database in credentialsConfigInstance.databases)
                {
                    arguments.Append(database + " ");
                }
                if (credentialsConfigInstance.excludeTables != null)
                {
                    for (int i = 0; i < credentialsConfigInstance.excludeTables.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(credentialsConfigInstance.excludeTables[i]))
                        {
                            string[] tables = credentialsConfigInstance.excludeTables[i].Split(',');
                            foreach (string table in tables)
                            {
                                arguments.Append("--ignore-table=" + credentialsConfigInstance.databases[i] + "." + table + " ");
                            }
                        }
                    }
                }
            }

            //</ConfigurationSection>
            resultObj.wasSuccessful = true;
            return(arguments);
        }
Пример #5
0
        public DumpResultSet executeDump()
        {
            Tuple <StringBuilder, DumpResultSet> pair = calculateArguments();
            StringBuilder arguments = pair.Item1;
            DumpResultSet resultObj = pair.Item2;

            if (!resultObj.wasSuccessful)
            {
                return(resultObj);
            }
            //dump execution
            Console.WriteLine(arguments.ToString());
            proc = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName               = mysqldumpfile,//AppDomain.CurrentDomain.BaseDirectory.Replace("bin\\Debug\\", "mysqldump.exe") ,
                    Arguments              = arguments.ToString(),
                    UseShellExecute        = false,
                    RedirectStandardOutput = true, //prepei na diavastoun me ti seira pou ginonte ta redirect alliws kolaei se endless loop
                    RedirectStandardError  = true,
                    CreateNoWindow         = true
                }
            };

            Console.WriteLine("MySqlDump: Dump starting now");
            proc.Start();

            Random rnd = new Random();
            string fileExt;
            ConfigurationManager configurationManagerInstance = ConfigurationManager.getInstance();

            if (configurationManagerInstance.mysqlDumpConfigInstance.getSettings().xml)
            {
                fileExt = ".xml";
            }
            else
            {
                fileExt = ".sql";
            }
            String filename = "dump" + rnd.Next(1000000, 9999999) + fileExt;

            Directory.CreateDirectory(configurationManagerInstance.mysqlDumpConfigInstance.getSettings().tempSavePath);


            //checking if file exists
            while (File.Exists(configurationManagerInstance.mysqlDumpConfigInstance.getSettings().tempSavePath + filename))
            {
                filename = "Dump" + rnd.Next(10000000, 99999999) + fileExt;
            }

            try
            {
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@configurationManagerInstance.mysqlDumpConfigInstance.getSettings().tempSavePath + filename))
                {
                    //addCustomCommentInHeader
                    if (!string.IsNullOrEmpty(configurationManagerInstance.mysqlDumpConfigInstance.getSettings().addCustomCommentInHeader))
                    {
                        file.WriteLine("-- Custom comment: " + configurationManagerInstance.mysqlDumpConfigInstance.getSettings().addCustomCommentInHeader);
                    }


                    while (!proc.StandardOutput.EndOfStream)
                    {
                        string line = proc.StandardOutput.ReadLine();
                        file.WriteLine(line);
                        handleLineOutput(line, configurationManagerInstance.mysqlDumpConfigInstance.getSettings());
                    }
                }

                resultObj.mysqldumpexeStandardError = "";
                while (!proc.StandardError.EndOfStream)
                {
                    resultObj.mysqldumpexeStandardError += proc.StandardError.ReadLine() + "\n";
                }

                if (resultObj.mysqldumpexeStandardError.StartsWith("Warning: Using a password"))
                {
                    resultObj.mysqldumpexeStandardError = resultObj.mysqldumpexeStandardError.Replace("Warning: Using a password on the command line interface can be insecure.\n", "");
                }

                Console.WriteLine(resultObj.mysqldumpexeStandardError); //for testing

                proc.WaitForExit();
            }
            catch (NullReferenceException ex)
            {
                Console.WriteLine("MySQLdump null reference exception on proccess: " + ex.Message);
                File.Delete(configurationManagerInstance.mysqlDumpConfigInstance.getSettings().tempSavePath + filename);
            }
            if (proc == null || proc.ExitCode != 0)
            {
                resultObj.wasSuccessful = false;
                resultObj.errorNumber   = -2;
                if (proc == null)
                {
                    resultObj.mysqldumpexeStandardError = "MySQL dump proccess was killed.";
                }
                File.Delete(configurationManagerInstance.mysqlDumpConfigInstance.getSettings().tempSavePath + filename);
            }
            else
            {
                resultObj.wasSuccessful = true;
                resultObj.fileAbsPath   = configurationManagerInstance.mysqlDumpConfigInstance.getSettings().tempSavePath + filename;

                //compression
                if (configurationManagerInstance.compressConfigInstance.getSettings().enableCompression)
                {
                    comp = new Compression();
                    comp.absolutePath      = resultObj.fileAbsPath;
                    comp.CompressProgress += onCompressProgressHandler;
                    comp.CompressStart    += onCompressStartHandler;

                    CompressionResultSet compResult = comp.doCompress7z(); //edw kaleitai to compression

                    if (!compResult.wasSuccessful)
                    {
                        resultObj.wasSuccessful             = false;
                        resultObj.errorNumber               = -3;
                        resultObj.mysqldumpexeStandardError = compResult.standardError;
                    }
                    File.Delete(resultObj.fileAbsPath); //delete to sketo .sql
                    resultObj.fileAbsPath = compResult.resultAbsPath;
                }
            }

            return(resultObj);
        }