2013-10-17 05:14:46 +02:00
|
|
|
package fr.xephi.authme;
|
2013-03-09 03:42:17 +01:00
|
|
|
|
2016-01-30 13:19:05 +01:00
|
|
|
import fr.xephi.authme.datasource.DataSource;
|
|
|
|
import fr.xephi.authme.settings.NewSetting;
|
|
|
|
import fr.xephi.authme.settings.properties.BackupSettings;
|
|
|
|
import fr.xephi.authme.settings.properties.DatabaseSettings;
|
|
|
|
import fr.xephi.authme.util.StringUtils;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2013-03-09 03:42:17 +01:00
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Date;
|
2013-10-17 05:14:46 +02:00
|
|
|
|
2013-03-09 03:42:17 +01:00
|
|
|
/**
|
2015-11-23 20:20:42 +01:00
|
|
|
* The backup management class
|
2013-03-09 03:42:17 +01:00
|
|
|
*
|
|
|
|
* @author stefano
|
|
|
|
*/
|
|
|
|
public class PerformBackup {
|
2013-04-13 01:27:23 +02:00
|
|
|
|
2016-01-30 13:19:05 +01:00
|
|
|
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd_HH-mm");
|
|
|
|
|
|
|
|
private final String dbName;
|
|
|
|
private final String dbUserName;
|
|
|
|
private final String dbPassword;
|
|
|
|
private final String tblname;
|
|
|
|
private final String path;
|
|
|
|
private final File dataFolder;
|
|
|
|
private final NewSetting settings;
|
2014-08-08 23:14:56 +02:00
|
|
|
|
2015-11-21 01:27:06 +01:00
|
|
|
/**
|
|
|
|
* Constructor for PerformBackup.
|
2015-11-23 20:20:42 +01:00
|
|
|
*
|
2015-11-21 01:27:06 +01:00
|
|
|
* @param instance AuthMe
|
|
|
|
*/
|
2016-01-30 13:19:05 +01:00
|
|
|
public PerformBackup(AuthMe instance, NewSetting settings) {
|
|
|
|
this.dataFolder = instance.getDataFolder();
|
|
|
|
this.settings = settings;
|
|
|
|
this.dbName = settings.getProperty(DatabaseSettings.MYSQL_DATABASE);
|
|
|
|
this.dbUserName = settings.getProperty(DatabaseSettings.MYSQL_USERNAME);
|
|
|
|
this.dbPassword = settings.getProperty(DatabaseSettings.MYSQL_PASSWORD);
|
|
|
|
this.tblname = settings.getProperty(DatabaseSettings.MYSQL_TABLE);
|
|
|
|
|
|
|
|
String dateString = DATE_FORMAT.format(new Date());
|
|
|
|
this.path = StringUtils.join(File.separator,
|
|
|
|
instance.getDataFolder().getPath(), "backups", "backup" + dateString);
|
2014-08-08 23:14:56 +02:00
|
|
|
}
|
2013-04-13 01:27:23 +02:00
|
|
|
|
2015-11-23 20:20:42 +01:00
|
|
|
/**
|
2015-11-23 20:59:25 +01:00
|
|
|
* Perform a backup with the given reason.
|
2015-11-23 20:20:42 +01:00
|
|
|
*
|
2016-01-30 13:19:05 +01:00
|
|
|
* @param cause The cause of the backup.
|
2015-11-23 20:20:42 +01:00
|
|
|
*/
|
|
|
|
public void doBackup(BackupCause cause) {
|
2016-01-30 13:19:05 +01:00
|
|
|
if (!settings.getProperty(BackupSettings.ENABLED)) {
|
|
|
|
ConsoleLogger.showError("Can't perform a Backup: disabled in configuration. Cause of the Backup: "
|
|
|
|
+ cause.name());
|
2015-11-23 20:59:25 +01:00
|
|
|
}
|
2015-11-23 20:20:42 +01:00
|
|
|
// Check whether a backup should be made at the specified point in time
|
2016-01-30 13:19:05 +01:00
|
|
|
if (BackupCause.START.equals(cause) && !settings.getProperty(BackupSettings.ON_SERVER_START)
|
|
|
|
|| BackupCause.STOP.equals(cause) && !settings.getProperty(BackupSettings.ON_SERVER_STOP)) {
|
|
|
|
return;
|
2015-11-23 20:20:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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());
|
2015-11-23 20:20:42 +01:00
|
|
|
} else {
|
2015-11-23 20:59:25 +01:00
|
|
|
ConsoleLogger.showError("Error while performing a backup! Cause of the Backup: " + cause.name());
|
2015-11-23 20:20:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-15 19:07:34 +02:00
|
|
|
public boolean doBackup() {
|
2016-01-30 13:19:05 +01:00
|
|
|
DataSource.DataSourceType dataSourceType = settings.getProperty(DatabaseSettings.BACKEND);
|
|
|
|
switch (dataSourceType) {
|
2014-08-08 23:14:56 +02:00
|
|
|
case FILE:
|
2016-01-30 13:19:05 +01:00
|
|
|
return fileBackup("auths.db");
|
2014-08-08 23:14:56 +02:00
|
|
|
case MYSQL:
|
2016-01-30 13:19:05 +01:00
|
|
|
return mySqlBackup();
|
2015-09-13 15:01:22 +02:00
|
|
|
case SQLITE:
|
2016-01-30 13:19:05 +01:00
|
|
|
return fileBackup(dbName + ".db");
|
|
|
|
default:
|
|
|
|
ConsoleLogger.showError("Unknown data source type '" + dataSourceType + "' for backup");
|
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;
|
|
|
|
}
|
2013-04-13 01:27:23 +02:00
|
|
|
|
2016-01-30 13:19:05 +01:00
|
|
|
private boolean mySqlBackup() {
|
|
|
|
File dirBackup = new File(dataFolder + File.separator + "backups");
|
2014-08-08 23:14:56 +02:00
|
|
|
|
2016-01-30 13:19:05 +01:00
|
|
|
if (!dirBackup.exists()) {
|
2014-08-25 03:12:28 +02:00
|
|
|
dirBackup.mkdir();
|
2016-01-30 13:19:05 +01:00
|
|
|
}
|
|
|
|
String backupWindowsPath = settings.getProperty(BackupSettings.MYSQL_WINDOWS_PATH);
|
|
|
|
if (checkWindows(backupWindowsPath)) {
|
|
|
|
String executeCmd = 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
|
|
|
}
|
2016-01-30 13:19:05 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
ConsoleLogger.showError("Error during backup: " + StringUtils.formatException(e));
|
|
|
|
ConsoleLogger.writeStackTrace(e);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
ConsoleLogger.showError("Backup was interrupted: " + StringUtils.formatException(e));
|
|
|
|
ConsoleLogger.writeStackTrace(e);
|
2014-08-08 23:14:56 +02:00
|
|
|
}
|
|
|
|
} else {
|
2014-08-25 03:12:28 +02:00
|
|
|
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
|
|
|
}
|
2016-01-30 13:19:05 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
ConsoleLogger.showError("Error during backup: " + StringUtils.formatException(e));
|
|
|
|
ConsoleLogger.writeStackTrace(e);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
ConsoleLogger.showError("Backup was interrupted: " + StringUtils.formatException(e));
|
|
|
|
ConsoleLogger.writeStackTrace(e);
|
2014-08-08 23:14:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-13 01:27:23 +02:00
|
|
|
|
2016-01-30 13:19:05 +01:00
|
|
|
private boolean fileBackup(String backend) {
|
|
|
|
File dirBackup = new File(dataFolder + File.separator + "backups");
|
2014-08-08 23:14:56 +02:00
|
|
|
|
2014-08-25 03:12:28 +02:00
|
|
|
if (!dirBackup.exists())
|
|
|
|
dirBackup.mkdir();
|
2013-04-13 01:27:23 +02:00
|
|
|
|
2013-03-09 03:42:17 +01:00
|
|
|
try {
|
2016-01-30 13:19:05 +01:00
|
|
|
copy("plugins" + File.separator + "AuthMe" + File.separator + backend, path + ".db");
|
2014-08-08 23:14:56 +02:00
|
|
|
return true;
|
2016-01-30 13:19:05 +01:00
|
|
|
} catch (IOException ex) {
|
|
|
|
ConsoleLogger.showError("Encountered an error during file backup: " + StringUtils.formatException(ex));
|
|
|
|
ConsoleLogger.writeStackTrace(ex);
|
2013-03-09 03:42:17 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-13 01:27:23 +02:00
|
|
|
|
2015-11-21 01:27:06 +01:00
|
|
|
/**
|
2016-01-30 13:19:05 +01:00
|
|
|
* Check if we are under Windows and correct location of mysqldump.exe
|
|
|
|
* otherwise return error.
|
2015-11-23 21:46:34 +01:00
|
|
|
*
|
2016-01-30 13:19:05 +01:00
|
|
|
* @param windowsPath The path to check
|
|
|
|
* @return True if the path is correct, false if it is incorrect or the OS is not Windows
|
2015-11-23 20:20:42 +01:00
|
|
|
*/
|
2016-01-30 13:19:05 +01:00
|
|
|
private static boolean checkWindows(String windowsPath) {
|
2014-08-08 23:14:56 +02:00
|
|
|
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 {
|
2016-01-30 13:19:05 +01:00
|
|
|
ConsoleLogger.showError("Mysql Windows Path is incorrect. Please check it");
|
|
|
|
return false;
|
2014-08-08 23:14:56 +02:00
|
|
|
}
|
2016-01-30 13:19:05 +01:00
|
|
|
}
|
|
|
|
return false;
|
2013-03-09 03:42:17 +01:00
|
|
|
}
|
2014-08-08 23:14:56 +02:00
|
|
|
|
2016-01-30 13:19:05 +01:00
|
|
|
private static void copy(String src, String dst) throws IOException {
|
2014-08-08 23:14:56 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-21 01:27:06 +01:00
|
|
|
/**
|
2015-11-23 20:20:42 +01:00
|
|
|
* Possible backup causes.
|
|
|
|
*/
|
|
|
|
public enum BackupCause {
|
|
|
|
START,
|
|
|
|
STOP,
|
2016-01-30 13:19:05 +01:00
|
|
|
COMMAND
|
2014-08-08 23:14:56 +02:00
|
|
|
}
|
2013-04-13 01:27:23 +02:00
|
|
|
|
2013-03-09 03:42:17 +01:00
|
|
|
}
|