2013-10-17 05:14:46 +02:00
|
|
|
package fr.xephi.authme;
|
2013-03-09 03:42:17 +01:00
|
|
|
|
2016-02-07 14:27:03 +01:00
|
|
|
import fr.xephi.authme.datasource.DataSourceType;
|
2016-07-23 15:50:40 +02:00
|
|
|
import fr.xephi.authme.settings.Settings;
|
2016-01-30 13:19:05 +01:00
|
|
|
import fr.xephi.authme.settings.properties.BackupSettings;
|
|
|
|
import fr.xephi.authme.settings.properties.DatabaseSettings;
|
|
|
|
|
|
|
|
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;
|
2016-07-23 15:50:40 +02:00
|
|
|
private final Settings 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-02-11 23:09:23 +01:00
|
|
|
* @param settings The plugin settings
|
2015-11-21 01:27:06 +01:00
|
|
|
*/
|
2016-07-23 15:50:40 +02:00
|
|
|
public PerformBackup(AuthMe instance, Settings settings) {
|
2016-01-30 13:19:05 +01:00
|
|
|
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());
|
2016-10-02 12:44:10 +02:00
|
|
|
this.path = String.join(File.separator,
|
2016-01-30 13:19:05 +01:00
|
|
|
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)) {
|
2016-01-29 20:22:15 +01:00
|
|
|
// Print a warning if the backup was requested via command or by another plugin
|
|
|
|
if (cause == BackupCause.COMMAND || cause == BackupCause.OTHER) {
|
2016-07-12 22:05:36 +02:00
|
|
|
ConsoleLogger.warning("Can't perform a Backup: disabled in configuration. Cause of the Backup: "
|
2016-01-31 21:31:42 +01:00
|
|
|
+ cause.name());
|
2016-01-29 20:22:15 +01:00
|
|
|
}
|
|
|
|
return;
|
2015-11-23 20:59:25 +01:00
|
|
|
}
|
2016-01-29 20:22:15 +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 {
|
2016-07-12 22:05:36 +02:00
|
|
|
ConsoleLogger.warning("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-02-07 14:27:03 +01:00
|
|
|
DataSourceType dataSourceType = settings.getProperty(DatabaseSettings.BACKEND);
|
2016-01-30 13:19:05 +01:00
|
|
|
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:
|
2016-07-12 22:05:36 +02:00
|
|
|
ConsoleLogger.warning("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 {
|
2016-07-12 22:05:36 +02:00
|
|
|
ConsoleLogger.warning("Could not create the backup! (Windows)");
|
2014-08-08 23:14:56 +02:00
|
|
|
}
|
2016-02-07 14:27:03 +01:00
|
|
|
} catch (IOException | InterruptedException e) {
|
|
|
|
ConsoleLogger.logException("Error during Windows backup:", 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 {
|
2016-07-12 22:05:36 +02:00
|
|
|
ConsoleLogger.warning("Could not create the backup!");
|
2014-08-08 23:14:56 +02:00
|
|
|
}
|
2016-02-07 14:27:03 +01:00
|
|
|
} catch (IOException | InterruptedException e) {
|
|
|
|
ConsoleLogger.logException("Error during backup:", 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) {
|
2016-07-12 22:05:36 +02:00
|
|
|
ConsoleLogger.logException("Encountered an error during file backup:", 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-07-12 22:05:36 +02:00
|
|
|
ConsoleLogger.warning("Mysql Windows Path is incorrect. Please check it");
|
2016-01-30 13:19:05 +01:00
|
|
|
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,
|
|
|
|
COMMAND,
|
2016-01-31 21:31:42 +01:00
|
|
|
OTHER
|
2014-08-08 23:14:56 +02:00
|
|
|
}
|
2013-04-13 01:27:23 +02:00
|
|
|
|
2013-03-09 03:42:17 +01:00
|
|
|
}
|