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

@@ -0,0 +1,3 @@
MySQL backups are stored in this folder.
Separate game and login server backups are possible.
Just enable the 'BackupDatabase' option in the equivalent server config file.

View File

@@ -57,6 +57,25 @@ MaximumDbConnections = 50
# Default: 0
MaximumDbIdleTime = 0
# ---------------------------------------------------------------------------
# Automatic Database Backup Settings
# ---------------------------------------------------------------------------
# Generate database backups when server restarts or shuts down.
BackupDatabase = False
# Path to MySQL bin folder. Only necessary on Windows.
MySqlBinLocation = C:/xampp/mysql/bin/
# Path where MySQL backups are stored.
BackupPath = ../backup/
# Maximum number of days that backups will be kept.
# Old backups will be deleted.
# Set to 0 to disable.
BackupDays = 30
# Setting emulation off the kernel (package SendStatus)
RemoteWhoLog = True
RemoteWhoSendTrash = True

View File

@@ -67,6 +67,25 @@ MaximumDbConnections = 50
# Default: 0
MaximumDbIdleTime = 0
# ---------------------------------------------------------------------------
# Automatic Database Backup Settings
# ---------------------------------------------------------------------------
# Generate database backups when server restarts or shuts down.
BackupDatabase = False
# Path to MySQL bin folder. Only necessary on Windows.
MySqlBinLocation = C:/xampp/mysql/bin/
# Path where MySQL backups are stored.
BackupPath = ../backup/
# Maximum number of days that backups will be kept.
# Old backups will be deleted.
# Set to 0 to disable.
BackupDays = 30
# Limit fast connections (input username / password)
FastConnectionLimit = 15
# Time of the normal connection (in ms)

View File

@@ -212,6 +212,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 boolean RESERVE_HOST_ON_LOGIN = false;
public static boolean RWHO_LOG;
public static int RWHO_FORCE_INC;
@@ -1367,6 +1371,11 @@ public final class Config
DATABASE_MAX_CONNECTIONS = Integer.parseInt(serverSettings.getProperty("MaximumDbConnections", "10"));
DATABASE_MAX_IDLE_TIME = Integer.parseInt(serverSettings.getProperty("MaximumDbIdleTime", "0"));
BACKUP_DATABASE = Boolean.valueOf(serverSettings.getProperty("BackupDatabase", "false"));
MYSQL_BIN_PATH = serverSettings.getProperty("MySqlBinLocation", "C:/xampp/mysql/bin/");
BACKUP_PATH = serverSettings.getProperty("BackupPath", "../backup/");
BACKUP_DAYS = Integer.parseInt(serverSettings.getProperty("BackupDays", "30"));
DATAPACK_ROOT = new File(serverSettings.getProperty("DatapackRoot", ".")).getCanonicalFile();
final Random ppc = new Random();
@@ -3816,6 +3825,11 @@ public final class Config
DATABASE_MAX_CONNECTIONS = Integer.parseInt(serverSettings.getProperty("MaximumDbConnections", "10"));
DATABASE_MAX_IDLE_TIME = Integer.parseInt(serverSettings.getProperty("MaximumDbIdleTime", "0"));
BACKUP_DATABASE = Boolean.valueOf(serverSettings.getProperty("BackupDatabase", "false"));
MYSQL_BIN_PATH = serverSettings.getProperty("MySqlBinLocation", "C:/xampp/mysql/bin/");
BACKUP_PATH = serverSettings.getProperty("BackupPath", "../backup/");
BACKUP_DAYS = Integer.parseInt(serverSettings.getProperty("BackupDays", "30"));
// Anti Brute force attack on login
BRUT_AVG_TIME = Integer.parseInt(serverSettings.getProperty("BrutAvgTime", "30")); // in Seconds
BRUT_LOGON_ATTEMPTS = Integer.parseInt(serverSettings.getProperty("BrutLogonAttempts", "15"));

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

@@ -20,6 +20,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.datatables.BufferTable;
import com.l2jmobius.gameserver.datatables.OfflineTradeTable;
@@ -480,6 +481,12 @@ public class Shutdown extends Thread
LOGGER.info("All database data committed.");
// Backup database.
if (Config.BACKUP_DATABASE)
{
DatabaseBackup.performBackup();
}
System.runFinalization();
System.gc();

View File

@@ -28,6 +28,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.commons.mmocore.NetcoreConfig;
import com.l2jmobius.commons.mmocore.SelectorConfig;
@@ -189,6 +190,12 @@ public class LoginServer
public void shutdown(boolean restart)
{
// Backup database.
if (Config.BACKUP_DATABASE)
{
DatabaseBackup.performBackup();
}
LoginController.getInstance().shutdown();
System.gc();
Runtime.getRuntime().exit(restart ? 2 : 0);