• 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

    • It's just exactly what it looks like. The facial polygon count is low and rigid, and the background appears heavily blurred. You can clearly see jagged edges around the car window as it exits the tunnel. The character animations feel like they're from 2010. The Xbox Series S has performance similar to an RX 570 or GTX 1660, which is disappointing and contributes to holding back game development. The Switch GPU is roughly on par with a 2050 or 3050, which is even worse. Developers have often discussed the challenges and limitations of developing for outdated/low end hardware, noting how it can hold back progress. This isn't a new issue. I am sure we will hear about the "Challenges" they had to overcome here and sacrifices that had to be made for compatibility with the outdated/low end hardware.
    • Whether on Edge or Firefox, I don't get this prompt to accept cookies, because they're already whitelisted by the Cookie AutoDelete extension, both on Edge and Firefox. And, as I've already said, using Edge (yesterday), Neowin isn't whitelisted (I never use Edge), and I didn't get that pop-up. And this morning, after checking, still no pop-up, and everything is activated, the proof (screenshot), I have no ads on the right side. I must add that I block the following trackers Doubleclick, and Scorecardresearch, as well as a few other undesirable ones, via the Host file. Back to square one! After all, it's a simple click to continue, so I'll do with it. 🤷🏽‍♂️ Thank You
    • F'ing FIIINALLY! Much easier to ask people what their hardware is 🙏 and could also very well just say DDR4 or DDR5 without asking to bloat it too much. I'm also curious to see what it will say in More device info...
    • Sure, that must be why it was revealed at the Sony showcase... the game was announced 5 years ago and the visuals are far from poor, what the hell are you smoking, plus the Switch 2 is pretty much on par with an Xbox Series S. It would be absolutely insane for a dev to build a game "with the Switch 2 in mind" when it's coming to all platforms where the majority of sales will happen.
  • Recent Achievements

    • Week One Done
      jbatch earned a badge
      Week One Done
    • First Post
      Yianis earned a badge
      First Post
    • Rookie
      GTRoberts went up a rank
      Rookie
    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      400
    2. 2
      +FloatingFatMan
      178
    3. 3
      snowy owl
      170
    4. 4
      ATLien_0
      167
    5. 5
      Xenon
      134
  • Tell a friend

    Love Neowin? Tell a friend!