-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upload config database backups to r2
- Loading branch information
Showing
4 changed files
with
55 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |