2013-10-17 05:14:46 +02:00
|
|
|
package fr.xephi.authme;
|
2013-03-09 03:42:17 +01:00
|
|
|
|
2015-11-23 20:20:42 +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-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
|
2015-11-21 01:27:06 +01:00
|
|
|
* @version $Revision: 1.0 $
|
2013-03-09 03:42:17 +01:00
|
|
|
*/
|
|
|
|
public class PerformBackup {
|
2013-04-13 01:27:23 +02:00
|
|
|
|
2015-11-23 20:20:42 +01:00
|
|
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH-mm");
|
|
|
|
String dateString = format.format(new Date());
|
2014-08-08 23:14:56 +02:00
|
|
|
private String dbName = Settings.getMySQLDatabase;
|
|
|
|
private String dbUserName = Settings.getMySQLUsername;
|
|
|
|
private String dbPassword = Settings.getMySQLPassword;
|
|
|
|
private String tblname = Settings.getMySQLTablename;
|
2014-08-25 03:12:28 +02:00
|
|
|
private 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-23 20:20:42 +01:00
|
|
|
*
|
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);
|
|
|
|
}
|
2013-04-13 01:27:23 +02:00
|
|
|
|
2015-11-23 20:20:42 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public void doBackup(BackupCause cause) {
|
|
|
|
// 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()) {
|
|
|
|
ConsoleLogger.info("A backup has been performed successfully");
|
|
|
|
} else {
|
|
|
|
ConsoleLogger.showError("Error while performing a backup!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-21 01:27:06 +01:00
|
|
|
/**
|
|
|
|
* Method doBackup.
|
2015-11-23 20:20:42 +01:00
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2015-09-15 19:07:34 +02:00
|
|
|
public boolean doBackup() {
|
2013-04-13 01:27:23 +02:00
|
|
|
|
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;
|
|
|
|
}
|
2013-04-13 01:27:23 +02:00
|
|
|
|
2015-11-21 01:27:06 +01:00
|
|
|
/**
|
|
|
|
* Method MySqlBackup.
|
2015-11-23 20:20:42 +01:00
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2014-08-08 23:14:56 +02:00
|
|
|
private boolean MySqlBackup() {
|
2014-08-25 03:12:28 +02:00
|
|
|
File dirBackup = new File(AuthMe.getInstance().getDataFolder() + "/backups");
|
2014-08-08 23:14:56 +02:00
|
|
|
|
2014-08-25 03:12:28 +02:00
|
|
|
if (!dirBackup.exists())
|
|
|
|
dirBackup.mkdir();
|
2014-08-08 23:14:56 +02:00
|
|
|
if (checkWindows(Settings.backupWindowsPath)) {
|
2014-08-25 03:12:28 +02:00
|
|
|
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 {
|
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
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-13 01:27:23 +02:00
|
|
|
|
2015-11-21 01:27:06 +01:00
|
|
|
/**
|
|
|
|
* Method FileBackup.
|
2015-11-23 20:20:42 +01:00
|
|
|
*
|
2015-11-21 01:27:06 +01:00
|
|
|
* @param backend String
|
2015-11-23 20:20:42 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2014-08-08 23:14:56 +02:00
|
|
|
private boolean FileBackup(String backend) {
|
2014-08-25 03:12:28 +02:00
|
|
|
File dirBackup = new File(AuthMe.getInstance().getDataFolder() + "/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 {
|
2014-08-25 03:12:28 +02:00
|
|
|
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;
|
|
|
|
}
|
2013-04-13 01:27:23 +02:00
|
|
|
|
2015-11-21 01:27:06 +01:00
|
|
|
/**
|
|
|
|
* Method checkWindows.
|
2015-11-23 20:20:42 +01:00
|
|
|
*
|
2015-11-21 01:27:06 +01:00
|
|
|
* @param windowsPath String
|
2015-11-23 20:20:42 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2014-08-08 23:14:56 +02:00
|
|
|
private boolean checkWindows(String windowsPath) {
|
|
|
|
String isWin = System.getProperty("os.name").toLowerCase();
|
|
|
|
if (isWin.indexOf("win") >= 0) {
|
|
|
|
if (new File(windowsPath + "\\bin\\mysqldump.exe").exists()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
2014-08-25 03:12:28 +02:00
|
|
|
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
|
|
|
|
|
|
|
/*
|
2015-11-23 20:20:42 +01: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-23 20:20:42 +01:00
|
|
|
|
2015-11-21 01:27:06 +01:00
|
|
|
/**
|
|
|
|
* Method copy.
|
2015-11-23 20:20:42 +01:00
|
|
|
*
|
2015-11-21 01:27:06 +01:00
|
|
|
* @param src File
|
|
|
|
* @param dst File
|
2015-11-23 20:20:42 +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
|
|
|
}
|
|
|
|
|
2015-11-23 20:20:42 +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-23 20:20:42 +01:00
|
|
|
*
|
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
|
|
|
/**
|
2015-11-23 20:20:42 +01:00
|
|
|
* Possible backup causes.
|
|
|
|
*/
|
|
|
|
public enum BackupCause {
|
|
|
|
START,
|
|
|
|
STOP,
|
|
|
|
COMMAND,
|
|
|
|
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
|
|
|
}
|