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

154 lines
4.6 KiB
Java
Raw Normal View History

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;
import fr.xephi.authme.settings.Settings;
2013-03-09 03:42:17 +01:00
/**
*
* @author stefano
*/
public class PerformBackup {
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());
private String path = AuthMe.getInstance().getDataFolder()
+ "/backups/backup" + dateString;
private AuthMe instance;
public PerformBackup(AuthMe instance) {
this.setInstance(instance);
}
2014-08-08 23:14:56 +02:00
public boolean DoBackup() {
2014-08-08 23:14:56 +02:00
switch (Settings.getDataSource) {
case FILE:
return FileBackup("auths.db");
2014-08-08 23:14:56 +02:00
case MYSQL:
return MySqlBackup();
2013-03-09 03:42:17 +01:00
2014-08-08 23:14:56 +02:00
case SQLITE:
return FileBackup(Settings.getMySQLDatabase + ".db");
2013-03-09 03:42:17 +01:00
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;
}
2014-08-08 23:14:56 +02:00
private boolean MySqlBackup() {
File dirBackup = new File(AuthMe.getInstance().getDataFolder()
+ "/backups");
if (!dirBackup.exists()) dirBackup.mkdir();
if (checkWindows(Settings.backupWindowsPath)) {
String executeCmd = Settings.backupWindowsPath
+ "\\bin\\mysqldump.exe -u " + dbUserName + " -p"
+ dbPassword + " " + dbName + " --tables " + tblname
+ " -r " + path + ".sql";
Process runtimeProcess;
try {
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
ConsoleLogger.info("Backup created successfully");
return true;
} else {
ConsoleLogger.info("Could not create the backup");
}
} catch (Exception ex) {
ex.printStackTrace();
}
} else {
String executeCmd = "mysqldump -u " + dbUserName + " -p"
+ dbPassword + " " + dbName + " --tables " + tblname
+ " -r " + path + ".sql";
Process runtimeProcess;
try {
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
ConsoleLogger.info("Backup created successfully");
return true;
} else {
ConsoleLogger.info("Could not create the backup");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
return false;
}
2014-08-08 23:14:56 +02:00
private boolean FileBackup(String backend) {
File dirBackup = new File(AuthMe.getInstance().getDataFolder()
+ "/backups");
if (!dirBackup.exists()) dirBackup.mkdir();
2013-03-09 03:42:17 +01:00
try {
2014-08-08 23:14:56 +02:00
copy(new File("plugins/AuthMe/" + backend), new File(path + ".db"));
return true;
2013-03-09 03:42:17 +01:00
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
2014-08-08 23:14:56 +02:00
/*
* Check if we are under Windows and correct location of mysqldump.exe
* otherwise return error.
*/
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");
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
*/
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
}
2014-08-08 23:14:56 +02:00
public void setInstance(AuthMe instance) {
this.instance = instance;
}
2013-03-09 03:42:17 +01:00
2014-08-08 23:14:56 +02:00
public AuthMe getInstance() {
return instance;
}
2013-03-09 03:42:17 +01:00
}