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

221 lines
6.0 KiB
Java
Raw Normal View History

package fr.xephi.authme;
2013-03-09 03:42:17 +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-03-09 03:42:17 +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 {
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;
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-21 01:27:06 +01:00
* @param instance AuthMe
*/
2014-08-08 23:14:56 +02:00
public PerformBackup(AuthMe instance) {
this.setInstance(instance);
}
/**
*
*/
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.
*
* @return boolean
*/
2015-09-15 19:07:34 +02:00
public boolean doBackup() {
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;
}
2015-11-21 01:27:06 +01:00
/**
* Method MySqlBackup.
*
* @return boolean
*/
2014-08-08 23:14:56 +02:00
private boolean MySqlBackup() {
File dirBackup = new File(AuthMe.getInstance().getDataFolder() + "/backups");
2014-08-08 23:14:56 +02:00
if (!dirBackup.exists())
dirBackup.mkdir();
2014-08-08 23:14:56 +02:00
if (checkWindows(Settings.backupWindowsPath)) {
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 {
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;
}
2015-11-21 01:27:06 +01:00
/**
* Method FileBackup.
*
2015-11-21 01:27:06 +01:00
* @param backend String
* @return boolean
*/
2014-08-08 23:14:56 +02:00
private boolean FileBackup(String backend) {
File dirBackup = new File(AuthMe.getInstance().getDataFolder() + "/backups");
2014-08-08 23:14:56 +02:00
if (!dirBackup.exists())
dirBackup.mkdir();
2013-03-09 03:42:17 +01:00
try {
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;
}
2015-11-21 01:27:06 +01:00
/**
* Method checkWindows.
*
2015-11-21 01:27:06 +01:00
* @param windowsPath String
* @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 {
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
/*
* 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-21 01:27:06 +01:00
/**
* Method copy.
*
2015-11-21 01:27:06 +01:00
* @param src File
* @param dst File
* @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
}
/*
* 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-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
/**
* 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
}