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

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

/*
 * 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 com.jk.core.logging.JKLogger;
import com.jk.core.logging.JKLoggerFactory;
import com.jk.core.util.JK;
import com.jk.core.util.JKCompressionUtil;
import com.jk.data.dataaccess.JKDataAccessFactory;
import com.jk.data.datasource.JKDataSource;

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

	/** The Constant BACKUPS_FOLDER. */
	public static final String BACKUPS_FOLDER = "backups-folder";

	/** The Constant IS_AUTOMATIC_BACKUP. */
	public static final String IS_AUTOMATIC_BACKUP = "auto-backup";

	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main(final String[] args) {
		// DataBaseBackup dataBaseBackup = new DataBaseBackup();
		// dataBaseBackup.start();
		// GeneralUtility.deleteFile("C:/Documents and
		// Settings/jamil/Desktop/mrk/zxczxczxc.sql");
	}

	/** The data source. */
	private final JKDataSource dataSource;

	/** The compress. */
	private boolean compress;

	/** The output path. */
	private String outputPath;

	/** The logger. */
	private JKLogger logger = JKLoggerFactory.getLogger(getClass());

	/**
	 * Instantiates a new data base backup.
	 */
	// /////////////////////////////////////////////////////////
	public DataBaseBackup() {
		this(JKDataAccessFactory.getDefaultDataSource());
	}

	/**
	 * Instantiates a new data base backup.
	 *
	 * @param dataSource the data source
	 */
	// /////////////////////////////////////////////////////////
	public DataBaseBackup(final JKDataSource dataSource) {
		this.dataSource = dataSource;

	}

	/**
	 * Gets the output path.
	 *
	 * @return the output path
	 */
	public String getOutputPath() {
		return this.outputPath;
	}

	/**
	 * Checks if is compress.
	 *
	 * @return true, if is compress
	 */
	// /////////////////////////////////////////////////////////
	public boolean isCompress() {
		return this.compress;
	}

	/**
	 * Sets the compress.
	 *
	 * @param compress the new compress
	 */
	// /////////////////////////////////////////////////////////
	public void setCompress(final boolean compress) {
		this.compress = compress;
	}

	/**
	 * Sets the output path.
	 *
	 * @param outputPath the new output path
	 */
	public void setOutputPath(final String outputPath) {
		this.outputPath = outputPath;
	}

	/**
	 * Start.
	 */
	// /////////////////////////////////////////////////////////
	public void start() {
		JK.implementMe();
		final DatabaseInfo info = new DatabaseInfo(this.dataSource);
		final String path = getOutputPath();
		if (!path.equals("")) {
			final String logFilePath = getOutputPath() + ".log";
			JK.log("Before execution the dumb ", logFilePath);
			final String sqlPath = path + ".sql";
			info.setFileName(sqlPath);
			final String dbCompressedFileName = path + ".zip";
			MySqlUtil.export(info);
			JK.log("After execution the dumb ", logFilePath);
			JK.log("Before Add AutoCommit ", logFilePath);
//			GeneralUtility.addAutocommitFalse(sqlPath);
//			GeneralUtility.writeLog("After Add AutoCommit ", logFilePath);
			if (isCompress()) {
				JK.log("Before compress", logFilePath);
				JKCompressionUtil.compress(info.getFileName(), dbCompressedFileName, info.getDatabasePassword());
//				 JKCompressionUtil.compressAndSetPassword(info.getFileName(),info.getDatabasePassword());
				JK.log("after compress", logFilePath);
				logger.info("Done Compress sql file");
//				JKIOUtil.deleteFile(getOutputPath() + ".sql");
				logger.info("After delete sql file");
			}
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy