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

200 lines
7.0 KiB
Java
Raw Normal View History

package fr.xephi.authme;
2013-03-09 03:42:17 +01:00
import fr.xephi.authme.datasource.DataSourceType;
import fr.xephi.authme.settings.Settings;
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-03-09 03:42:17 +01:00
/**
* The backup management class
2013-03-09 03:42:17 +01:00
*
* @author stefano
*/
public class PerformBackup {
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 Settings settings;
2014-08-08 23:14:56 +02:00
2015-11-21 01:27:06 +01:00
/**
* Constructor for PerformBackup.
*
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
*/
public PerformBackup(AuthMe instance, Settings 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 = String.join(File.separator,
instance.getDataFolder().getPath(), "backups", "backup" + dateString);
2014-08-08 23:14:56 +02:00
}
/**
2015-11-23 20:59:25 +01:00
* Perform a backup with the given reason.
*
* @param cause The cause of the backup.
*/
public void doBackup(BackupCause cause) {
if (!settings.getProperty(BackupSettings.ENABLED)) {
// Print a warning if the backup was requested via command or by another plugin
if (cause == BackupCause.COMMAND || cause == BackupCause.OTHER) {
ConsoleLogger.warning("Can't perform a Backup: disabled in configuration. Cause of the Backup: "
+ cause.name());
}
return;
2015-11-23 20:59:25 +01:00
}
// Check whether a backup should be made at the specified point in time
if (BackupCause.START.equals(cause) && !settings.getProperty(BackupSettings.ON_SERVER_START)
|| BackupCause.STOP.equals(cause) && !settings.getProperty(BackupSettings.ON_SERVER_STOP)) {
return;
}
// 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 {
ConsoleLogger.warning("Error while performing a backup! Cause of the Backup: " + cause.name());
}
}
2015-09-15 19:07:34 +02:00
public boolean doBackup() {
DataSourceType dataSourceType = settings.getProperty(DatabaseSettings.BACKEND);
switch (dataSourceType) {
2014-08-08 23:14:56 +02:00
case FILE:
return fileBackup("auths.db");
2014-08-08 23:14:56 +02:00
case MYSQL:
return mySqlBackup();
2015-09-13 15:01:22 +02:00
case SQLITE:
return fileBackup(dbName + ".db");
default:
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;
}
private boolean mySqlBackup() {
File dirBackup = new File(dataFolder + File.separator + "backups");
2014-08-08 23:14:56 +02:00
if (!dirBackup.exists()) {
dirBackup.mkdir();
}
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 {
ConsoleLogger.warning("Could not create the backup! (Windows)");
2014-08-08 23:14:56 +02:00
}
} catch (IOException | InterruptedException e) {
ConsoleLogger.logException("Error during Windows backup:", e);
2014-08-08 23:14:56 +02:00
}
} 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 {
ConsoleLogger.warning("Could not create the backup!");
2014-08-08 23:14:56 +02:00
}
} catch (IOException | InterruptedException e) {
ConsoleLogger.logException("Error during backup:", e);
2014-08-08 23:14:56 +02:00
}
}
return false;
}
private boolean fileBackup(String backend) {
File dirBackup = new File(dataFolder + File.separator + "backups");
2014-08-08 23:14:56 +02:00
if (!dirBackup.exists())
dirBackup.mkdir();
2013-03-09 03:42:17 +01:00
try {
copy("plugins" + File.separator + "AuthMe" + File.separator + backend, path + ".db");
2014-08-08 23:14:56 +02:00
return true;
} catch (IOException ex) {
ConsoleLogger.logException("Encountered an error during file backup:", ex);
2013-03-09 03:42:17 +01:00
}
return false;
}
2015-11-21 01:27:06 +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
*
* @param windowsPath The path to check
* @return True if the path is correct, false if it is incorrect or the OS is not Windows
*/
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 {
ConsoleLogger.warning("Mysql Windows Path is incorrect. Please check it");
return false;
2014-08-08 23:14:56 +02:00
}
}
return false;
2013-03-09 03:42:17 +01:00
}
2014-08-08 23:14:56 +02: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
/**
* 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
}