Backup of MySQL database Java (using mysqldump) to complete database backup (and bat file backup database)

This article has participated in the "Newcomer Creation Ceremony" event to start the road of gold creation together.

Background needs:

The backup of the database in a normal project must be an indispensable backup of MySQL. Generally, the program mysqldump is used. Good nonsense is not much code! ! !


Java code:

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test{
	
	public static void main(String[] args) {
		boolean backupsDatabase = backupsDatabase();
		System.err.println(backupsDatabase);
	}
	//备份数据库
	public static boolean backupsDatabase() {
	boolean	brt=false;
		Runtime runtime = Runtime.getRuntime();
		String command = getCommand();
		//命令行运行命令
		try {
			runtime.exec(command);
			brt=true;
		} catch (IOException e) {
			System.err.println(e);
			brt=false;
		}
		return brt;
	}
	// 拼接cmd命令正常是要将所需的之传进来的测试我就直接写了
//	private static String getCommand(String username,String password,String host,String port,String exportDatabaseName,String savePath,String MysqlPath) {
		private static String getCommand() {
		StringBuffer stringcmd = new StringBuffer();
		 // 用户名
		 String username = "root";
		// 密码
		 String password = "123456";
		// 数据库所在的主机IP
		 String host = "127.0.0.1";
		// 端口号
		 String port = "3306";
		// 备份数据库的名称
		 String exportDatabaseName = "数据库名";
		 String getdate = getdate();
		// 备份文件储存位置
		 String savePath = "C:/test/"+getdate+".sql";
		 //路径是mysql中 C:/Program Files/MySQL/MySQL Server 5.7/bin默认·······路径
		 String MysqlPath = "C:/Program Files/MySQL/MySQL Server 5.7/bin/";
		// 注意哪些地方要空格,哪些不要空格
		 stringcmd.append(MysqlPath).append("mysqldump -u").append(username).append(" -p").append(password)// 密码是用的小p,而端口是用的大P。
				.append(" -h").append(host).append(" -P").append(port).append(" ").append(exportDatabaseName)
				.append(" -r ").append(savePath);
		return stringcmd.toString();
	}
	
	
	/**
	 * 时间戳转换
	 * @return
	 */
	public static String getdate() {
		Date date = new Date();
		 String dateStr = new SimpleDateFormat("yyyyMMddHHmmss").format(date);
		System.err.println(dateStr);
		return dateStr;
	}
	

}

复制代码

renderings

successful backup

bat backup

@echo off
color 0a
echo.
echo MySQL数据库备份
echo *****************************
echo.
echo 今天是 %date%
echo 时间是 %time%
echo.
echo *****************************
set "Ymd=%date:~,4%%date:~5,2%%date:~8,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
"C:/Program Files/MySQL/MySQL Server 5.7/bin/mysqldump" -uroot -p123456 -h127.0.0.1 -P3306 数据库名 -r C:/test/%Ymd%.sql
echo.
echo MySQL数据库备份完成至C:/test/%Ymd%.sql,请进行检查。。。
echo.
echo.
pause
复制代码

Note that there are spaces in the path of /Program Files, which need to be enclosed in quotation marks! You can use it when you create a new file and save it in bat format.

renderings

It can also be executed with windows scheduled tasksInterested friends can try it out. . .

Guess you like

Origin juejin.im/post/7084057591751376910
Recommended