All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jk.data.backup.MySqlUtil Maven / Gradle / Ivy

Go to download

This contains a set of API's that ease the database programming with Java, in both: JDBC and JPA Persisitnce).

There is a newer version: 7.0.0-M7
Show newest version
/*
 * Copyright 2002-2022 Dr. Jalal Kiswani. 
 * Email: [email protected]
 * Check out https://smart-api.com for more details
 * 
 * All the opensource projects of Dr. Jalal Kiswani are free for personal and academic use only, 
 * for commercial usage and support, please contact the author.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.jk.data.backup;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import com.jk.core.logging.JKLogger;
import com.jk.core.logging.JKLoggerFactory;
import com.jk.core.util.JK;

// TODO: Auto-generated Javadoc
/**
 * The Class MySqlUtil.
 */
public class MySqlUtil {

	/** The logger. */
	static JKLogger logger = JKLoggerFactory.getLogger(MySqlUtil.class);

	/** The Constant EXPORT_UTIL_FILE_NAME. */
	private static final Object EXPORT_UTIL_FILE_NAME = "mysqldump.exe";

	/** The Constant IMPORT_UTIL_FILE_NAME. */
	private static final String IMPORT_UTIL_FILE_NAME = "mysql.exe";

	/**
	 * Execute.
	 *
	 * @param buffer the buffer
	 */
	private static void execute(final StringBuffer buffer) {
		final String command = buffer.toString();
		final String cmd[] = { "cmd", "/c", command };
		Process process = null;
		try {

			process = Runtime.getRuntime().exec(cmd);
			final BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
			String line;
			while ((line = reader.readLine()) != null) {
				logger.debug(line);
			}
			if (process.exitValue() != 0) {
				final BufferedReader buf = new BufferedReader(new InputStreamReader(process.getErrorStream()));
				String errorValue = "";
				while (errorValue != null) {
					errorValue = buf.readLine();
					logger.debug(errorValue);
				}
				buf.close();
				logger.error("MySql Export failed with the following error code : ", process.exitValue());
				throw new IOException(errorValue);
			}
		} catch (Exception e) {
			JK.throww(e);
		} finally {
			if (process != null) {
				process.destroy();
			}
		}
	}

	/**
	 * Export.
	 *
	 * @param info the info
	 */
	public static void export(final DatabaseInfo info) {
		final StringBuffer buffer = new StringBuffer();
		buffer.append(EXPORT_UTIL_FILE_NAME);
		buffer.append(" --skip-opt ");
		buffer.append("--add-drop-table ");
		buffer.append("--create-options ");
		buffer.append("--hex-blob");
		buffer.append(" -h ");
		buffer.append(info.getDatabaseHost());
		buffer.append(" -p");
		buffer.append(info.getDatabasePort());
		buffer.append(" -u ");
		buffer.append(info.getDatabaseUser());
		buffer.append(" -p");
		buffer.append(info.getDatabasePassword() + " ");

		// String[] igonoredTbls={"reg_students","reg_student_eq_courses"};
		// should read from cofig file
		// for (String tblName: igonoredTbls) {
		// buffer.append(" --ignore-table=");
		// buffer.append(info.getDatabaseName()+"."+tblName);
		// }

		buffer.append(" " + info.getDatabaseName());
		buffer.append(" >");
		buffer.append("\"" + info.getFileName() + "\"");
		logger.debug(buffer);
		execute(buffer);
		logger.debug("Done dump sql file : " + info.getFileName());
	}

	/**
	 * Import db.
	 *
	 * @param info the info
	 * @throws IOException Signals that an I/O exception has occurred.
	 */
	public static void importDb(final DatabaseInfo info) throws IOException {
		final StringBuffer buffer = new StringBuffer();
		buffer.append(IMPORT_UTIL_FILE_NAME);

		buffer.append(" --user=");
		buffer.append(info.getDatabaseUser());
		buffer.append(" --password=");
		buffer.append(info.getDatabasePassword());
		buffer.append(" --host=");
		buffer.append(info.getDatabaseHost());

		buffer.append(" " + info.getDatabaseName());
		buffer.append("<");
		buffer.append("\"" + info.getFileName() + "\"");
		logger.debug(buffer);
		execute(buffer);
		logger.debug("Done Import sql file : " + info.getFileName());
	}

	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main(final String[] args) {
		final DatabaseInfo info = new DatabaseInfo();
		info.setDatabaseHost("server");
		info.setDatabaseName("test-base-jo");
		info.setDatabasePassword("jk-dev");
		info.setDatabasePort(3306);
		info.setDatabaseUser("root");
		final String fileName = "c:\\app1-base-jo.sql";
		info.setFileName(fileName);
		export(info);
		System.out.println("Export Done");
		// CompressionUtil.compress(fileName, "c:/test-z.rar");
		System.out.println("Compression done");

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy