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

com.jk.data.backup.AutomaticDBBackup 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.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import com.jk.core.exceptions.handler.JKExceptionUtil;
import com.jk.core.util.JK;
import com.jk.core.util.JKDateTimeUtil;
import com.jk.core.util.JKIOUtil;

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

	/** The default backup folder. */
	private static String DEFAULT_BACKUP_FOLDER = "backups";

	////////////////////////////////////////////////////////////////////////////////////
	// todo: should be moved to utility calss because it use in several places
	//
	/**
	 * Gets the property.
	 *
	 * @return the property
	 */
//	////////////////////////////////////////////////////////////////////////////////////
//	public static String getProperty(final String propName, final String defultValue) {
//		return JKDataAccessFactory.getDefaultDataSource().getProperty(propName, defultValue);
//	}

	/**
	 * Process database autobackup.
	 */
	////////////////////////////////////////////////////////////////////////
	public static void processDatabaseAutobackup() {
		final Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					final AutomaticDBBackup backup = new AutomaticDBBackup();
					backup.startBackup(false);
				} catch (final Exception e) {
					JKExceptionUtil.handle(e);
				}
			}
		});
		thread.setPriority(Thread.MIN_PRIORITY);
		thread.start();
	}

	/**
	 * Instantiates a new automatic DB backup.
	 */
	//////////////////////////////////////////////////////////////////////////////////////
	public AutomaticDBBackup() {
		setBackupsFolder();
	}

	/**
	 * Gets the log file name.
	 *
	 * @return the log file name
	 */
	////////////////////////////////////////////////////////////////////////
	private String getLogFileName() {
		return getOutputPath() + ".log";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jk.framework.apps.backup.DataBaseBackup#getOutputPath()
	 */
	/**
	 * Gets the output path.
	 *
	 * @return the output path
	 */
	//////////////////////////////////////////////////////////////////////////////////////
	@Override
	public String getOutputPath() {
		return DEFAULT_BACKUP_FOLDER + File.separator + JKDateTimeUtil.formatCurrentDate();
	}

	/**
	 * Checks if is automatic backup allwoed.
	 *
	 * @return true, if is automatic backup allwoed
	 */
	//////////////////////////////////////////////////////////////////////////////////////
	public boolean isAutomaticBackupAllwoed() {
		// String isAutomaticBackEnabled =
		// getProperty(DataBaseBackup.IS_AUTOMATIC_BACKUP, "false");
		// return Boolean.valueOf(isAutomaticBackEnabled);
		return false;
	}

	/**
	 * Checks if is backup created.
	 *
	 * @return true, if is backup created
	 */
	////////////////////////////////////////////////////////////////////////
	protected boolean isBackupCreated() {
		final boolean fileExist = JKIOUtil.isFileExist(getLogFileName());
		if (fileExist) {
			System.err.println("Backup for today is already created");
		}
		return fileExist;
	}

	/**
	 * Sets the backups folder.
	 */
	//////////////////////////////////////////////////////////////////////////////////////
	public void setBackupsFolder() {
		DEFAULT_BACKUP_FOLDER = JK.getProperty(DataBaseBackup.BACKUPS_FOLDER, "backups");
	}

	/**
	 * Start backup.
	 *
	 * @param overwrite the overwrite
	 */
	////////////////////////////////////////////////////////////////////////////////////
	public void startBackup(final boolean overwrite) {
		if (overwrite || !isBackupCreated()) {
			if (!JK.isDebugMode() && isAutomaticBackupAllwoed()) {
				writeLogFile();
				setCompress(true);
				start();
			}
		}
	}

	/**
	 * Write log file.
	 */
	////////////////////////////////////////////////////////////////////////////////////
	private void writeLogFile() {
		BufferedWriter out = null;
		FileWriter fileWriter = null;
		try {
			JKIOUtil.createDirectory(DEFAULT_BACKUP_FOLDER);
			fileWriter = new FileWriter(getLogFileName());
			out = new BufferedWriter(fileWriter);
		} catch (final IOException e) {
			JKExceptionUtil.handle(e);
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (final IOException e) {
				}
			}
		}

	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy