Configurable automatic database backups.

This commit is contained in:
MobiusDev
2018-05-01 07:23:13 +00:00
parent 0c6b352227
commit d2ed161dda
61 changed files with 1185 additions and 0 deletions

View File

@@ -859,6 +859,10 @@ public final class Config
public static String DATABASE_PASSWORD;
public static int DATABASE_MAX_CONNECTIONS;
public static int DATABASE_MAX_IDLE_TIME;
public static boolean BACKUP_DATABASE;
public static String MYSQL_BIN_PATH;
public static String BACKUP_PATH;
public static int BACKUP_DAYS;
public static int MAXIMUM_ONLINE_USERS;
public static String CNAME_TEMPLATE;
public static String PET_NAME_TEMPLATE;
@@ -1374,6 +1378,11 @@ public final class Config
DATABASE_MAX_CONNECTIONS = serverSettings.getInt("MaximumDbConnections", 10);
DATABASE_MAX_IDLE_TIME = serverSettings.getInt("MaximumDbIdleTime", 0);
BACKUP_DATABASE = serverSettings.getBoolean("BackupDatabase", false);
MYSQL_BIN_PATH = serverSettings.getString("MySqlBinLocation", "C:/xampp/mysql/bin/");
BACKUP_PATH = serverSettings.getString("BackupPath", "../backup/");
BACKUP_DAYS = serverSettings.getInt("BackupDays", 30);
try
{
DATAPACK_ROOT = new File(serverSettings.getString("DatapackRoot", ".").replaceAll("\\\\", "/")).getCanonicalFile();
@@ -3332,6 +3341,11 @@ public final class Config
DATABASE_MAX_CONNECTIONS = ServerSettings.getInt("MaximumDbConnections", 10);
DATABASE_MAX_IDLE_TIME = ServerSettings.getInt("MaximumDbIdleTime", 0);
BACKUP_DATABASE = ServerSettings.getBoolean("BackupDatabase", false);
MYSQL_BIN_PATH = ServerSettings.getString("MySqlBinLocation", "C:/xampp/mysql/bin/");
BACKUP_PATH = ServerSettings.getString("BackupPath", "../backup/");
BACKUP_DAYS = ServerSettings.getInt("BackupDays", 30);
SHOW_LICENCE = ServerSettings.getBoolean("ShowLicence", true);
AUTO_CREATE_ACCOUNTS = ServerSettings.getBoolean("AutoCreateAccounts", true);

View File

@@ -0,0 +1,82 @@
/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.commons.database;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.concurrent.TimeUnit;
import com.l2jmobius.Config;
import com.l2jmobius.Server;
/**
* @author Mobius
*/
public class DatabaseBackup
{
public static void performBackup()
{
// Delete old files.
if (Config.BACKUP_DAYS > 0)
{
final long cut = LocalDateTime.now().minusDays(Config.BACKUP_DAYS).toEpochSecond(ZoneOffset.UTC);
final Path path = Paths.get(Config.BACKUP_PATH);
try
{
Files.list(path).filter(n ->
{
try
{
return Files.getLastModifiedTime(n).to(TimeUnit.SECONDS) < cut;
}
catch (Exception ex)
{
return false;
}
}).forEach(n ->
{
try
{
Files.delete(n);
}
catch (Exception ex)
{
}
});
}
catch (Exception e)
{
}
}
// Dump to file.
final String mysqldumpPath = System.getProperty("os.name").toLowerCase().indexOf("win") >= 0 ? Config.MYSQL_BIN_PATH : "";
try
{
final Process process = Runtime.getRuntime().exec(mysqldumpPath + "mysqldump -u " + Config.DATABASE_LOGIN + (Config.DATABASE_PASSWORD.trim().isEmpty() ? "" : " -p" + Config.DATABASE_PASSWORD) + " " + Config.DATABASE_URL.replace("jdbc:mysql://", "").replaceAll(".*\\/|\\?.*", "") + " -r " + Config.BACKUP_PATH + (Server.serverMode == Server.MODE_GAMESERVER ? "game" : "login") + new SimpleDateFormat("_yyyy_MM_dd_HH_mm'.sql'").format(new Date()));
process.waitFor();
}
catch (Exception e)
{
}
}
}

View File

@@ -21,6 +21,7 @@ import java.util.logging.Logger;
import com.l2jmobius.Config;
import com.l2jmobius.commons.concurrent.ThreadPool;
import com.l2jmobius.commons.database.DatabaseBackup;
import com.l2jmobius.commons.database.DatabaseFactory;
import com.l2jmobius.gameserver.data.sql.impl.ClanTable;
import com.l2jmobius.gameserver.data.sql.impl.OfflineTradersTable;
@@ -270,6 +271,12 @@ public class Shutdown extends Thread
{
}
// Backup database.
if (Config.BACKUP_DATABASE)
{
DatabaseBackup.performBackup();
}
// server will quit, when this function ends.
if (getInstance()._shutdownMode == GM_RESTART)
{

View File

@@ -30,6 +30,7 @@ import java.util.logging.Logger;
import com.l2jmobius.Config;
import com.l2jmobius.Server;
import com.l2jmobius.commons.database.DatabaseBackup;
import com.l2jmobius.commons.database.DatabaseFactory;
import com.l2jmobius.loginserver.network.ClientNetworkManager;
@@ -206,6 +207,10 @@ public final class LoginServer
public void shutdown(boolean restart)
{
if (Config.BACKUP_DATABASE)
{
DatabaseBackup.performBackup();
}
Runtime.getRuntime().exit(restart ? 2 : 0);
}
}