Skip to content

Commit

Permalink
upload config database backups to r2
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Nov 27, 2023
1 parent cf33302 commit 491df08
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ BOT_TOKEN=my_epic_token
REDIS_URL=redis://redis.com:6969
REDIS_USERNAME=redis
REDIS_PASSWORD=password123
BUCKET_URL=https://<ACCOUNT_ID>.r2.cloudflarestorage.com
BUCKET_NAME=config-backups
AWS_ACCESS_KEY_ID=123123
AWS_SECRET_ACCESS_KEY=123123
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@
<artifactId>sqlite-jdbc</artifactId>
<version>3.44.0.0</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>2.21.30</version>
</dependency>
</dependencies>

<build>
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/vc/config/GuildConfigDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ public class GuildConfigDatabase {
ZoneId.of("America/Los_Angeles"));
private final Path backupPath = Paths.get("backups");
private final Connection connection;
private final RemoteDatabaseBackup remoteDatabaseBackup;

public GuildConfigDatabase() {
public GuildConfigDatabase(final RemoteDatabaseBackup remoteDatabaseBackup) {
this.remoteDatabaseBackup = remoteDatabaseBackup;
try {
final Path dbPath = Paths.get("guild-config.db");
connection = DriverManager.getConnection("jdbc:sqlite:" + dbPath);
Expand All @@ -53,7 +55,9 @@ public void backupDatabase() {
if (!backupPath.toFile().exists()) {
backupPath.toFile().mkdirs();
}
connection.createStatement().executeUpdate("BACKUP TO 'backups/guild-config-backup-" + DATE_FORMATTER.format(Instant.now()) + ".db'");
var backupPath = "backups/guild-config-backup-" + DATE_FORMATTER.format(Instant.now()) + ".db";
connection.createStatement().executeUpdate("BACKUP TO '" + backupPath + "'");
remoteDatabaseBackup.uploadDatabaseBackup(backupPath);
} catch (final Exception e) {
LOGGER.error("Error backing up guild config database", e);
}
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/vc/config/RemoteDatabaseBackup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package vc.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.services.s3.S3Client;

import java.net.URI;
import java.nio.file.Paths;

@Component
public class RemoteDatabaseBackup {
private static final Logger LOGGER = LoggerFactory.getLogger("RemoteDatabaseBackup");
private final S3Client s3Client;
private final String bucketName;

public RemoteDatabaseBackup(
@Value("${BUCKET_URL}") final String bucketUrl,
@Value("${AWS_ACCESS_KEY_ID}") final String awsAccessKeyId,
@Value("${AWS_SECRET_ACCESS_KEY}") final String awsSecretAccessKey,
@Value("${BUCKET_NAME}") final String bucketName) {
this.bucketName = bucketName;
s3Client = S3Client.builder()
.endpointOverride(URI.create(bucketUrl))
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(awsAccessKeyId, awsSecretAccessKey)))
.build();
}

public void uploadDatabaseBackup(final String backupPath) {
try {
s3Client.putObject(builder -> builder.bucket(bucketName).key(backupPath).build(), Paths.get(backupPath));
LOGGER.info("Uploaded database backup: {}", backupPath);
} catch (final Exception e) {
LOGGER.error("Error uploading database backup: {}", backupPath, e);
}
}
}

0 comments on commit 491df08

Please sign in to comment.