数据库备份

mysql数据库备份命令:

备份所有表:

mysqldump -u[username] -p[password] --default-character-set[=character] -h[host] -P[port] [dbName] >"D:/data_backup/bbac.sql"

 备份某些表

mysqldump -u[username] -p[password] --default-character-set[=character] -h[host] -P[port] [dbName] [table1 table2] >"D:/data_backup/bbac.sql"

 如果没有反应,配置环境变量:
设置mysql的环境变量(在path中添加%MYSQL_HOME%\bin),重启命令窗口。
如果是程序调用,需要重启电脑
java备份调用程序:

public static void main(String[] args) {
		try {
			Runtime rt = Runtime.getRuntime();
			String backPath = "D:/data_backup/"
					+ System.currentTimeMillis() + ".sql";
			String username = "name";
			String password = "pwd";
			String host = "host";;
			String port = "port";
			String dbName = "dbname";
			String table = "t_table";
			//备份全部数据空间中的表信息
//			String mysql = "mysqldump -u" + username + " -p"
//					+ password + " --default-character-set=utf8 -h"
//					+ host + " -P" + port + " " + dbName + " >"
//					+ "\"" + backPath + "\"";
			//只备份某些表的信息
			//mysqldump -p[username] -p[password] --default-character-set[=characterset] -h[host] -P[port] [dbname] [table1 table1 ...] >"D:/data_backup/bbac.sql"
			String mysql = "mysqldump -u" + username + " -p"
					+ password + " --default-character-set=utf8 -h"
					+ host + " -P" + port + " " + dbName + " " + table +" >"
					+ "\"" + backPath + "\"";
			Process proc = rt.exec("cmd.exe /c " + mysql);// 设置导出编码为utf8。这里必须是utf8
			// String backExe =
			// ServletActionContext.getServletContext().getRealPath("/")+"bin/mysqldump.exe";
			// String mysql = getDbname()+ " -u" + getUsername()+ " -p" +
			// getPassword() +
			// " --default-character-set=utf8 -h"+getHost()+" -P"+getPORT()+" >"+"\""+backPath+"\"";
			proc.waitFor();// 等待进程终止
		} catch (Exception e) {
			e.printStackTrace();
		}

你可能感兴趣的:(数据库备份)