Create backup of backpack in case of errors

This should prevent lost backpacks and should allow to restore/recover them in case of problems
This commit is contained in:
GeorgH93 2018-05-23 20:16:50 +02:00
parent b3993fcc9a
commit 5c75bea337
No known key found for this signature in database
GPG Key ID: D1630D37F9E4B3C8
3 changed files with 28 additions and 9 deletions

View File

@ -57,7 +57,7 @@
<dependency>
<groupId>at.pcgamingfreaks</groupId>
<artifactId>PluginLib</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>1.0.2-SNAPSHOT</version>
</dependency>
</dependencies>

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2017 GeorgH93
* Copyright (C) 2016-2018 GeorgH93
*
* 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
@ -33,7 +33,7 @@
public class Files extends Database
{
private static final String EXT = ".backpack", EXT_REGEX = "\\.backpack";
protected static final String EXT = ".backpack", EXT_REGEX = "\\.backpack";
private final File saveFolder;
@ -199,10 +199,7 @@ public boolean accept(File file)
if (!file.isDirectory())
{
String path = file.getAbsolutePath().toLowerCase();
if ((path.endsWith(extension) && (path.charAt(path.length() - extension.length() - 1)) == '.'))
{
return true;
}
return (path.endsWith(extension) && (path.charAt(path.length() - extension.length() - 1)) == '.');
}
return false;
}

View File

@ -33,12 +33,13 @@
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.*;
import java.util.*;
public abstract class SQL extends Database
{
//TODO load cooldown
{ //TODO load cooldown
private HikariDataSource dataSource;
protected String tablePlayers, tableBackpacks, tableCooldowns; // Table Names
@ -46,6 +47,7 @@ public abstract class SQL extends Database
@Language("SQL") protected String queryUpdatePlayerAdd, queryGetPlayerID, queryInsertBp, queryUpdateBp, queryGetBP, queryDeleteOldBackpacks, queryGetUnsetOrInvalidUUIDs, queryFixUUIDs; // DB Querys
@Language("SQL") protected String queryDeleteOldCooldowns, querySyncCooldown, queryGetCooldown; // DB Querys
protected boolean updatePlayer, syncCooldown;
private final File backupFolder;
public SQL(Minepacks mp)
{
@ -91,6 +93,8 @@ public SQL(Minepacks mp)
e.printStackTrace();
}
}
backupFolder = new File(this.plugin.getDataFolder(), "backups");
}
protected abstract @Nullable HikariConfig getPoolConfig();
@ -321,6 +325,7 @@ public void saveBackpack(final Backpack backpack)
else
{
plugin.getLogger().warning("Failed saving backpack for: " + name + " (Unable to get players ID from database)");
writeBackup(nameOrUUID, usedSerializer, data);
}
}
}
@ -332,11 +337,28 @@ public void saveBackpack(final Backpack backpack)
}
catch(SQLException e)
{
plugin.getLogger().warning("Failed to save backpack in database! Error: " + e.getMessage());
e.printStackTrace();
writeBackup(nameOrUUID, usedSerializer, data);
}
});
}
private void writeBackup(final String userIdentifier, final int usedSerializer, final byte[] data)
{
File save = new File(backupFolder, userIdentifier + "_" + System.currentTimeMillis() + Files.EXT);
try(FileOutputStream fos = new FileOutputStream(save))
{
fos.write(usedSerializer);
fos.write(data);
plugin.getLogger().info("Backup of the backpack has been created: " + save.getAbsolutePath());
}
catch(Exception e2)
{
plugin.getLogger().warning("Failed to write backup! Error: " + e2.getMessage());
}
}
@Override
protected void loadBackpack(final OfflinePlayer player, final Callback<Backpack> callback)
{