0

I was trying to make a simple backup application to do some tasks like

  • Rar folder to destination
  • backup database files
  • copy some files from another machine

I can do all those things in CMD, so I made an EXE that simple executes those commands in the CMD with Process.

The only one that simple don't work, and don't send any error messages is the database backup, IF I copy the genereted string and past in a CMD it works. So I have no idea why it is not working.

The process config, start, stop and output listner code:

    private string lastLine;
    private void Wait(string text) {
        while (lastLine == null || !lastLine.Contains(text)) {
            System.Threading.Thread.Sleep(500);
        }
    }

    private Process cmd = null;
    private void StartCmd() {
        if (cmd != null) {
            StopCmd();
        }

        ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe") {
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            UseShellExecute = false,
            CreateNoWindow = true,
            Verb = "runas",
        };

        cmd = Process.Start(processStartInfo);

        cmd.OutputDataReceived += CmdDataListner;
        cmd.BeginOutputReadLine();
    }

    private void StopCmd() {
        cmd.Close();
        cmd.Dispose();
        cmd = null;
    }

    private void CmdDataListner(object sender, DataReceivedEventArgs e) {
        CmdListner(e.Data);
    }

    //This is just a RichText in the form to see the output
    private void CmdListner(string text) {
        if (text == null) {
            return;
        }

        text = text.Trim();
        if (text.Length == 0) {
            return;
        }

        if (richTextBox1.InvokeRequired) {
            richTextBox1.Invoke(new Action<string>(CmdListner), text);
        } else {
            lastLine = text;
            richTextBox1.AppendText(text + Environment.NewLine);
            richTextBox1.SelectionStart = richTextBox1.Text.Length;
            richTextBox1.ScrollToCaret();
        }
    }

The CMD commands execution:

private void Backup(){
    cmd.StandardInput.WriteLine("D:");
    cmd.StandardInput.WriteLine("cd " + path);
    cmd.StandardInput.WriteLine("\"c:\\Program Files (x86)\\WinRAR\"\\rar.exe a File.rar targetFolder\"");
    Wait("Done");

    //More commands...
    //Everything works fine so far...

    //Than I try
    cmd.StandardInput.WriteLine("SqlCmd -E -S ServerName\\Instance –Q \"BACKUP DATABASE BaseXYZ TO DISK = 'D:\\backups\\BaseXYZ.bak'\"");
    //Nothing more happens, no output, not error, no nothig...

    Wait("BACKUP DATABASE successfully");

    cmd.StandardInput.WriteLine("SqlCmd -E -S ServerName\\Instance –Q \"BACKUP DATABASE BaseXYZxxx TO DISK = 'D:\\backups\\BaseXYZxxx.bak'\"");
    Wait("BACKUP DATABASE successfully");

    cmd.StandardInput.WriteLine("exit");
    cmd.StandardInput.Close();
    cmd.WaitForExit();

    StopCmd();
    Application.Exit();
}

Sure that I have way more commands then on this block, lots of for loops, etc. But everything works, only the Database backup that don't work, even in isolated test. It simple hangs like if it was waiting for more inputs

I have tested it like

string commandLine = sqlBackup;
MessageBox.Show(commandLine);
cmd.StandardInput.WriteLine(commandLine);

You just press CTRL + C in the Alert box to copy it. Than past it in the CMD and works, the backup take less then 1sec. The file is there in the folder.

When executed by the Process it just hangs, it does not create any file, even if I wait way more than 1sec.

P. Waksman
  • 979
  • 7
  • 21
  • 1
    IMO you would be a lot better off if you stop using CMD and start using SQL Server's API: https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.backup.aspx –  Feb 15 '18 at 14:12
  • Nice, didn't know that was an API for that. It was actually easy to implement it. But I still wondering, why the code when executed by the process simple hangs. – P. Waksman Feb 15 '18 at 17:21
  • That I can't answer, sorry. Just offering alternatives. –  Feb 15 '18 at 18:29
  • No problem, the alternative works fine. Thanks for pointing that out. – P. Waksman Feb 15 '18 at 23:33

0 Answers0