user553802
user553802

Reputation: 11

MySQL backup with C# backup stopped in the middle

I'm a newbie with Mysql and C#. I have tried to write a function to backup my database.

Here's what I wrote:

public static void Backup()
{
    try
    {
        DateTime Time = DateTime.Now;
        int year = Time.Year;
        int month = Time.Month;
        int day = Time.Day;
        int hour = Time.Hour;
        int minute = Time.Minute;
        int second = Time.Second;
        int millisecond = Time.Millisecond;

        string path;
        path = "C:\\MySqlBackup" + year + "-" + month + "-" + day +
        "-" + hour + "-" + minute + "-" + second + "-" + millisecond +".sql";

        StreamWriter file = new StreamWriter(path);

        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = "C:\\Program Files\\MySQL\\MySQL Server 5.1\\bin\\mysqldump";
        psi.RedirectStandardInput = false;
        psi.RedirectStandardOutput = true;

        psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3} > {4};",
        uid, password, server, database, path);
        psi.UseShellExecute = false;

        Process process = Process.Start(psi);

        Process process = Process.Start(psi);

        string output;
        output = process.StandardOutput.ReadToEnd();
        file.WriteLine(output);
        process.WaitForExit();
        file.Close();
        process.Close();
    }
    catch (IOException ex)
    {
        Console.Write("Error , unable to backup!");
    }
}

The problem is that at the backup file I'm getting only the first lines:

-- MySQL dump 10.13 Distrib 5.1.51, for Win32 (ia32)
-- 
-- Host: localhost Database: try
-- ------------------------------------------------------
-- Server version 5.1.51-community

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

but all the tables and all of the record weren't backup.

I tried to run a backup on cmd and it works fine. I can not find the reason.

Someone can help me, please? I'll appreciate any help. thanks

Upvotes: 1

Views: 808

Answers (2)

Orhan
Orhan

Reputation: 11

The problem is with this line:

string.Format(@"-u{0} -p{1} -h{2} {3} > {4};",uid, password, server, database, path);

Just rewrite the line as:

string.Format(@"-u{0} -p{1} -h{2} {3}",uid, password, server, database, path);

Upvotes: 1

Klaus Byskov Pedersen
Klaus Byskov Pedersen

Reputation: 120917

I'm wondering why you have this line twice:

Process process = Process.Start(psi);

Maybe that has something to do with it?

Upvotes: 1

Related Questions