• 0

c# , simple MySQL backup solution


Question

I'm coding an app in c# that uses a MySQL database as a backend and it needs a simple backup feature

the backup will be done to a USB memory stick (probably 256MB or so)

I need a simple and very preferably free way to do this

I tried calling mysql dump from inside c# with the following code

 private void backupButtonj_Click(object sender, System.EventArgs e) {
 	 System.Diagnostics.Process proc = new System.Diagnostics.Process();
 	 proc.EnableRaisingEvents=false;
 	 proc.StartInfo.FileName="mysqldump";
 	 proc.StartInfo.Arguments="supplydirect > I:\\backup\\backup.txt";
 	 proc.Start();
 	 proc.WaitForExit();
  }

but for some reason it just flashes up the command window and doesnt do the backup

but doing the same thing from the DOS prompt works...

Link to comment
https://www.neowin.net/forum/topic/179187-c-simple-mysql-backup-solution/
Share on other sites

10 answers to this question

Recommended Posts

  • 0

yea , you were right thanks , here's the final code if anyone cares

 private void backupButtonj_Click(object sender, System.EventArgs e) {
    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 = "I:\\"+year+"-"+month+"-"+day+"-"+hour+"-"+minute+"-"+second+"-"+ms+".txt";
   	 StreamWriter file = new StreamWriter(tmestr);
   	 ProcessStartInfo proc = new ProcessStartInfo(); 
   	 proc.FileName = "mysqldump";
   	 proc.RedirectStandardInput = false;
   	 proc.RedirectStandardOutput = true;
   	 proc.Arguments = "supplydirect";
   	 proc.UseShellExecute = false; 
   	 Process p = Process.Start(proc);
   	 string res;
   	 res = p.StandardOutput.ReadToEnd();
   	 file.WriteLine(res);
   	 p.WaitForExit(); 
   	 file.Close();
    }

    catch (IOException ex) {
   	 MessageBox.Show("Disk or other IO error , unable to backup!");
    }
  }

  • 0

While searching for backup mysql data from C#, I found this one good solution, but it did not solve my problem completely.

The problem is that, I am just getting following text in backup file instead of whole database.

  Quote
-- MySQL dump 10.10

--

-- Host: localhost Database: smartdb

-- ------------------------------------------------------

-- Server version 5.0.22-community-nt

/*!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 */;

I have modified the code as below.

  Quote
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", "password", "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 (IOException ex)

{

MessageBox.Show("Disk or other IO error , unable to backup!");

}

THANKS FOR YOUR HELP.

SHRESTHA

  • 0

The problem is about this line:

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

You should not include piping options in the string. also the string should not contain any semicolon ( ; ) character.

Just rewrite the line as:

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

and the everything will be OK.

  • 0

Hello,

the Backup of the Database works fine like this

System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = Path.Combine(ApplicationConfig.ApplicationDataPath, "db\\bin\\mysqldump.exe");
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.CreateNoWindow = true;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.Arguments = "-h localhost --user=" + dbUser + " --password=" + dbPwd + " --databases db1 db2 --result-file " + OutputFile;
myProcess.Start();
string output = myProcess.StandardOutput.ReadToEnd();

if the output ios empty - no error occured.

But the question is: how does the restore work? I cant find a parameter like --result-file for the restore direction.

Are there other ways to do this?

Thanks

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I like Neowin a lot, but this "article" is shady and misleading. We need an "editorial" flag and this isn't "news" it's "my cousin's dog's best friend posted on twitter about their snapchat account from their dog's owner that a bad thing happened, so it's NEWS." Thanks
    • Burrrrn. Bought COD WWII last month when it was on sale for PC. Oh well. Excellent game tho, and the PC version plays/looks amazing (and has it's own PC achievements). ...wondering if this is a lesson on waiting on sales. ...also hoping Rise of the Tomb Raider has it's own PC Achievements
    • Google Chrome 137.0.7151.120 (offline installer) by Razvan Serea The web browser is arguably the most important piece of software on your computer. You spend much of your time online inside a browser: when you search, chat, email, shop, bank, read the news, and watch videos online, you often do all this using a browser. Google Chrome is a browser that combines a minimal design with sophisticated technology to make the web faster, safer, and easier. Use one box for everything--type in the address bar and get suggestions for both search and Web pages. Thumbnails of your top sites let you access your favorite pages instantly with lightning speed from any new tab. Desktop shortcuts allow you to launch your favorite Web apps straight from your desktop. Chrome has many useful features built in, including automatic full-page translation and access to thousands of apps, extensions, and themes from the Chrome Web Store. Google Chrome is one of the best solutions for Internet browsing giving you high level of security, speed and great features. Important to know! The offline installer links do not include the automatic update feature. Google Chrome 137.0.7151.120 changelog: [$7000][420697404] High CVE-2025-6191: Integer overflow in V8. Reported by Shaheen Fazim on 2025-05-27 [$4000][421471016] High CVE-2025-6192: Use after free in Profiler. Reported by Chaoyuan Peng (@ret2happy) on 2025-05-31 [425443272] Various fixes from internal audits, fuzzing and other initiatives Download web installer: Google Chrome Web 32-bit | Google Chrome 64-bit | Freeware Download: Google Chrome Offline Installer 64-bit | 128.0 MB Download: Google Chrome Offline Installer 32-bit | 115.0 MB Download page: Google Chrome Portable Download: Google Chrome MSI Installers for Windows (automatic update) View: Chrome Website | Release Notes Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • One Month Later
      Custom Greek Shirts earned a badge
      One Month Later
    • Week One Done
      Custom Greek Shirts earned a badge
      Week One Done
    • One Year In
      Custom Greek Shirts earned a badge
      One Year In
    • Week One Done
      topantidetectbrowser earned a badge
      Week One Done
    • Explorer
      Jdoe25 went up a rank
      Explorer
  • Popular Contributors

    1. 1
      +primortal
      672
    2. 2
      ATLien_0
      281
    3. 3
      Michael Scrip
      223
    4. 4
      +FloatingFatMan
      190
    5. 5
      Steven P.
      145
  • Tell a friend

    Love Neowin? Tell a friend!