2013-10-17 05:14:46 +02:00
|
|
|
package fr.xephi.authme;
|
2013-03-09 03:42:17 +01:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Date;
|
2013-10-17 05:14:46 +02:00
|
|
|
|
|
|
|
import fr.xephi.authme.settings.Settings;
|
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
|
|
|
|
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;
|
|
|
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH-mm");
|
|
|
|
String dateString = format.format(new Date());
|
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.
|
|
|
|
* @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-21 01:27:06 +01:00
|
|
|
/**
|
|
|
|
* Method doBackup.
|
2015-11-21 01:31:18 +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-21 01:31:18 +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.
|
|
|
|
* @param backend String
|
2015-11-21 01:31:18 +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
|
|
|
|
2014-08-08 23:14:56 +02:00
|
|
|
/*
|
|
|
|
* Check if we are under Windows and correct location of mysqldump.exe
|
|
|
|
* otherwise return error.
|
|
|
|
*/
|
2015-11-21 01:27:06 +01:00
|
|
|
/**
|
|
|
|
* Method checkWindows.
|
|
|
|
* @param windowsPath String
|
2015-11-21 01:31:18 +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
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyr src bytefile into dst file
|
|
|
|
*/
|
2015-11-21 01:27:06 +01:00
|
|
|
/**
|
|
|
|
* Method copy.
|
|
|
|
* @param src File
|
|
|
|
* @param dst File
|
2015-11-21 01:31:18 +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-21 01:27:06 +01:00
|
|
|
/**
|
|
|
|
* Method setInstance.
|
|
|
|
* @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
|
|
|
/**
|
|
|
|
* Method getInstance.
|
2015-11-21 01:31:18 +01:00
|
|
|
|
|
|
|
* @return AuthMe */
|
2014-08-08 23:14:56 +02:00
|
|
|
public AuthMe getInstance() {
|
|
|
|
return instance;
|
|
|
|
}
|
2013-04-13 01:27:23 +02:00
|
|
|
|
2013-03-09 03:42:17 +01:00
|
|
|
}
|