Skip to content

Commit

Permalink
rolling backups deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Nov 4, 2023
1 parent 9fd8282 commit 9105719
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
41 changes: 37 additions & 4 deletions src/main/java/vc/config/GuildConfigDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
Expand All @@ -17,6 +19,11 @@

public class GuildConfigDatabase {
private static final Logger LOGGER = LoggerFactory.getLogger(GuildConfigDatabase.class);
// backups older than this date will be deleted
private static final Duration ROLLING_BACKUP_DURATION = Duration.ofDays(7);
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss").withLocale(Locale.US).withZone(
ZoneId.of("America/Los_Angeles"));
private final Path backupPath = Paths.get("backups");
private final Connection connection;

public GuildConfigDatabase() {
Expand All @@ -41,16 +48,42 @@ private void close() {

public void backupDatabase() {
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss").withLocale(Locale.US).withZone(
ZoneId.of("America/Los_Angeles"));
final Path backupPath = Paths.get("backups");
if (!backupPath.toFile().exists()) {
backupPath.toFile().mkdirs();
}
connection.createStatement().executeUpdate("BACKUP TO 'backups/guild-config-backup-" + formatter.format(Instant.now()) + ".db'");
connection.createStatement().executeUpdate("BACKUP TO 'backups/guild-config-backup-" + DATE_FORMATTER.format(Instant.now()) + ".db'");
} catch (final Exception e) {
LOGGER.error("Error backing up guild config database", e);
}
cleanOldBackups();
}

private void cleanOldBackups() {
try {
if (!backupPath.toFile().exists()) {
return;
}
File[] files = backupPath.toFile().listFiles();
if (files == null) {
LOGGER.warn("no backups found?");
return;
}
for (final File file : files) {
if (file.getName().startsWith("guild-config-backup-")) {
final String dateString = file.getName().substring("guild-config-backup-".length(), "guild-config-backup-".length() + "yyyy-MM-dd-HH-mm-ss".length());
final Instant date = Instant.from(DATE_FORMATTER.parse(dateString));
if (date.isBefore(Instant.now().minus(ROLLING_BACKUP_DURATION))) {
LOGGER.info("Deleting old guild config database backup {}", file.getName());
if (!file.delete()) {
LOGGER.warn("Failed to delete old guild config database backup {}", file.getName());
}
}
}
}
LOGGER.info("Completed cleaning old backups");
} catch (final Exception e) {
LOGGER.error("Error cleaning old guild config database backups", e);
}
}

private void createGuildConfigTable() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/vc/live/LiveChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected GuildConfigRecord disableRecordInternal(final GuildConfigRecord in) {

@Override
protected GuildConfigRecord enableRecordInternal(final GuildConfigRecord in, final String guildId, final String channelId) {
return new GuildConfigRecord(in.guildId(), in.guildName(), true, channelId, in.liveConnectionsEnabled(), in.liveConnectionsChannelId());
return new GuildConfigRecord(guildId, in.guildName(), true, channelId, in.liveConnectionsEnabled(), in.liveConnectionsChannelId());
}

@Override
Expand Down

0 comments on commit 9105719

Please sign in to comment.