msql數據備份與還原

一數據庫備份:

1.將單個表導出爲csv文件

數據表導出csv語句:

select *from TSI_20140206_0_20120701 into outfile '/home/maysqldata/TSI_20140206_0_20120701.csv'  fields terminated by ',' optionally enclosed by'"' escaped by '"' lines terminated by '\r\n'; 

數據表導入csv語句:

導入的時候需要事先建好表結構,才能夠導入。

mysql導出表結構的代碼:

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

使用mysqldump命令
格式
mysqldump YourDatabaseName -uYourUserName -pYourPassword 
YourDatabaseName是你想處理的數據庫名
YourUserName和YourPassword 對應你的授權口令
如果只需要導出表的結構,那麼可以使用mysqldump的 -d 選項

導出整個庫的表結構如下:

mysqldump -uroot -p -d databasename > createtab.sql,

如果只想導出 表 table1 table2 table3 的 表結構 和 數據呢?
該如何導出?

mysqldump -uroot -p -d databasename test1 test2 test3 > createtab.sql

-- 上面的是導出指定表結構,下面這個可以導出指定表結構和數據
mysqldump -uroot -p --tables databasename > createtab.sql

mysqldump -uroot -p -d databasename table1table2 table3 > createtab.sql

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

然後導入:

load data infile '/tmp/test.csv' into table test_info  fields terminated by ','  optionally enclosed by '"' escaped by'"' lines terminated by '\r\n';  

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

2.當數據表的個數很大,以至於手動備份不方便時(例如將數據庫中所有表導出爲csv文件),這裏我用了一個比較笨的方法:

首先查詢數據庫中所有的表名,將表名稱輸出爲csv文件。

SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA ='BD_result_DB'into outfile '/mnt/80.50store/BD_mysql/BD_result_DB/tableName.csv';;

然後,自己編寫了一個java小程序,讀取這些表名稱,循環遍歷生成單表備份語句。

package cn.com.mysqlBack;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

public class TestMain {
	public static Logger LOG = Logger.getLogger(TestMain.class);

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		ResourceBundle bundle = ResourceBundle.getBundle("config",
				Locale.getDefault());
		String filePath = bundle.getString("inputfile");
		String outfile = bundle.getString("outfile");
		Operate operate = new Operate();
		String sqlDB = "use " + bundle.getString("DB");
		operate.executeQuery(sqlDB);
		String sql = bundle.getString("getTBNameSql");
		operate.executeQuery(sql);
		File file = new File(filePath);
		if (file.isFile() && file.exists()) {
			InputStreamReader read = new InputStreamReader(new FileInputStream(
					file));
			BufferedReader bufferedReader = new BufferedReader(read);
			String lineTxt = null;
			while ((lineTxt = bufferedReader.readLine()) != null) {
				String tableBackupSql = "select * from "
						+ lineTxt
						+ " into outfile "
						+ "\'/mnt/80.50store/BD_mysql/"
						+ bundle.getString("DB")
						+ "/"
						+ lineTxt
						+ ".csv\'"
						+ " FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' escaped by '\"'  lines terminated by  '\\r\\n';";
				System.out.println(tableBackupSql);
				// String tableBackupSql1
				// ="SELECT * FROM TSI_20140310_320500_0_20120701 INTO OUTFILE '/APP/niuxinzan/123.csv'FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
				// LINES TERMINATED BY '\n';

				// 直接執行sql語句,備份表
				operate.executeQuery(tableBackupSql);
				// 將sql語句保存到文件中,以備批量複製到mysql命令行中用。
				System.out.println(tableBackupSql);
				// File outfile1 = new File(outfile);
				// BufferedWriter bw = new BufferedWriter(new
				// FileWriter(outfile1,
				// true));
				// bw.write(tableBackupSql);
				// bw.write("\r\n");
				// bw.flush();
				// bw.close();
			}
			operate.close();
			LOG.info("------out to csv successful!-----");
		}

	}

}

最後將outfile的csv文件裏面的語句全部複製到mysql命令行中,執行完畢即可。或者,不用複製到mysql命令行,而是直接在程序中執行mysql命令也可。

二數據庫還原

package cn.com.mysqlLoad;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import cn.com.mysqlBack.Operate;
import cn.com.mysqlBack.TestMain;

public class loadCSVToMysql {
	public static Logger LOG = Logger.getLogger(loadCSVToMysql.class);

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		// System.out.println("lines terminated by '\\r\\n';");
		ResourceBundle bundle = ResourceBundle.getBundle("config",
				Locale.getDefault());
		String filePath = bundle.getString("loadinputfile");
		System.out.println("filePath: " + filePath);
		Operate operate = new Operate();
		String sqlDB = "use " + bundle.getString("DBofload");
		System.out.println("sqlDB: " + sqlDB);
		operate.execute(sqlDB);
		File file = new File(filePath);
		if (file.isFile() && file.exists()) {
			InputStreamReader read = new InputStreamReader(new FileInputStream(
					file));
			BufferedReader bufferedReader = new BufferedReader(read);
			String lineTxt = null;
			while ((lineTxt = bufferedReader.readLine()) != null) {
				String DROPtable = "";
				String createtable1 = "";
				if (lineTxt.contains("BOTTLENECK")) {
					DROPtable = "DROP TABLE IF EXISTS " + lineTxt;
					createtable1 = "CREATE TABLE "
							+ lineTxt
							..................
							+ " KEY idx_linkID (linkID)) ENGINE=myisam DEFAULT CHARSET=utf8;";

					System.out.println("create table sqlString: "
							+ createtable1);
				}
				if (lineTxt.contains("PASSABLEROUTEDETAIL")) {
					DROPtable = "DROP TABLE IF EXISTS " + lineTxt;
					createtable1 = "CREATE TABLE "
							+ lineTxt
							+ "("
							.............
							+ ") ENGINE=myisam DEFAULT CHARSET=utf8;";
				}
				if (lineTxt.contains("RTIC_INFO")) {
					DROPtable = "DROP TABLE IF EXISTS " + lineTxt;
					createtable1 = "CREATE TABLE "
							.................

					System.out.println("create table sqlString: "
							+ createtable1);
				}
				if (lineTxt.contains("RTIC_ROAD")) {

					DROPtable = "DROP TABLE IF EXISTS " + lineTxt;
					createtable1 = "CREATE TABLE " + lineTxt + "("
							
+..........
+ ") ENGINE=myisam DEFAULT CHARSET=utf8;";System.out.println("create table sqlString: "+ createtable1);}if (lineTxt.contains("RTICLINKINFO")) {DROPtable = "DROP TABLE IF EXISTS " + lineTxt;createtable1 = "CREATE TABLE " + lineTxt + "(".........................+ ") ENGINE=myisam DEFAULT CHARSET=utf8;";System.out.println("create table sqlString: "+ createtable1);}if (lineTxt.contains("TRAFFICVOLUME")) {DROPtable = "DROP TABLE IF EXISTS " + lineTxt;createtable1 = "CREATE TABLE "+ lineTxt............+ ") ENGINE=myisam DEFAULT CHARSET=utf8;";System.out.println("create table sqlString: "+ createtable1);}if (lineTxt.contains("TSI_")) {DROPtable = "DROP TABLE IF EXISTS " + lineTxt;createtable1 = "CREATE TABLE "+ lineTxt+ "("..................+ ") ENGINE=myisam DEFAULT CHARSET=utf8;";System.out.println("create table sqlString: "+ createtable1);}if (lineTxt.contains("user_info")) {DROPtable = "DROP TABLE IF EXISTS " + lineTxt;createtable1 = "CREATE TABLE " + lineTxt + "("+ "id int(11) NOT NULL AUTO_INCREMENT,".............+ ") ENGINE=myisam DEFAULT CHARSET=utf8;";System.out.println("drop table sqlString: " + DROPtable);System.out.println("create table sqlString: "+ createtable1);}operate.execute(DROPtable);operate.execute(createtable1);String tableBackupSql = "load data infile "+ "\'/mnt/80.50store/BD_mysql/"+ bundle.getString("DBofcsv")+ "/"+ lineTxt+ ".csv\'"+ " into table "+ lineTxt+ " fields terminated by ',' optionally enclosed by '\"' escaped by'\"' lines terminated by '\\r\\n';";System.out.println("load table sqlString:" + tableBackupSql);operate.execute(tableBackupSql);}operate.close();LOG.info("------out to csv successful!-----");}}}





發佈了68 篇原創文章 · 獲贊 12 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章