开发者

c# console application backup mysql

开发者 https://www.devze.com 2023-02-05 05:13 出处:网络
I try to use this code as a console application so that I can back up mydatabase automatics I am not sure what wrong with it

I try to use this code as a console application so that I can back up mydatabase automatics I am not sure what wrong with it

static void Main(string[] args)
{    
    try
    {
        DateTime backupTime = DateTime.Now;
        int year = backupTime.Year;
        int month = backupTime.Month;
        int day = backupTime.Day;
        int hour = backupTime.Hour;
        int minute = backupTime.Minute;
        int second = backupTime.Second;
        int ms = backupTime.Millisecond;

        String tmestr = backupTime.ToString();
        tmestr = "C:\\" + year + "-" + month + "-" + day + "-" + hour + "-" + minute + ".sql";
        StreamWriter file = new StreamWriter(tmestr);
        ProcessStartInfo proc = new ProcessStartInfo();
        string cmd = string.Format(@"-u{0} -p{1} -h{2} {3} > {4};", "root", "", "localhost", "dbfile", "backup.sql");
        proc.FileName = "mysqldump";
        proc.RedirectStandardInput = false;
        proc.RedirectStandardOutput = true;
        proc.Arguments = cmd;//"-u root -p smartdb > testdb.sql";
        proc.UseShellExecute = false;
        Process p = Process.Start(proc);
        string res;
        res = p.StandardOutput.ReadToEnd();
        file.WriteLine(res);
        p.WaitForExit();
        file.Close();

    }

    catch (I开发者_运维知识库OException ex)
    {
        MessageBox.Show("Disk full or other IO error , unable to backup!");
    }
}


Since I'm not sure what kind of error you're geting, atleast this could be changed.

string cmd = string.Format(@"-u{0} -p{1} -h{2} {3} > {4};", "root", "", "localhost", "dbfile", "backup.sql");

Later you commented it should be -u root -p smartdb > testdb.sql"; except the above, is missing the space after the -u so I'd change it to:

string cmd = string.Format(@"-u {0} -p {1} -h {2} {3} > {4};", "root", "", "localhost", "dbfile", "backup.sql");


Why are you creating a StreamWriter and trying to get the output of mysqldump, Why not just tell mysqldump to write to the backup file itself?

string cmd = string.Format(@"-u{0} -p{1} -h{2} {3} > ""{4}"";",
    "root", "", "localhost", "dbfile", tmestr);


for starters i would change how you're outputting the information. You're essentially opening a file so you can redirect the backup utility's output to that file. The > in your process call is for that very purpose. Just change your "backup.sql" parameter to tmestr.

Also, because the output is already being redirected to a file, your process won't have anything to return. But because we're now having it dump to the correct path, this should be irrelevant.

One final change, add a space between your -u{0} so it's -u {0}. And the same with -h{2}.

In summary:

  • remove StreamWriter and all variables associated to it
  • modify the String.Format in your process arguments to use `tmestr
  • add spaces in your cmd variable for -u&-h

And you should be good-to-go (assuming it's not a problem with locating the mysqldump executable.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号