AuthMeReloaded/src/main/java/fr/xephi/authme/PerformBackup.java

229 lines
6.4 KiB
Java
Raw Normal View History

package fr.xephi.authme;
2013-03-09 03:42:17 +01:00
import fr.xephi.authme.settings.Settings;
import java.io.*;
2013-03-09 03:42:17 +01:00
import java.text.SimpleDateFormat;
import java.util.Date;
2013-03-09 03:42:17 +01:00
/**
* The backup management class
2013-03-09 03:42:17 +01:00
*
* @author stefano
2015-11-21 01:27:06 +01:00
* @version $Revision: 1.0 $
2013-03-09 03:42:17 +01:00
*/
public class PerformBackup {
final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH-mm");
final String dateString = format.format(new Date());
private final String dbName = Settings.getMySQLDatabase;
private final String dbUserName = Settings.getMySQLUsername;
private final String dbPassword = Settings.getMySQLPassword;
private final String tblname = Settings.getMySQLTablename;
private final String path = AuthMe.getInstance().getDataFolder() + File.separator + "backups" + File.separator + "backup" + dateString;
2014-08-08 23:14:56 +02:00
private AuthMe instance;
2015-11-21 01:27:06 +01:00
/**
* Constructor for PerformBackup.
*
2015-11-21 01:27:06 +01:00
* @param instance AuthMe
*/
2014-08-08 23:14:56 +02:00
public PerformBackup(AuthMe instance) {
this.setInstance(instance);
}
/**
2015-11-23 20:59:25 +01:00
* Perform a backup with the given reason.
*
2015-11-23 20:59:25 +01:00
* @param cause BackupCause The cause of the backup.
*/
public void doBackup(BackupCause cause) {
if (!Settings.isBackupActivated) {
2015-11-23 20:59:25 +01:00
ConsoleLogger.showError("Can't perform a Backup: disabled in configuration. Cause of the Backup: " + cause.name());
}
// Check whether a backup should be made at the specified point in time
switch (cause) {
case START:
if (!Settings.isBackupOnStart)
return;
case STOP:
if (!Settings.isBackupOnStop)
return;
case COMMAND:
case OTHER:
}
// Do backup and check return value!
if (doBackup()) {
2015-11-23 20:59:25 +01:00
ConsoleLogger.info("A backup has been performed successfully. Cause of the Backup: " + cause.name());
} else {
2015-11-23 20:59:25 +01:00
ConsoleLogger.showError("Error while performing a backup! Cause of the Backup: " + cause.name());
}
}
2015-11-21 01:27:06 +01:00
/**
* Method doBackup.
*
* @return boolean
*/
2015-09-15 19:07:34 +02:00
public boolean doBackup() {
2014-08-08 23:14:56 +02:00
switch (Settings.getDataSource) {
case FILE:
return FileBackup("auths.db");
case MYSQL:
return MySqlBackup();
2015-09-13 15:01:22 +02:00
case SQLITE:
2015-09-08 17:33:52 +02:00
return FileBackup(Settings.getMySQLDatabase + ".db");
2014-08-08 23:14:56 +02:00
}
2013-03-09 03:42:17 +01:00
2014-08-08 23:14:56 +02:00
return false;
}
2015-11-21 01:27:06 +01:00
/**
* Method MySqlBackup.
*
* @return boolean
*/
2014-08-08 23:14:56 +02:00
private boolean MySqlBackup() {
File dirBackup = new File(AuthMe.getInstance().getDataFolder() + "/backups");
2014-08-08 23:14:56 +02:00
if (!dirBackup.exists())
dirBackup.mkdir();
2014-08-08 23:14:56 +02:00
if (checkWindows(Settings.backupWindowsPath)) {
String executeCmd = Settings.backupWindowsPath + "\\bin\\mysqldump.exe -u " + dbUserName + " -p" + dbPassword + " " + dbName + " --tables " + tblname + " -r " + path + ".sql";
2014-08-08 23:14:56 +02:00
Process runtimeProcess;
try {
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
2015-07-16 21:16:58 +02:00
ConsoleLogger.info("Backup created successfully.");
2014-08-08 23:14:56 +02:00
return true;
} else {
2015-07-16 21:16:58 +02:00
ConsoleLogger.showError("Could not create the backup!");
2014-08-08 23:14:56 +02:00
}
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
String executeCmd = "mysqldump -u " + dbUserName + " -p" + dbPassword + " " + dbName + " --tables " + tblname + " -r " + path + ".sql";
2014-08-08 23:14:56 +02:00
Process runtimeProcess;
try {
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
2015-07-16 21:16:58 +02:00
ConsoleLogger.info("Backup created successfully.");
2014-08-08 23:14:56 +02:00
return true;
} else {
2015-07-16 21:16:58 +02:00
ConsoleLogger.showError("Could not create the backup!");
2014-08-08 23:14:56 +02:00
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return false;
}
2015-11-21 01:27:06 +01:00
/**
* Method FileBackup.
*
2015-11-21 01:27:06 +01:00
* @param backend String
2015-11-23 21:46:34 +01:00
*
* @return boolean
*/
2014-08-08 23:14:56 +02:00
private boolean FileBackup(String backend) {
File dirBackup = new File(AuthMe.getInstance().getDataFolder() + "/backups");
2014-08-08 23:14:56 +02:00
if (!dirBackup.exists())
dirBackup.mkdir();
2013-03-09 03:42:17 +01:00
try {
copy(new File("plugins" + File.separator + "AuthMe" + File.separator + backend), new File(path + ".db"));
2014-08-08 23:14:56 +02:00
return true;
2013-03-09 03:42:17 +01:00
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
2015-11-21 01:27:06 +01:00
/**
* Method checkWindows.
*
2015-11-21 01:27:06 +01:00
* @param windowsPath String
2015-11-23 21:46:34 +01:00
*
* @return boolean
*/
2014-08-08 23:14:56 +02:00
private boolean checkWindows(String windowsPath) {
String isWin = System.getProperty("os.name").toLowerCase();
2015-11-23 21:43:40 +01:00
if (isWin.contains("win")) {
2014-08-08 23:14:56 +02:00
if (new File(windowsPath + "\\bin\\mysqldump.exe").exists()) {
return true;
} else {
ConsoleLogger.showError("Mysql Windows Path is incorrect please check it");
2014-08-08 23:14:56 +02:00
return true;
}
} else return false;
2013-03-09 03:42:17 +01:00
}
2014-08-08 23:14:56 +02:00
/*
* Check if we are under Windows and correct location of mysqldump.exe
* otherwise return error.
2014-08-08 23:14:56 +02:00
*/
2015-11-21 01:27:06 +01:00
/**
* Method copy.
*
2015-11-21 01:27:06 +01:00
* @param src File
* @param dst File
2015-11-23 21:46:34 +01:00
*
* @throws IOException
*/
2014-08-08 23:14:56 +02:00
void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
2013-03-09 03:42:17 +01:00
}
/*
* Copyr src bytefile into dst file
*/
/**
* Method getInstance.
*
* @return AuthMe
*/
public AuthMe getInstance() {
return instance;
}
2015-11-21 01:27:06 +01:00
/**
* Method setInstance.
*
2015-11-21 01:27:06 +01:00
* @param instance AuthMe
*/
2014-08-08 23:14:56 +02:00
public void setInstance(AuthMe instance) {
this.instance = instance;
}
2013-03-09 03:42:17 +01:00
2015-11-21 01:27:06 +01:00
/**
* Possible backup causes.
*/
public enum BackupCause {
START,
STOP,
COMMAND,
OTHER,
2014-08-08 23:14:56 +02:00
}
2013-03-09 03:42:17 +01:00
}