• 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

    • For anyone looking for a lightweight formatting-free text editor, I recommend Notepad3.
    • This looks really dumb, especially if it costs $100+. Noone who cares about using a flight yoke would touch that thing, people who don't care are probably fine using the analog sticks on their controller, so who is it for?
    • A) "they shouldn't be making money off of those [free videos]"?? That is literally their business model, making money off videos that users post...if you don't feel like that should be allowed, then are you saying YouTube shouldn't exist. B) Yes, the example I gave is a net-negative transaction. If YouTube makes money from others who are following their rules, it doesn't change the fact that the person using an ad-blocker is costing them money. C) YouTube has always operated at a loss...kind of invalidates your entire argument. As I always say, I don't care what you do, I will not even say you are wrong for doing it. That is purely your choice. Just be honest enough to say something like "Google is rich, I honestly don't care." Perfectly fine reason. Don't act like there is some imagined justification for why it isn't breaking the rules.
    • You can now present content from your camera feed in Google Meet by David Uzondu Google has a new feature rolling out for Google Meet that lets you directly present video from an external camera feed right into your meetings. This means if you have a document camera for showing physical papers, a dedicated external camera for a better angle, or even output from a video production tool, you can now pipe that into Meet as a presentation source. This new option supports video up to 1080p at 30FPS. This "present from camera" function offers a more integrated way to handle certain video inputs compared to some existing workarounds. For instance, it might prove less complicated than a setup with OBS Studio where you arrange your various video sources into scenes, activate the virtual camera output, and then navigate Google Meet's settings to specifically choose "OBS Virtual Camera" as your video input before you can even start presenting that customized feed. Alongside this camera presentation feature, Google's announcement also mentioned several improvements to the general screen sharing experience in Meet. Initiating any type of screen share is faster now, and video quality during screen sharing has also been sharpened, with better handling of dynamic content like scrolling text or embedded videos. To reduce interruptions, if a second presenter stops sharing their screen, any previous presentation will now automatically resume. For those wondering when they can get their hands on this, the rollout for the camera presentation feature and these screen sharing enhancements has begun for Rapid Release domains. Users on Scheduled Release domains will start seeing it from June 11, 2025. Google notes that it could take up to 15 days for these features to be visible to all eligible users. Most Google Workspace accounts, including Business Standard and Plus, various Enterprise and Education tiers, and Workspace Individual subscribers, will have access. This new presentation option joins other recent Google Workspace enhancements. For instance, Gemini in Google Drive can now summarize changes to your files, offering a quick way to get updated on what you missed in documents since you last opened them.
  • Recent Achievements

    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
    • Apprentice
      DarkShrunken went up a rank
      Apprentice
    • Dedicated
      CHUNWEI earned a badge
      Dedicated
    • Collaborator
      DarkShrunken earned a badge
      Collaborator
  • Popular Contributors

    1. 1
      +primortal
      382
    2. 2
      +FloatingFatMan
      177
    3. 3
      ATLien_0
      174
    4. 4
      snowy owl
      169
    5. 5
      Xenon
      134
  • Tell a friend

    Love Neowin? Tell a friend!