Advertisement
RuBeRto

backupdatabase

Sep 21st, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1. #region backupdatabase
  2.             if (Now.Hour == 00 && Now.Minute == 00 && Now.Second == 00)
  3.             {
  4.                 string a = @"C:\AppServ";
  5.                 string user = "root";
  6.                 string pass = "12345678";
  7.                 string database = "hard";
  8.                 string filename = "databackup.sql";
  9.                 try
  10.                 {
  11.                     if (File.Exists(@"C:\AppServ\databackup.sql"))
  12.                     {
  13.                         Console.WriteLine("Deleting old file");
  14.                         File.Delete(@"C:\AppServ\databackup.sql");
  15.                         StreamWriter MySWriter;
  16.                         StreamReader MySReader;
  17.                         Process myp = new Process();
  18.                         myp.StartInfo.FileName = "cmd.exe";
  19.                         myp.StartInfo.UseShellExecute = false;
  20.                         myp.StartInfo.WorkingDirectory = a;
  21.                         myp.StartInfo.RedirectStandardInput = true;
  22.                         myp.StartInfo.RedirectStandardOutput = true;
  23.                         myp.Start();
  24.  
  25.                         MySWriter = myp.StandardInput;
  26.                         MySReader = myp.StandardOutput;
  27.                         MySWriter.WriteLine("mysqldump --no-defaults -u " + user + " -p" + pass + " " + database + " >" + filename + "");
  28.                         MySWriter.Close();
  29.                         myp.WaitForExit();
  30.                         myp.Close();
  31.                         Console.WriteLine("database backedup at " + DateTime.Now.Date);
  32.                     }
  33.                     else
  34.                     {
  35.                         StreamWriter MySWriter;
  36.                         StreamReader MySReader;
  37.                         Process myp = new Process();
  38.                         myp.StartInfo.FileName = "cmd.exe";
  39.                         myp.StartInfo.UseShellExecute = false;
  40.                         myp.StartInfo.WorkingDirectory = a;
  41.                         myp.StartInfo.RedirectStandardInput = true;
  42.                         myp.StartInfo.RedirectStandardOutput = true;
  43.                         myp.Start();
  44.  
  45.                         MySWriter = myp.StandardInput;
  46.                         MySReader = myp.StandardOutput;
  47.                         MySWriter.WriteLine("mysqldump --no-defaults -u " + user + " -p" + pass + " " + database + " >" + filename + "");
  48.                         MySWriter.Close();
  49.                         myp.WaitForExit();
  50.                         myp.Close();
  51.                         Console.WriteLine("database backedup at " + DateTime.Now.Date);
  52.                     }
  53.                 }
  54.                 catch (Exception ef)
  55.                 {
  56.                     Console.WriteLine(ef);
  57.                 }
  58.             }                            
  59.  
  60.             #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement