MySql Data backup 1.
cmd environment backup
backing up the MySQL database in a cmd environment is available to the following commands:
1. Back up a table
Mysqldump-u username-p dbname table1 table2 ...-> backupname.sql
parameter Description :
dbname: represents the name of the database;
Table1 and table2: represents the name of the table that needs to be backed up, and the entire database is backed up as empty;
Backupname.sql: represents the name of the backup file, which can be preceded by an absolute path.
2. Backing up multiple databases
Mysqldump-u username-p--databases dbname2 dbname2 > Backupname.sql
3. back up all databases
Mysqldump-u username-p-all-databases > Backupname.sql
4. Data Restore
Mysql-u root-p [dbname] < BACKUP.SQ
2.
call mysqldump.exe backup
sometimes in Java Code, a scheduled job backup is required for automatic backup. The data can be backed up in the following ways. The approximate process is:
1. Create A bat file
2. Call The bat file to get the input stream to write to the backup file
The code examples are as follows:
Packagecom.hymake.xmq;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;ImportJava.util.Calendar;Importjava.util.Date;/*** MySQL Data Backup tool class *@authorXIONGMQ **/ Public classMysqlbackup { Public Static voidMain (string[] args) {dobackup (); } //MySQL Data backup Public Static BooleanDobackup () {Booleanflag=false; //Set up backup informationString databasename= "Weixin";//Database nameString mysqldumpurl= "C:\\Program Files\\mysql\\mysql Server 5.5";//MySQL Installation locationString savepath= "d:\\ data Backup";//backup File Save locationString host= "127.0.0.1";//Host IPString port= "3306";//PortString username= "root";//Login NameString password= "123456";//Login Password//Initialize the bat fileString Databackupbatpath = System.getproperty ("Java.io.tmpdir") + "\\dataBackup.bat"; //Create a bat file to make a callGetbackupbat (Databackupbatpath); //Patchwork Backup CommandsStringBuilder cmdstr=NewStringBuilder (); Cmdstr.append (Databackupbatpath); Calendar Cal=calendar.getinstance (); Date Now=NewDate (); Cal.settime (now); String Year=string.valueof (Cal.get (calendar.year)); String Month=cal.get (calendar.month) +1<10? " 0 "+string.valueof (cal.get (Calendar.month) +1): String.valueof (Cal.get (calendar.month) +1); String Date=cal.get (calendar.date) <10? " 0 "+string.valueof (Cal.get (calendar.date)): String.valueof (Cal.get (calendar.date)); String Hour=cal.get (calendar.hour_of_day) <10? " 0 "+string.valueof (Cal.get (Calendar.hour_of_day)): String.valueof (Cal.get (calendar.hour_of_day)); String minute=cal.get (Calendar.minute) <10? " 0 "+string.valueof (Cal.get (Calendar.minute)): String.valueof (Cal.get (Calendar.minute)); String sec=cal.get (Calendar.second) <10? " 0 "+string.valueof (Cal.get (Calendar.second)): String.valueof (Cal.get (Calendar.second)); String Sqlfielname=databasename+ "_" +year+month+date+hour+minute+sec; String Sqlfielpath=year+ "\ \" +month; Cmdstr.append ("\" "+savepath+" \ ""); Cmdstr.append ("\" "+sqlfielpath+" \ ""); //Get Database LocationCmdstr.append ("\" "+mysqldumpurl+" \ ""); Cmdstr.append (" "+host); Cmdstr.append (" "+port); Cmdstr.append (" "+userName); Cmdstr.append (" "+password); Cmdstr.append (" "+databaseName); Cmdstr.append (" "+sqlfielname); Try { //Execute CommandProcess p =runtime.getruntime (). EXEC (cmdstr.tostring ()); InputStream FIS=P.getinputstream (); InputStreamReader ISR=NewInputStreamReader (FIS); BufferedReader BR=NewBufferedReader (ISR); while((Br.readline ())! =NULL) {flag=true; } } Catch(IOException e) {e.printstacktrace (); } returnFlag; } /*** Create a BAT file *@paramTempPath*/ Public Static BooleanGetbackupbat (String temppath) {Booleanflag=false; StringBuilder content=NewStringBuilder (); Content.append ("SET back_dir=%1"); Content.append ("\ r \ n"); Content.append ("MD%back_dir%\\%2"); Content.append ("\ r \ n"); Content.append ("SET back_path=%back_dir%\\%2"); Content.append ("\ r \ n"); Content.append ("SET mysql_path=%3"); Content.append ("\ r \ n"); Content.append ("%mysql_path%\\bin\\mysqldump.exe--opt-h%4-p%5-u%6-p%7%8>%back_path%\\%9.sql"); Content.append ("\ r \ n"); Content.append ("Echo Data backup complete!"); String DirectoryPath= temppath.substring (0, temppath.lastindexof ("\ \")); String FileName= Temppath.substring (temppath.lastindexof ("\ \") + 1, Temppath.length ()); BooleanCheckdirectoryisexist =NewFile (DirectoryPath). exists (); if(!checkdirectoryisexist) { NewFile (DirectoryPath). Mkdirs (); } Try{File File=NewFile (DirectoryPath + file.separator +fileName); BufferedWriter BW=NewBufferedWriter (NewOutputStreamWriter (NewFileOutputStream (file), "UTF-8")); Bw.write (Content.tostring ()); Flag=true; Bw.close (); } Catch(IOException e) {e.printstacktrace (); } returnFlag; }}
MySQL Data backup